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']
相关推荐
leo__5202 天前
自动化运维:使用Ansible简化日常任务
运维·自动化·ansible
风清再凯7 天前
自动化工具ansible,以及playbook剧本
运维·自动化·ansible
IT乌鸦坐飞机7 天前
ansible部署数据库服务随机启动并创建用户和设置用户有完全权限
数据库·ansible·centos7
遇见火星20 天前
如何使用Ansible一键部署MinIO集群?
ansible
粥周粥20 天前
ANSIBLE
ansible
码农101号20 天前
Linux中ansible模块补充和playbook讲解
linux·运维·ansible
码农101号20 天前
Linux的Ansible软件基础使用讲解和ssh远程连接
ansible
烟雨书信21 天前
ANSIBLE运维自动化管理端部署
运维·自动化·ansible
碎碎-li21 天前
ANSIBLE(运维自动化)
运维·自动化·ansible
@donshu@24 天前
Linux运维-ansible-python开发-获取inventroy信息
linux·运维·ansible