8.ansible之特殊模块

1.Setup模块

  • ansible_facts用于采集被管理设备的系统信息
  • 所有收集的信息都被保存在变量中
  • 每次执行playbook默认第一个任务就是Gathering Facts
  • 使用setup模块可以查看收集到的facts信息
[root@db04 ansible]# ansible db01  -m setup
db01 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.10.147"
        ],
        省略......

由于setup模块打印出来的信息比较多,我们可以通过参数filter进行过滤,比如说过滤内存信息

[root@db04 ansible]# ansible db01  -m setup -a "filter=ansible_mem*"
db01 | SUCCESS => {
    "ansible_facts": {
        "ansible_memfree_mb": 717, 
        "ansible_memory_mb": {
            "nocache": {
                "free": 805, 
                "used": 167
            }, 
            "real": {
                "free": 717, 
                "total": 972, 
                "used": 255
            }, 
            "swap": {
                "cached": 0, 
                "free": 2047, 
                "total": 2047, 
                "used": 0
            }
        }, 
        "ansible_memtotal_mb": 972, 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}

2.debug模块

  • debug模块可以显示变量的值,可以辅助排错
  • debug模块有两个参数,var和msg(引用变量需要{{}})
[root@db04 ansible]# cat debug.yml 
---
- hosts: db01
  tasks:
      - debug:
          var:  ansible_all_ipv4.addresses  
      - debug:
          msg:  "主机名称是:{{ansible_hostname}}"
      - debug:
          var: ansible_devices.sda.partitions.sda1.size
      - debug:
          msg:  "总内存大小:{{ansible_memtotal_mb}}"

结果显示:

[root@db04 ansible]# ansible-playbook debug.yml 

PLAY [db01] *********************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************
ok: [db01]

TASK [debug] ********************************************************************************************************************************************
ok: [db01] => {
    "ansible_all_ipv4.addresses": "VARIABLE IS NOT DEFINED!"
}

TASK [debug] ********************************************************************************************************************************************
ok: [db01] => {
    "msg": "主机名称是:db01"
}

TASK [debug] ********************************************************************************************************************************************
ok: [db01] => {
    "ansible_devices.sda.partitions.sda1.size": "200.00 MB"
}

TASK [debug] ********************************************************************************************************************************************
ok: [db01] => {
    "msg": "总内存大小:972"
}

PLAY RECAP **********************************************************************************************************************************************
db01                       : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

总结:

  1. 利用setup模块获取主机信息

  2.通过debug模块显示变量信息

posted on 2023-05-16 23:29  太白金星有点烦  阅读(49)  评论(0)    收藏  举报

导航