ansible-paybook是运行Playbook文件的命令,用于在目标主机上(由Playbook中的hosts
指定)执行Playbook中定义的任务(由Playbook中的tasks
定义)。
一般用法为:
ansible-playbook [OPTIONS]
用于检查指定的Playbook文件是否有语法错误(可以简写为--syntax
)
--syntax-check
perform a syntax check on the playbook, but do not execute it
示例:
#无语法错误
[root@ansible ~]# ansible-playbook --syntax-check debug.yml
playbook: debug.yml
#故意改错(将name:后的空格删除)
[root@ansible ~]# vim debug.yml
---
- hosts: 192.168.1.*
tasks:
- name:debug module test
debug:
msg: IpAddr of Host {{ ansible_nodename }} is {{ ansible_default_ipv4.address }}
...
#再次检查
[root@ansible ~]# ansible-playbook --syntax debug.yml
加上-C
选项后,不做任何实际的操作,只是将预测的任务执行过程打印出来,检查可能出现的错误。
-C, --check
don't make any changes; instead, try to predict some of the changes that may occur
示例:
#编写一个删除fstab文件的playbook
[root@ansible ~]# vim file_absent.yml
---
- hosts: websrvs
remote_user: root
tasks:
- name: remove fstab
file: name=/etc/fstab state=absent
...
#模拟执行
[root@ansible ~]# ansible-playbook -C file_absent.yml
#目标主机上查看,fstab文件还存在
root@node111:~# ll /etc/fstab
-rw-r--r-- 1 root root 550 Nov 26 10:51 /etc/fstab
列出Playbook中hosts
指定的HOST_PATTER
匹配到的主机。
--list-hosts
outputs a list of matching hosts; does not execute anything else
示例(列出debug.yml中所涉及的目标主机):
#playbook内容
[root@ansible ~]# cat debug.yml
---
- hosts: websrvs:testsrvs
tasks:
- name: debug module test
debug:
msg: IpAddr of Host {{ ansible_nodename }} is {{ ansible_default_ipv4.address }}
...
#列出Playbook匹配到的主机
[root@ansible ~]# ansible-playbook --list-hosts debug.yml
playbook: debug.yml
play #1 (websrvs:testsrvs): websrvs:testsrvs TAGS: []
pattern: [u'websrvs:testsrvs']
hosts (2):
192.168.1.113
192.168.1.111
列出Playbook中包含的任务标签。
--list-tags
list all available tags
示例:
#Playbook内容
[root@ansible ~]# cat httpd.yml
---
- hosts: websrvs
remote_user: root
gather_facts: no
tasks:
- name: install httpd
yum: name=httpd
tags: install
- name: start httpd
service: name=httpd state=started enabled=yes
tags: start
#列出标签:
[root@ansible ~]# ansible-playbook --list-tags httpd.yml
playbook: httpd.yml
play #1 (websrvs): websrvs TAGS: []
TASK TAGS: [install, start]
列出Playbook中所有将被执行的任务(注意是将被执行的任务,不是所有任务)。
--list-tasks
list all tasks that would be executed
示例:
#Playbook内容(index file这个任务被打了never标签,意味着不会被执行)
[root@ansible ~]# vim httpd.yml
---
- hosts: websrvs
remote_user: root
gather_facts: no
tasks:
- name: install httpd
yum: name=httpd
tags: install
- name: start httpd
service: name=httpd state=started enabled=yes
tags: start
- name: index file
copy: src=index.html dest=/var/www/html/index.html owner=www group=www
tags: index, never
#列出将被执行的任务(不包括never标签的任务)
[root@ansible ~]# ansible-playbook --list-tasks httpd.yml
playbook: httpd.yml
play #1 (websrvs): websrvs TAGS: []
tasks:
install httpd TAGS: [install]
start httpd TAGS: [start]
使用-l
或--limit
指定额外的限制条件,对Playbook中hosts
匹配到的主机列表进行二次匹配。
-l 'SUBSET', --limit 'SUBSET'
further limit selected hosts to an additional pattern
示例:
#Playbook匹配的主机为websrvs组和testsrvs组的并集:
[root@ansible ~]# ansible-playbook --list-hosts debug.yml
#执行Playbook时,添加额外的匹配条件'web*',对匹配到的主机列表进行二次匹配,就只能匹配到websrvs组中的主机了
[root@ansible ~]# ansible-playbook -l 'web*' -C debug.yml
用于指定标签,来仅运行Playbook中特定的PlAY或TASK。
-t, --tags
only run plays and tasks tagged with these values
示例:
#Playbook内容
[root@ansible ~]# cat apache2.yml
---
- hosts: websrvs
remote_user: root
gather_facts: no
tasks:
- name: install apache2
apt: name=apache2
tags: install
- name: start apache2
service: name=apache2 state=started enabled=yes
tags: start
- name: index file
copy: src=index.html dest=/var/www/html/index.html owner=www group=www
tags: index
...
#仅运行打了start标签的任务
[root@ansible ~]# ansible-playbook -t start apache2.yml
通常ansible-playbook
命令会从相应的ansible.cfg
配置文件中inventory
配置项指定的路径查找主机清单文件,使用-i
选项可以指定本次运行时使用的主机清单文件。
-i, --inventory, --inventory-file
specify inventory host path or comma separated host list. --inventory-file is deprecated
示例:
#Playbook内容(hosts指定的是websrvs组)
[root@ansible ~]# cat debug.yml
---
- hosts: websrvs
tasks:
- name: debug module test
debug:
msg: IpAddr of Host {{ ansible_nodename }} is {{ ansible_default_ipv4.address }}
...
#默认的主机清单文件内容(websrvs组中的主机是192.168.1.111):
[root@ansible ~]# cat /etc/ansible/hosts
[websrvs]
192.168.1.111
#查看Playbook匹配到的目标主机(匹配到的是192.168.1.111)
[root@ansible ~]# ansible-playbook --list-hosts debug.yml
playbook: debug.yml
play #1 (websrvs): websrvs TAGS: []
pattern: [u'websrvs']
hosts (1):
192.168.1.111
#自定义的主机清单文件(websrvs组中的主机是192.168.1.222):
[root@ansible ~]# cat /tmp/hosts
[websrvs]
192.168.1.222
#指定自定义的主机清单文件,查看Playbook匹配到的目标主机(匹配到的是192.168.1.222):
[root@ansible ~]# ansible-playbook -i /tmp/hosts --list-hosts debug.yml
playbook: debug.yml
play #1 (websrvs): websrvs TAGS: []
pattern: [u'websrvs']
hosts (1):
192.168.1.222
默认执行Playbook中全部的可执行TASK,使用--start-at-task
可以从指定的TASK开始执行。
注意这里指定的是TASK的任务名称,而非标签。
--start-at-task 'START_AT_TASK'
start the playbook at the task matching this name
示例:
#Playbook内容
[root@ansible ~]# cat apache2.yml
---
- hosts: websrvs
remote_user: root
gather_facts: no
tasks:
- name: install apache2
apt: name=apache2
tags: install
- name: start apache2
service: name=apache2 state=started enabled=yes
tags: start
- name: index file
copy: src=index.html dest=/var/www/html/index.html owner=www group=www
tags: index
...
#从start apache2这个任务开始往后执行(即只执行start apache2和index file两个任务)
[root@ansible ~]# ansible-playbook --start-at-task 'start apache2' apache2.yml
用于在命令行中为Playbook定义变量,或指定变量文件(YAML/JSON,指定文件时用@作为前缀)。
-e, --extra-vars
set additional variables as key=value or YAML/JSON, if filename prepend with @
Playbook 示例:
[root@ansible ~]# vim remove.yml
---
- hosts: websrvs
tasks:
- name: remove {{ rmfile_name }}
file: path={{ rmfile_name }} state=absent
...
在命令行中定义变量并执行Playbook:
[root@ansible ~]# ansible-playbook -e rmfile_name=/etc/fstab -C remove.yml
可以将多个变量定义在一个变量文件中:
[root@ansible ~]# vim vars.yml
src_file: files/ports.conf
dest_file: /etc/apache2/ports.conf
Playbook 示例:
[root@ansible ~]# vim copy.yml
---
- hosts: websrvs
tasks:
- name: copy {{ src_file }} to {{ dest_file }} on {{ ansible_nodename }}
copy:
src: "{{ src_file }}"
dest: "{{ dest_file }}"
...
在命令行中指定变量文件并执行Playbook:
[root@ansible ~]# ansible-playbook -e '@vars.yml' -C copy.yml