一、tasks:任务,即调用模块完成的操做 二、variables:变量 三、templates:模板 四、handlers:处理器,当条件知足时执行操做,一般前面使用notify声明。 五、roles:角色,分门别类管理playbook的一种方式,后面将详细介绍roles的用法。
vim /opt/test.yamlphp
- hosts: webserver //定义的主机组,即应用的主机 vars: //定义变量 http_port: 80 //此处自定义变量中变量写在yaml文件,也可写在ansible/hosts文件中应用主机后 max_clients: 200 remote_user: root //远程用户为root tasks: //执行的任务 - name: ensure apache is at the latest version //下面即将执行的步骤名称 yum: pkg=httpd state=latest - name: write the apache config file //下面即将执行的步骤名称 template: src=/srv/httpd.j2 dest=/etc/httpd.conf //模板文件,src为路径为控制端,dest为被控制端 notify: //声明须要执行的操做 - restart apache //操做名称 - name: ensure apache is running service: name=httpd state=started handlers: //处理器,处理名称须要对应notify的名称,才能自动执行 - name: restart apache service: name=httpd state=restarted
ansible-playbook test.yaml --syntax-check #检查yaml文件的语法是否正确 ansible-playbook test.yaml --list-task #检查tasks任务 ansible-playbook test.yaml --list-hosts #检查生效的主机 ansible-playbook test.yaml --start-at-task='Copy Nginx.conf' #指定从某个task开始运行
--- #表示该文件是YAML文件,非必须 - hosts: webserver #指定主机组,能够是一个或多个组。 remote_user: root #指定远程主机执行的用户名 还能够为每一个任务定义远程执行用户: --- - hosts: mysql remote_user: root tasks: - name: test connection ping: remote_user: mysql #指定远程主机执行tasks的运行用户为mysql
ansible-playbook ping.yaml mysql
--- - hosts: mysql remote_user: root become: yes #2.6版本之后的参数,以前是sudo,意思为切换用户运行 become_user: mysql #指定sudo用户为mysql
Handlers也是一些task的列表,和通常的task并无什么区别。是由通知者进行的notify,若是没有被notify,则Handlers不会执行,假如被notify了,则Handlers被执行,无论有多少个通知者进行了notify,等到play中的全部task执行完成以后,handlers也只会被执行一次。web
- hosts: webserver remote_user: root tasks: - name: install httpd package yum: name=httpd state=latest - name: install configuration file for httpd copy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf notify: //声明后面Handlers的名称 -restart httpd //注意该名称为notify的子集,须要靠空格隔开 - name: start httpd service service: enabled=true name=httpd state=started handlers: - name: restart httpd service: name=httpd state=restarted 也能够引入变量 --- - hosts: webserver remote_user: root vars: //引入变量 - package: httpd //变量名称 - service: httpd tasks: - name: install httpd package yum: name={{package}} state=latest //引入变量方式 - name: install configuration file for httpd copy: src=/opt/httpd.conf dest=/etc/httpd/conf/httpd.conf notify: -restart httpd - name: start httpd service service: enabled=true name={{service}} state=started handlers: - name: restart httpd //handlers执行的操做必须是notify中声明的操做名称,若此处执行的操做名称notify中没有声明,那么handlers不会执行 service: name={{service}} state=restarted
在引入变量过程当中,若针对一个组里不一样主机须要引入的变量值不相同时,好比IP地址等,则须要在hosts文件中,在不一样主机后面跟上变量名称与值,yaml文件中执行操做时引入变量名称便可。sql
vim /etc/ansible/hostsshell
[webserver] 192.168.144.110 httpd_port="192.168.144.110:80" server_name="www.yun01.com:80" 192.168.144.111 httpd_port="192.168.144.111:80" server_name="www.yun02.com:80"
Jinja是基于Python的模板引擎。template类是jinja的另外一个重要组件,能够看作是编译过的模板文件,用来生产目标文件,传递Python的变量给模板去替换模板中的标记。apache
cp /etc/httpd/conf/httpd.conf /opt/httpd.conf.j2 复制模板,注意文件格式为j2结尾
vim /opt/httpd.conf.j2vim
Listen {{httpd_port}} //为须要修改的部分设定变量 ... ServerName {{server_name}}
vim /etc/ansible/hosts安全
[webserver] 192.168.144.110 httpd_port="192.168.144.110:80" server_name="www.yun01.com:80" 192.168.144.111 httpd_port="192.168.144.111:80" server_name="www.yun02.com:80"
vim /opt/httpd.yaml服务器
- hosts: webserver //针对webserver组执行 vars: package: httpd //变量名称 server: httpd tasks: - name: install httpd yum: name={{package}} state=latest - name: write the apache config file template: src=/opt/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf //注意模板文件位置 notify: restart httpd - name: start httpd service: name={{server}} enabled=true state=started handlers: - name: restart httpd service: name={{server}} state=restarted
vi when.yml --- - hosts: mysql remote_user: root tasks: - name: "shutdown CentOS" command: /sbin/shutdown -h now when: ansible_distribution == "CentOS"
vi when.yml --- - hosts: mysql remote_user: root tasks: - name: "shut down CentOS 7 systems" command: /sbin/shutdown -r now when: - ansible_distribution == "CentOS" - ansible_distribution_major_version == "7"
vi when.yml --- - hosts: mysql remote_user: root tasks: - name: "shut down CentOS 6 and Debian 7 systems" command: /sbin/shutdown -t now when: (ansible_distribution == "CentOS" and ansible_distribution_major_version == "6") or (ansible_distribution == "Debian" and ansible_distribution_major_version == "7")
vi when.yml --- - hosts: all vars: exist: "True" tasks: - name: creaet file command: touch /tmp/test.txt when: exist | match("True") - name: delete file command: rm -rf /tmp/test.txt when: exist | match("False")
--- - hosts: webserver remote_user: root tasks: - name: "Install Packages" yum: name={{ item }} state=latest with_items: - httpd - mysql-server - php
--- - hosts: webserver remote_user: root tasks: - name: "Add users" user: name={{ item.name }} state=present groups={{ item.groups }} with_items: - { name:'test1', groups:'wheel'} - { name:'test2', groups:'root'}
vi hosts.yml --- - hosts: webserver remote_user: root tasks: - name: Copy hosts file copy: src=/etc/hosts dest=/etc/hosts tags: - only - name: touch file file: path=/opt/hosts state=touch
执行命令:ide
ansible-playbook hosts.yml --tags="only"
ansible-playbook hosts.yml
vi hosts.yml --- - hosts: webserver remote_user: root tasks: - name: Copy hosts file copy: src=/etc/hosts dest=/etc/hosts tags: - only - name: copy test file copy: src=/etc/test.txt dest=/opt/test.txt - name: touch file file: path=/opt/hosts state=touch tags: - always
执行命令:
ansible-playbook hosts.yml --tags="only"