playbook是由一个或多个play组成的列表,ansible能够直接调用playbook,按照事先定义好的规则在远程主机一次执行playbook上的操做。ansible的playbook是采用YAML的形式,文件后缀为.yaml,须要遵循YAML的语法进行定义。web
注意:
playbook一个-name只能有一个模块被调用,不然会报错服务器
ansible-playbook --syntax-check user.yml #测试语法是否正确 ansible-playbook -C user.yml #测试执行,并非真正运行 ansible-playbook user.yml #运行playbook
[test] 192.168.189.129 user=test_3调用方法:
\- hosts: test remote_user: root tasks: \- name: add {{ user }} #经过{{ 变量名 }}调用变量 user: name={{ user }} state=present system=yes
[test] 192.168.189.129 [test:vars] user=test_4
\ - hosts: test remote_user: root vars: \ - user: test_5 \ - password: 1234567a tasks: \ - name: add {{ user }} user: name={{ user }} state=present system=yes password={{ password }}
[test] 192.168.189.129 ansible_ssh_pass=1234567a
- hosts: test vars: - service: httpd remote_user: root tasks: - name: install {{ service }} yum: name={{ service }} state=latest - name: install configure file copy: src=/root/httpd.conf dest=/etc/httpd/conf/httpd.conf notify: restart httpd - name: start {{ service }} service: name= {{ service }} state=started enabled=yes handlers: - name: restart httpd service: name=httpd state=restarted
- hosts: test remote_user: root tasks: - name: print hello world copy: content='hello world' dest=/tmp/test.log when: facter_ipaddress == '192.168.189.129' #只有当条件知足的时候才执行此tasks
playbook常常须要重复执行某指令,此时可使用迭代,迭代固定用法以下:ssh
- hosts: test remote_user: root tasks: - name: del user user: name={{ item }} state=present #此处固定必须为item,表示调用迭代对象 with_items: #此处固定为with_items,下面一次列出迭代对象 - test_2 - test_3 - test_4 - test_5
也能够采用字典列表的方式来进行item调用:测试
- hosts: all remote_user: root tasks: - name: create groups group: name={{ item }} state=present with_items: - groupx1 - groupx2 - groupx3 - name: create users user: name={{ item.name }} group={{ item.group }} state=present with_items: - {name: 'userx1', group: 'groupx1'} - {name: 'userx2', group: 'groupx2'} - {name: 'userx3', group: 'groupx3'}
template文件可使用变量来代替某些固定的值,这样能够针对不一样的服务器提供不一样的配置文件。
变量支持以下类型:命令行
- hosts: test vars: - service: httpd remote_user: root tasks: - name: install {{ service }} yum: name={{ service }} state=latest - name: install configure file template: src=/etc/ansible/templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf #template文件能够用j2结尾,表明是jinjia2文件 notify: restart httpd - name: start {{ service }} service: name= {{ service }} state=started enabled=yes handlers: - name: restart httpd service: name=httpd state=restarted
能够对某一个task打上tag标签,这样在执行playbook的时候能够直接指定要执行的tag,就不用总体在把全部的task再执行一遍了rest
- hosts: test vars: - service: httpd remote_user: root tasks: - name: install {{ service }} yum: name={{ service }} state=latest - name: install configure file template: src=/etc/ansible/templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf notify: restart httpd tags: conf #此处的conf表示tag名 - name: start {{ service }} service: name= {{ service }} state=started enabled=yes handlers: - name: restart httpd
调动tag的命令:code
ansible-playbook httpd.yml -t conf ansible-playbook httpd.yml --tags conf
roles能够对多个playbook进行复用,好比说A主机须要安装httpd,B主机也须要安装httpd,可是他们又没有在一个主机组,这个时候正常来讲须要写两遍playbook,就形成了重复现象。此时能够采用roles,对两个主机都指定role为httpd所在的roles便可进行playbook的复用。
roles有特定的格式,首先须要有一个roles目录,而后在目录下面须要有各个role的主目录,主目录下面为各个分目录:对象
#site.yml文件,下面文件表明给test配置websers何dbsers的role - hosts: test remote_user: root roles: - websers - dbsers
#tasks下main.yml文件 - name: install httpd yum: name=httpd state=latest - name: install config template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf #template和copy模块的src能够直接调用相对路径,相对roles/websers/files notify: restart httpd tags: conf - name: start httpd service: name=httpd state=started enabled=yes
#handlers下的main.yml文件 - name: restart httpd service: name=httpd state=restarted