ansible学习之 Facts
Facts 是什么:
在Ansible中,Facts是关于目标主机的信息,这些信息由Ansible在执行任务之前收集。Facts是特定于主机的,它们提供了系统级的数据,例如操作系统类型、内核版本、IP地址、已安装的包列表等。这些信息可以用来在playbook中做出决策,例如,使用条件语句或变量来应用不同的配置,这些变量可以在任务和模板中使用。
如何手动获取 远程系统的数据
通常facts中的数据是由Ansible中的 ‘setup’模块自动发现的,我们可以调用setup模块来获取远程主机的信息。
# 192.168.0.187 远程主机名或者组
# ansible 192.168.0.187 -m setup
.....	# 信息巨多这里就省略掉了
 
如何关闭Facts
收集信息会减慢 ansible 或者 ansible-playbook 的执行速度,如果你不需要用到 facts,你可以把它关闭掉。
1. 使用ad-hoc时关闭facts:
# ansible all -m ping  -e "gather_facts=False"  
 
- -e : 传递参数给 模块
 
2. 在使用ansible-playbook 时关闭facts
---            
- hosts: allgather_facts: notasks:- name: test connectping:
 
- gather_facts: no : 全局生效
 
3. 配置文件中,关闭facts信息收集
# cat /etc/ansible/ansible.cfg
gathering = False
 
使用ansible-playbook 调用facts中的数据
- hosts: alltasks:- name: Print the hostname of the remote systemdebug:var: ansible_nodename
 
[root@localhost ~]# ansible-playbook ping-playbook.yaml PLAY [all] **************************************************************************************************************************************************TASK [Gathering Facts] **************************************************************************************************************************************
ok: [192.168.0.187]TASK [Print the hostname of the remote system] **************************************************************************************************************
ok: [192.168.0.187] => {"ansible_nodename": "idealpxe"
}PLAY RECAP **************************************************************************************************************************************************
192.168.0.187              : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
 
- var:ansible_nodename 打印变量的值
 - var: ansible_facts 打印所有事务
 
扩展 debug模块介绍
Ansible中的debug模块是一个用于输出变量值或执行其他调试任务的非常有用的模块。它通常用于以下目的:
- 打印变量值:在执行过程中,如果你想查看某个变量的值,可以使用debug模块打印出来。
 - 调试任务:如果你需要调试任务,debug模块可以用来打印任务执行过程中的中间状态或结果。
 - 消息通知:debug模块可以用来向用户显示消息或通知。
 - 条件调试:结合when条件,debug模块可以只在满足特定条件时执行.
 - 测试和开发:在开发和测试Ansible playbook时,debug模块可以帮助你理解代码的执行流程。
 
案例一、打印消息,使用msg参数
- hosts: alltasks:- name:  Debug taskdebug:msg: "This is a debug message"
 
PLAY [all] **************************************************************************************************************************************************TASK [Gathering Facts] **************************************************************************************************************************************
ok: [192.168.0.187]TASK [Debug task] *******************************************************************************************************************************************
ok: [192.168.0.187] => {"msg": "This is a debug message"
}PLAY RECAP **************************************************************************************************************************************************
192.168.0.187              : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
 
- msg:用于输出一条消息。
 
输出消息时解释变量
- hosts: alltasks:- name:  print remote hostnamedebug:msg: "Hostname is {{ansible_nodename}}"
 
PLAY [all] **************************************************************************************************************************************************TASK [Gathering Facts] **************************************************************************************************************************************
ok: [192.168.0.187]TASK [print remote hostname] ********************************************************************************************************************************
ok: [192.168.0.187] => {"msg": "Hostname is idealpxe"
}PLAY RECAP **************************************************************************************************************************************************
192.168.0.187              : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
 
- 在消息中调用变量需要使用 “{{}}”,双引号也不能少。ansible_nodename 变量是调用facts 事务中的,所有事物一定要打开,也可以换一种方式调用变量如下:
 
- hosts: alltasks:- name:  print remote hostnamedebug:msg: "The hostname of the target system is {{ ansible_facts['hostname'] }}"
 
案例二、打印变量使用 var 参数
变量同样来自事务,所有事务需要打开
- hosts: alltasks:- name:  print remote hostnamedebug:var:  ansible_facts['hostname']
 
或者
- hosts: alltasks:- name:  print remote hostnamedebug:var: ansible_nodename 
 
- var参数是直接打印变量的值,不需要引用
 
