1. 程式人生 > >Ansible Playbook 使用條件判斷語句

Ansible Playbook 使用條件判斷語句

先介紹一下 gather_facts 引數,該引數用於指定在執行任務前,是否先執行 setup 模組獲取主機相關資訊,以便給後面的任務使用

[[email protected] ~]# ansible 192.168.119.134 -m setup    # 檢視主機的facter資訊
192.168.119.134 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.119.134"
......

條件判斷語句寫法:

[[email protected] ~]$ 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 命令