如果仅定义了变量,如何运行任务

时间:2019-01-21 15:33:16

标签: ansible ansible-2.x ansible-template

我有以下任务:

-name : task name
 cron:
     ...
     ...
     ...     
 when:  "{{ vars[cname].cron }}" is defined

仅当定义了主机server1的变量server1.cron和主机server2等的server2.cron时,我才想运行任务。但是当我只允许vars [cname]时,不喜欢语法.cron与ou“”不会用server1.cron或server2.cron代替vars [cname] .cron,它会将其解释为不存在的“ vars [cname] .cron”变量。 你遇到这个问题了吗? 在此先感谢^^!

2 个答案:

答案 0 :(得分:0)

尝试一下:

---

- hosts: all
  gather_facts: False

  vars:
    my_vars:
      cname1:
        cron: "val1"
      cname2:
        cron2: "val2"

  tasks:
    - name: task name 1
      debug:
        msg: "test"
      when:  my_vars['cname1']['cron'] is defined

    - name: task name 2
      debug:
        msg: "test"
      when:  my_vars['cname2']['cron'] is defined

输出为

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

TASK [task name 1] *******************************************************************************************************************************************
ok: [host] => {
    "msg": "test"
}

TASK [task name 2] *******************************************************************************************************************************************
skipping: [host]

PLAY RECAP ***************************************************************************************************************************************************
host                    : ok=1    changed=0    unreachable=0    failed=0

或另一种方法:

库存

[all]
staging cname=server1_prod
testing cname=server2_prod

剧本:

---

- hosts: all
  gather_facts: False

  vars:
    my_vars:
      server1_prod:
        cron: "val1"
      server2_prod:
        cron2: "val2"

  tasks:
    - name: task name 1
      debug:
        msg: "test"
      when:  my_vars[cname]['cron'] is defined

您应该跳过when部分中的引号。 https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html

答案 1 :(得分:0)

我在清单文件中:

    server1-prod     cname=server1_prod
    server2-prdo    cname=server2_prod

......
.....

在我的角色中,我在vars中为每个服务器都有一个这样的文件:server1_pprod server2_pp等... <==这是role / vars /

中文件的名称
cron:
  job1:
        name: "******"
        job: "********"
        minute: "0"
        hour: "0"
        day: "*"
        month: "*"
        weekday: "*"
        disabled: "no"
        backup: yes
        job: "*******************************"


cron_special_time:
  job1:
        name: "******"
        special_time: "reboot"
        disabled: "yes"
        backup: yes
        job: "*****"
  job2:
        name: "*****"
        special_time: "reboot"
        disabled: "yes"
        backup: yes
        job: "*****"

我将文件包含在此任务中,这样我就为每个文件serverX_prod拥有了一个variabl serveX_prod,它核心花费到清单中的serveX-prod

- name: Loading system cron  file to vars
  include_vars:
    file: "{{ cname }}"
    name: "{{ cname  }}"

然后我访问任务中的变量:

- name: Add system  cron jobs
  cron:
              .....
             ......
  with_dict:
        - "{{ vars[cname].cron }}"
   when:  "{{ vars[cname].cron }}" is defined

您看到的问题是,我不能在句子中放入“”并且没有“”也无法获得动态变量,我很困惑