playbook 指的是用于配置管理的脚本, 实际上playbook才是ansible的重头戏
我将尝试使用一个playbook同时控制之前创建的3个虚拟机(docker 容器)
先看一个简单脚本
[root@vagrant playbooks]# vi hostname.yml
- name: call hostname
hosts: dockers
sudo: False
tasks:
- name: install
shell: hostname > /hostname.txt
直接调用
[root@vagrant playbooks]# ansible-playbook hostname.yml
PLAY [call hostname] **********************************************************
GATHERING FACTS ***************************************************************
ok: [172.17.0.4]
ok: [172.17.0.3]
ok: [172.17.0.2]
TASK: [install] ***************************************************************
changed: [172.17.0.3]
changed: [172.17.0.4]
changed: [172.17.0.2]
PLAY RECAP ********************************************************************
172.17.0.2 : ok=2 changed=1 unreachable=0 failed=0
172.17.0.3 : ok=2 changed=1 unreachable=0 failed=0
172.17.0.4 : ok=2 changed=1 unreachable=0 failed=0
检查一下结果

下面是一个安装httpd的脚本
[root@vagrant playbooks]# vi httpd.yml
- name: install httpd from yum
hosts: dockers
sudo: False
tasks:
- name: install
shell: yum install httpd
- name: start httpd
shell: /usr/sbin/httpd
调用 ansible-playbook ./playbooks/httpd.yml
可以看到log中
[root@vagrant log]# tail -500f ansible.log
2017-06-06 14:55:23,353 p=19632 u=root |
2017-06-06 14:55:23,354 p=19632 u=root | /usr/bin/ansible dockers -a hostname
2017-06-06 14:55:23,354 p=19632 u=root |
2017-06-06 14:55:25,190 p=19632 u=root | 172.17.0.3 | success | rc=0 >>
fd62f7fed827
2017-06-06 14:55:25,195 p=19632 u=root | 172.17.0.2 | success | rc=0 >>
86b87edef2cd
2017-06-06 14:55:25,195 p=19632 u=root | 172.17.0.4 | success | rc=0 >>
7c6125cedfd3
2017-06-06 15:43:02,383 p=21343 u=root |
2017-06-06 15:43:02,384 p=21343 u=root | /usr/bin/ansible-playbook ./playbooks/httpd.yml
2017-06-06 15:43:02,384 p=21343 u=root |
2017-06-06 15:43:02,387 p=21343 u=root | ERROR: Syntax Error while loading YAML script, ./playbooks/httpd.yml
Note: The error may actually appear before this position: line 6, column 5
- name: install
shell: yum install httpd
^
2017-06-06 15:45:22,766 p=21408 u=root |
2017-06-06 15:45:22,766 p=21408 u=root | /usr/bin/ansible-playbook ./playbooks/httpd.yml
2017-06-06 15:45:22,766 p=21408 u=root |
2017-06-06 15:45:22,795 p=21408 u=root | PLAY [install httpd from yum] *************************************************
2017-06-06 15:45:22,795 p=21408 u=root | GATHERING FACTS ***************************************************************
2017-06-06 15:45:24,231 p=21408 u=root | failed: [172.17.0.4] => {"failed": true, "parsed": false}
2017-06-06 15:45:24,232 p=21408 u=root | /bin/sh: sudo: command not found
OpenSSH_6.6.1, OpenSSL 1.0.1e-fips 11 Feb 2013
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 56: Applying options for *
debug1: auto-mux: Trying existing master
debug1: mux_client_request_session: master session id: 2
Shared connection to 172.17.0.4 closed.
2017-06-06 15:45:24,235 p=21408 u=root | failed: [172.17.0.3] => {"failed": true, "parsed": false}
2017-06-06 15:45:24,235 p=21408 u=root | /bin/sh: sudo: command not found
OpenSSH_6.6.1, OpenSSL 1.0.1e-fips 11 Feb 2013
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 56: Applying options for *
debug1: auto-mux: Trying existing master
debug1: mux_client_request_session: master session id: 2
Shared connection to 172.17.0.3 closed.
2017-06-06 15:45:24,240 p=21408 u=root | failed: [172.17.0.2] => {"failed": true, "parsed": false}
2017-06-06 15:45:24,240 p=21408 u=root | /bin/sh: sudo: command not found
OpenSSH_6.6.1, OpenSSL 1.0.1e-fips 11 Feb 2013
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 56: Applying options for *
debug1: auto-mux: Trying existing master
debug1: mux_client_request_session: master session id: 2
Shared connection to 172.17.0.2 closed.
2017-06-06 15:45:24,249 p=21408 u=root | TASK: [install] ***************************************************************
2017-06-06 15:45:24,255 p=21408 u=root | FATAL: no hosts matched or all hosts have already failed -- aborting
2017-06-06 15:45:24,255 p=21408 u=root | PLAY RECAP ********************************************************************
2017-06-06 15:45:24,256 p=21408 u=root | to retry, use: --limit @/root/httpd.retry
2017-06-06 15:45:24,256 p=21408 u=root | 172.17.0.2 : ok=0 changed=0 unreachable=0 failed=1
2017-06-06 15:45:24,256 p=21408 u=root | 172.17.0.3 : ok=0 changed=0 unreachable=0 failed=1
2017-06-06 15:45:24,256 p=21408 u=root | 172.17.0.4 : ok=0 changed=0 unreachable=0 failed=1
2017-06-06 15:45:53,254 p=21584 u=root |
2017-06-06 15:45:53,254 p=21584 u=root | /usr/bin/ansible-playbook ./playbooks/httpd.yml
2017-06-06 15:45:53,254 p=21584 u=root |
2017-06-06 15:45:53,269 p=21584 u=root | PLAY [install httpd from yum] *************************************************
2017-06-06 15:45:53,270 p=21584 u=root | GATHERING FACTS ***************************************************************
2017-06-06 15:45:55,102 p=21584 u=root | ok: [172.17.0.2]
2017-06-06 15:45:55,107 p=21584 u=root | ok: [172.17.0.4]
2017-06-06 15:45:55,134 p=21584 u=root | ok: [172.17.0.3]
2017-06-06 15:45:55,144 p=21584 u=root | TASK: [install] ***************************************************************
由于网络问题这里我等待的时间太长了,所以直接中断了 🙁