默认会查询每台服务器的facts值
---
- name: a play example
hosts: all
remote_user: root
tasks:
- name: install nginx package
yum: name=nginx state=present
- name: copy nginx.conf to remote server
copy: src="/a2020/img/data-img.jpg" data-src=nginx.conf dest=/etc/nginx/nginx.conf
- name: start nginx server
service:
name: nginx
enabled: true
state: started
...
[root@bogon ~]# ansible-playbook facts.yaml
PLAY [a play example] *****************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ****************************************************************************************************************************************************************************************************************
ok: [192.168.216.134]
ok: [192.168.216.132]
TASK [install nginx package] **********************************************************************************************************************************************************************************************************
ok: [192.168.216.132]
若在整个PlayBook 的执行过程中,完全未使用过 Facts 变量,此时我们可以将其关闭,以加快PlayBook的执行速度
[root@bogon ~]# cat factsout.yaml
---
- name: a play example
hosts: webservers
# 关闭 facts 变量收集功能
gather_facts: no
remote_user: root
tasks:
- name: install nginx package
yum: name=nginx state=present
- name: copy nginx.conf to remote server
copy: src="/a2020/img/data-img.jpg" data-src=nginx.conf dest=/etc/nginx/nginx.conf
- name: start nginx server
service:
name: nginx
enabled: true
state: started
...
[root@bogon ~]# ansible-playbook factsout.yaml
[WARNING]: Could not match supplied host pattern, ignoring: webservers
PLAY [a play example] *****************************************************************************************************************************************************************************************************************
skipping: no hosts matched
PLAY RECAP ****************************************************************************************************************************************************************************************************************************
注册变量
通常用于保存一个task任务的执行结果,便于debug使用
或者将此次的task任务结果作为下一task任务的条件
注册变量在playbook中通过register关键字去实现
[root@bogon ~]# cat zuce.yaml
---
- name: install a package and print the result
hosts: webservers
remote_user: root
tasks:
- name: install nginx package
yum: name=nginx state=present
register: install_result #变量名称
- name: print result
debug: var=install_result #输出结果
...
[root@bogon ~]# ansible-playbook zuce.yaml
[WARNING]: Could not match supplied host pattern, ignoring: webservers
PLAY [install a package and print the result] *****************************************************************************************************************************************************************************************
skipping: no hosts matched
PLAY RECAP ****************************************************************************************************************************************************************************************************************************
变量的优先权
变量这一篇加上,上一篇变量
一大堆
有全局变量,剧本,资产,Facts变量,注册变量
其中facts变量不需要人为声明赋值,注册变量只需要register去声明,而不需要赋值
全局变量,剧本及资产则完全需要人为的声明赋值
*当一个变量同时在全局变量、剧本变量和资产变量中定义时,优先级最高的是全局变量;其次是剧本变量;最后才是资产变量。