linux学习

Ansible 变量

fasts 变量

facts变量是Ansible自带的预定义变量,用于描述被控端软硬件信息,如系统版本、内核版本、网络接口、磁盘空间等。facts变量通过setup模块获得。

[root@pubserver ansible]# ansible web1 -m setup         # 通过setup模块查看所有facts变量
web1 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.88.11"
        ],
        "ansible_all_ipv6_addresses": [
            "fe80::20c:29ff:feed:ccb6"
        ],
 ..................

facts变量是一个由{ }构成的键值对字典,在{ }中有很多层级的嵌套,可以通过参数过滤出第一个层级的内容。

查看所有的IPv4地址,代码示意如下:

[root@pubserver ansible]# ansible webservers -m setup -a "filter=ansible_all_ipv4_addresses"
web2 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.88.12"
        ],
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
}
web1 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.88.11"
        ],
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
}

查看可用内存,代码示意如下:

[root@pubserver ansible]# ansible webservers -m setup -a "filter=ansible_memfree_mb"
web2 | SUCCESS => {
    "ansible_facts": {
        "ansible_memfree_mb": 142,
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
}
web1 | SUCCESS => {
    "ansible_facts": {
        "ansible_memfree_mb": 136,
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
}

常用的facts 变量

Ansible使用facts变量获取被控端主机的硬件信息,常用的facts变量有:

ansible_all_ipv4_addresses:所有的IPV4地址

ansible_bios_version:BIOS版本信息

ansible_memtotal_mb:总内存大小

ansible_hostname:主机名

debug变量

Ansible使用debug模块输出信息,常用的参数是msg,用于输出指定内容。

显示远程主机的主机名和内存大小,代码示意如下:

[root@pubserver ansible]# vim debug.yml
---
- name: display host info
  hosts: webservers
  tasks:
    - name: display hostname and memory
      debug:
        msg: "hostname:{{ansible_hostname}}; mem:{{ansible_memtotal_mb}}MB"

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

PLAY [display host info] **************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************
ok: [web2]
ok: [web1]

TASK [display hostname and memory] ****************************************************************************************************************
ok: [web1] => {
    "msg": "hostname:web1; mem:777MB"
}
ok: [web2] => {
    "msg": "hostname:web2; mem:777MB"
}

PLAY RECAP ****************************************************************************************************************************************
web1                       : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
web2                       : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

留言

您的电子邮箱地址不会被公开。 必填项已用 * 标注