ansible学习之 Facts

Facts 是什么:

在Ansible中,Facts是关于目标主机的信息,这些信息由Ansible在执行任务之前收集。Facts是特定于主机的,它们提供了系统级的数据,例如操作系统类型、内核版本、IP地址、已安装的包列表等。这些信息可以用来在playbook中做出决策,例如,使用条件语句或变量来应用不同的配置,这些变量可以在任务和模板中使用。

如何手动获取 远程系统的数据

通常facts中的数据是由Ansible中的 'setup'模块自动发现的,我们可以调用setup模块来获取远程主机的信息。

bash 复制代码
# 192.168.0.187 远程主机名或者组
# ansible 192.168.0.187 -m setup
.....	# 信息巨多这里就省略掉了

如何关闭Facts

收集信息会减慢 ansible 或者 ansible-playbook 的执行速度,如果你不需要用到 facts,你可以把它关闭掉。

1. 使用ad-hoc时关闭facts:

bash 复制代码
# ansible all -m ping  -e "gather_facts=False"  
  • -e : 传递参数给 模块

2. 在使用ansible-playbook 时关闭facts

bash 复制代码
---            
- hosts: all
  gather_facts: no
  tasks:
    - name: test connect
      ping:
  • gather_facts: no : 全局生效

3. 配置文件中,关闭facts信息收集

bash 复制代码
# cat /etc/ansible/ansible.cfg
gathering = False

使用ansible-playbook 调用facts中的数据

bash 复制代码
- hosts: all
  tasks:
    - name: Print the hostname of the remote system
      debug:
        var: ansible_nodename
bash 复制代码
[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参数

bash 复制代码
- hosts: all
  tasks:
    - name:  Debug task
      debug:
        msg: "This is a debug message"
bash 复制代码
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:用于输出一条消息。

输出消息时解释变量

bash 复制代码
- hosts: all
  tasks:
    - name:  print remote hostname
      debug:
        msg: "Hostname is {{ansible_nodename}}"
bash 复制代码
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 事务中的,所有事物一定要打开,也可以换一种方式调用变量如下:
bash 复制代码
- hosts: all
  tasks:
    - name:  print remote hostname
      debug:
        msg: "The hostname of the target system is {{ ansible_facts['hostname'] }}"

案例二、打印变量使用 var 参数

变量同样来自事务,所有事务需要打开

bash 复制代码
- hosts: all
  tasks:
    - name:  print remote hostname
      debug:
        var:  ansible_facts['hostname']

或者

bash 复制代码
- hosts: all
  tasks:
    - name:  print remote hostname
      debug:
        var: ansible_nodename 
  • var参数是直接打印变量的值,不需要引用

案例三、打印复杂变量数据

截取一段facts的数据:

bash 复制代码
     "ansible_facts": {
        "ansible_default_ipv4": {
            "address": "192.168.0.187", 
            "alias": "ens32", 
            "broadcast": "192.168.0.255", 
            "gateway": "192.168.0.2", 
            "interface": "ens32", 
            "macaddress": "00:0c:29:77:50:d9", 
            "mtu": 1500, 
            "netmask": "255.255.255.0", 
            "network": "192.168.0.0", 
            "type": "ether"
        }, 
      }

将address字符的数据打印出来:

方法一、使用msg参数

bash 复制代码
- hosts: all
  tasks:
    - name:  print remote hostname
      debug:
        msg: "{{ansible_default_ipv4.address}}"

或者

bash 复制代码
- hosts: all
  tasks:
    - name:  print remote hostname
      debug:
        msg: "{{ansible_default_ipv4['address']}}"

方法二、使用var参数

bash 复制代码
- hosts: all
  tasks:
    - name:  print remote hostname
      debug:
        var: ansible_default_ipv4.address

或者

bash 复制代码
- hosts: all
  tasks:
    - name:  print remote hostname
      debug:
        var: ansible_default_ipv4['address']
相关推荐
张3231 小时前
配置加载混淆(vars_files与from_yaml)
ansible
张3232 小时前
角色与内容集合:自动化配置的标准化复用机制
运维·自动化·ansible
张3231 天前
Ansible实施任务控制
linux·ansible
张3231 天前
Ansible Playbook
ansible
张3231 天前
Ansible文件部署
服务器·ansible
张3231 天前
Ansible介绍
ansible
热爱Liunx的丘丘人2 天前
Ansible的Playbook案例一
linux·运维·服务器·ansible
小梦爱安全2 天前
Ansible剧本1
java·网络·ansible
WJ.Polar2 天前
Ansible任务控制
linux·运维·网络·python·ansible
热爱Liunx的丘丘人2 天前
PlayBook常用的模块编写
linux·服务器·ansible