学习linux的第七十四天
playbook的条件判断 查看到setup收集到的所有的facter信息: ansible testhost -m setup 编辑条件: [root@KXLZQ ~]# vim /etc/ansible/when.yml hosts: testhost user: root gather_facts: True tasks: - name: use when shell: touch /tmp/when.txt when: ansible_ens33.ipv4.address == “192.168.70.129” 注: when: ansible_ens33是一个数组存储着网卡相关信息,ipv4属于该数组的子元素,但是ipv4也是一个数组,而address则是ipv4数组的子元素。我们需要使用address 来作为判断条件。所以要访问address就需要使用这样的格式: when: ansible_ens33.ipv4.address,address表示的是键,而"192.168.70.129"则是值,when为判断语句相当于if,所以其判断条件为:该键的值为"192.168.70.129"时就执行shell模块里定义的语句。 执行: [root@KXLZQ ~]# ansible-playbook /etc/ansible/when.yml PLAY [testhost]