playbook
1.调用剧本
ansible-playbook /etc/ansible/playbook/book001.yml
2.编写剧本
3.剧本的语法
---
- hosts:
remote_user:
vars:
- a
tasks:
- name: 说明
调用模块
notify:
- handlername:
-handlers:
- name: handlername
模块语句
...
[root@m0 ~]# ansible group02 -m ping
[root@m0 ~]# vim /etc/ansible/playbook/nginx.yml
---
- hosts: group02
remote_user: root
tasks:
- name: 卸载httpd
yum: name=httpd state=present
- name: 安装nginx
yum: name=nginx state=absent
- name: 修改资源文件
shell: echo 'i am nginx,port is 80' > /usr/share/nginx/html/index.html
- name: 修改端口
command: sed -i '/listen/s/80/8080/g' /etc/nginx/nginx.conf
notify:
- restart nginx
- name: 启动服务
service: name=nginx state=started enabled=yes
handlers:
- name: restart nginx
service: name=nginx state=restarted
...
roles模块
[root@m0 ~]# cd /etc/ansible/roles/
[root@m0 roles]# ls
[root@m0 roles]#
[root@m0 roles]# mkdir nginx
[root@m0 roles]# ls
nginx
[root@m0 roles]# vim /etc/ansible/playbook/nginx.yml
[root@m0 roles]# cd nginx/
[root@m0 nginx]# mkdir files
[root@m0 nginx]# mkdir tasks
[root@m0 nginx]# mkdir handlers
[root@m0 nginx]# mkdir templates
[root@m0 nginx]# mkdir vars
[root@m0 nginx]# mkdir tree
[root@m0 nginx]# rm -rf tree
[root@m0 nginx]# ls
files handlers tasks templates vars
[root@m0 nginx]# touch files/main.yml
[root@m0 nginx]# touch handlers/main.yml
[root@m0 nginx]# touch tasks/main.yml
[root@m0 nginx]# touch vars/main.yml
[root@m0 nginx]# rm -rf files/main.yml
[root@m0 nginx]# vim /etc/ansible/playbook/test002.yml
---
- hosts: group02
remote_user: root
roles:
- nginx
...
[root@m0 nginx]# ansible-playbook /etc/ansible/playbook/test002.yml
PLAY [group02] *************************************************************************
TASK [Gathering Facts] *****************************************************************
ok: [other]
ok: [192.168.2.61]
ok: [192.168.2.60]
PLAY RECAP *****************************************************************************
192.168.2.60 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.2.61 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
other : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@m0 nginx]# vim /etc/ansible/roles/nginx/tasks/main.yml
---
- name: 卸载httpd
yum: name=httpd state=absent
- name: 卸载nginx
yum: name=nginx state=present
- name: 安装nginx
yum: name=nginx state=present
- name: 启动服务
service: name=nginx state=started enabled=yes
[root@m0 nginx]# ansible-playbook /etc/ansible/playbook/test002.yml
PLAY [group02] *************************************************************************
TASK [Gathering Facts] *****************************************************************
ok: [other]
ok: [192.168.2.60]
ok: [192.168.2.61]
TASK [nginx : 卸载httpd] *****************************************************************
changed: [other]
changed: [192.168.2.61]
changed: [192.168.2.60]
TASK [卸载nginx] *************************************************************************
changed: [192.168.2.60]
changed: [192.168.2.61]
changed: [other]
TASK [安装nginx] *************************************************************************
ok: [192.168.2.60]
ok: [192.168.2.61]
ok: [other]
TASK [nginx : 启动服务] ********************************************************************
changed: [192.168.2.60]
changed: [192.168.2.61]
changed: [other]
PLAY RECAP *****************************************************************************
192.168.2.60 : ok=5 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.2.61 : ok=5 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
other : ok=5 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@m0 nginx]# vim /etc/ansible/playbook/test002.yml
[root@m0 nginx]# ansible-playbook /etc/ansible/playbook/test002.yml
PLAY [group02] *************************************************************************
TASK [Gathering Facts] *****************************************************************
ok: [192.168.2.60]
ok: [other]
ok: [192.168.2.61]
TASK [nginx : 卸载httpd] *****************************************************************
ok: [192.168.2.61]
ok: [192.168.2.60]
ok: [other]
TASK [卸载nginx] *************************************************************************
ok: [192.168.2.60]
ok: [192.168.2.61]
ok: [other]
TASK [安装nginx] *************************************************************************
ok: [192.168.2.60]
ok: [192.168.2.61]
ok: [other]
TASK [nginx : 启动服务] ********************************************************************
ok: [192.168.2.61]
ok: [192.168.2.60]
ok: [other]
TASK [mysql : 卸载httpd] *****************************************************************
ok: [192.168.2.60]
ok: [192.168.2.61]
ok: [other]
TASK [mysql : 卸载nginx] *****************************************************************
ok: [192.168.2.60]
ok: [other]
ok: [192.168.2.61]
TASK [mysql : 安装nginx] *****************************************************************
ok: [192.168.2.61]
ok: [192.168.2.60]
ok: [other]
TASK [mysql : 启动服务] ********************************************************************
ok: [192.168.2.60]
ok: [192.168.2.61]
ok: [other]
PLAY RECAP *****************************************************************************
192.168.2.60 : ok=9 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.2.61 : ok=9 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
other : ok=9 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0