当前位置: 首页 > news >正文

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参数是直接打印变量的值,不需要引用

http://www.mrgr.cn/news/39705.html

相关文章:

  • PC流量转移动流量,提升网盘拉新收益
  • Study Plan For Algorithms - Part44
  • 在起草行业标准时,如何确保格式统一?
  • 网站建设方案书怎么写
  • VUE前后端分离毕业设计题目项目有哪些,VUE程序开发常见毕业论文设计推荐
  • 银河麒麟服务器:更新软件源
  • 如何查看NVIDIA Container Toolkit是否配置成功
  • Python中的Threading模块:让并发编程变得轻松
  • 数据转换新利器,开启企业高效之路
  • WebSocket 2024/9/30
  • 如何使用WinRAR锁定压缩文件,防止文件被修改或删除?
  • 详解Java之继承与多态
  • 为别人做嫁衣——代理模式
  • 101条团队管理秘籍
  • 算法-数组笔记
  • 诗画紫砂壶
  • 跨颜色人员重识别
  • Java 中 synchronized 和 Thread 的使用场合介绍
  • 【MYSQL】mysql约束---自增长约束(auto_increment)
  • markdown 中启用音频支持