当条件只是跳过时

时间:2018-08-20 04:31:43

标签: ansible

我有一个Ansible剧本,如下所示。唯一的问题是,每次运行时,它只是跳过/忽略任务。

能帮我找出问题所在吗?


- name: Register Host to Dynamic Inventory
  hosts: localhost
  gather_facts: false
  tasks:
    - add_host:
        name: "{{ myhost }}"

- name: Enabling the services
  hosts: "{{ myhost }}"
  gather_facts: true
  tasks:
    - name: Make the service available persistently after a reboot for  SLES11
      command: systemctl enable after.local
      with_items: "{{ ansible_distribution_major_version }}"
      when: ansible_distribution_major_version == 11

    - name: Make the service available persistently after a reboot for  SLES12
      command: systemctl enable after.local
      with_items: "{{ ansible_distributioni_major_version }}"
      when: ansible_distribution_major_version == 12
TASK [add_host] ****************************************************************03:22:06
changed: [localhost]
PLAY [Enabling the services] ***************************************************03:22:06
TASK [Gathering Facts] *********************************************************03:22:06
ok: [hostname]
TASK [Make the service available persistently after a reboot for  SLES11] ******03:22:10
skipping: [hostname] => (item=12) 
TASK [Make the service available persistently after a reboot for  SLES12] ******03:22:10
skipping: [hostname]

1 个答案:

答案 0 :(得分:2)

由于ansible_distribution_major_version是字符串,并且将其与整数值进行比较,因此跳过了任务。

您应该将条件修改为:

when: ansible_distribution_major_version == "12"

或强制转换值:

when: ansible_distribution_major_version | int == 12

已修复该问题,其余代码毫无意义,并且会产生语法错误。