set up 模块
返回很多远程主机的信息
ansible testB -m setup
使用通配拿到一些基本信息
ansible testB -m setup -a "filter=*mb*"
还可以从远程主机拿到一些自定义信息
在远程主机创建一份信息
- 注意: 文件路径 和 最后的文件必须以.fact结尾
mkdir /etc/ansible
cd /etc/ansible
mkdir facts.d
cd facts.d
vim information.fact
[message]
msg1=The first message
msg2=The second message
这些信息被称为local facts
debug模块
测试
1.输出自定义信息
---
- hosts: testB
remote_user: root
tasks:
- name: touch file
file: path=/testdir/file
state=touch
- name: debug demo
debug:
msg: This is debug info,The test file has been touched
将信息展示在控制台
2.输出自定义变量
---
- hosts: testB
remote_user: root
vars:
the_var: the key
tasks:
- name: debug demo
debug:
var: the_var
3.输出自定义信息+变量
如果在调用vars时有特殊符号 那就整句加上引号
---
- hosts: testB
remote_user: root
vars:
the_var: value
tasks:
- name: debug demo
debug:
msg: "The infomation in : {{the_var}}"
4.在playbook中引用远程主机的信息,直接将关键字当作变量
---
- hosts: testB
remote_user: root
tasks:
- name: debug demo
debug:
msg: "Remote host memory information: {{ansible_memory_mb}}"
只想获取real部分的信息
---
- hosts: testB
remote_user: root
tasks:
- name: debug demo
debug:
msg: "Remote host memory information: {{ansible_memory_mb.real}}"
---
- hosts: testB
remote_user: root
tasks:
- name: debug demo
debug:
msg: "Remote host memory information: {{ansible_memory_mb['real']}}"