2020-09-04

Linux高级运维-事实、变量、循环

1.事实指的是受控主机自动检测到的变量
[root@afei weixin]# ansible webservers -i inventory -m setup |less
192.168.240.134 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.240.134",
            "192.168.122.1"
        ],
        "ansible_all_ipv6_addresses": [
            "fe80::7c16:db91:85ec:a36c"
        ],
        "ansible_apparmor": {
            "status": "disabled"
        },
        "ansible_architecture": "x86_64",
        "ansible_bios_date": "07/29/2019",
        "ansible_bios_version": "6.00",

2.用变量取出所有事实facts
[root@afei weixin]# vim playbook.yml
---
- hosts: all
  tasks:
    - name: make facts
      debug:
        var: ansible_facts
[root@afei weixin]# ansible-playbook -C playbook.yml 
          },
            "hw_timestamp_filters": [],
            "macaddress": "52:54:00:d5:ed:f8",
            "mtu": 1500,
            "promisc": true,
            "timestamping": [
                "tx_software",
                "rx_software",
                "software"
            ],
            "type": "ether"
        },
        "virtualization_role": "guest",
        "virtualization_type": "VMware"
    }
}

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

3.用变量取出事实facts中的ip部分
[root@afei weixin]# vim playbook.yml
---
- hosts: all
  tasks:
    - name: make facts
      debug:
        var: ansible_facts['default_ipv4']['address']
~    
[root@afei weixin]# ansible-playbook -C playbook.yml 

PLAY [all] ****************************************************************************

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

TASK [make facts] *********************************************************************
ok: [192.168.240.134] => {
    "ansible_facts['default_ipv4']['address']": "192.168.240.134"
}

PLAY RECAP ****************************************************************************
192.168.240.134            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
                                                       
4.关闭事实收集
[root@afei weixin]# vim playbook.yml
---
- hosts: all
  gather_facts: no
  tasks:
    - name: make facts
      debug:
        var: ansible_facts['default_ipv4']['address']
[root@afei weixin]# ansible-playbook  playbook.yml 

PLAY [all] ****************************************************************************

TASK [make facts] *********************************************************************
ok: [192.168.240.134] => {
    "ansible_facts['default_ipv4']['address']": "VARIABLE IS NOT DEFINED!"
}

PLAY RECAP ****************************************************************************
192.168.240.134            : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

        

5.即使关闭了,也可以用setup临时收集事实
[root@afei weixin]# vim playbook.yml
---
- hosts: all
  gather_facts: no
  tasks:
    - name: feige
      setup:
    - name: make facts
      debug:
        var: ansible_facts['default_ipv4']['address']
[root@afei weixin]# ansible-playbook -C playbook.yml 

PLAY [all] ****************************************************************************

TASK [feige] **************************************************************************
ok: [192.168.240.134]

TASK [make facts] *********************************************************************
ok: [192.168.240.134] => {
    "ansible_facts['default_ipv4']['address']": "192.168.240.134"
}

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

6.创建自定义事实
[root@localhost ~]# mkdir -p /etc/ansible/facts.d
[root@localhost ~]# cd /etc/ansible/facts.d/
[root@localhost facts.d]# vim feige.facts.d
[packages]
web_package = httpd
db_package = mariadb-server

[users]
user1 = joe
user2 = jane
[root@localhost ~]# ansible all -m setup|less

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值