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: all
  gather_facts: no
  tasks:
    - name: test connect
      ping:
  • gather_facts: no : 全局生效

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

# cat /etc/ansible/ansible.cfg
gathering = False

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

- hosts: all
  tasks:
    - name: Print the hostname of the remote system
      debug:
        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: all
  tasks:
    - name:  Debug task
      debug:
        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: all
  tasks:
    - name:  print remote hostname
      debug:
        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: all
  tasks:
    - name:  print remote hostname
      debug:
        msg: "The hostname of the target system is {{ ansible_facts['hostname'] }}"

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

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

- hosts: all
  tasks:
    - name:  print remote hostname
      debug:
        var:  ansible_facts['hostname']

或者

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

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

截取一段facts的数据:

     "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参数

- hosts: all
  tasks:
    - name:  print remote hostname
      debug:
        msg: "{{ansible_default_ipv4.address}}"

或者

- hosts: all
  tasks:
    - name:  print remote hostname
      debug:
        msg: "{{ansible_default_ipv4['address']}}"

方法二、使用var参数

- hosts: all
  tasks:
    - name:  print remote hostname
      debug:
        var: ansible_default_ipv4.address

或者

- hosts: all
  tasks:
    - name:  print remote hostname
      debug:
        var: ansible_default_ipv4['address']
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值