先介紹一下 gather_facts 參數,該參數用於指定在執行任務前,是否先執行 setup 模塊獲取主機相關信息,以便給后面的任務使用
[root@localhost ~]# ansible 192.168.119.134 -m setup # 查看主機的facter信息 192.168.119.134 | SUCCESS => { "ansible_facts": { "ansible_all_ipv4_addresses": [ "192.168.119.134"
......
條件判斷語句寫法:
[root@localhost ~]$ cat when.yml --- - hosts: 192.168.119.134 user: root gather_facts: True # 必須先開啟這個 tasks: - name: use when shell: touch /tmp/when.txt when: ansible_date_time.year == "2019" # 表示當 ansible_date_time.year 的值等於 2019 時,就使用 shell 模塊執行 touch /tmp/when.txt 命令