0

我有一个 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]
4

1 回答 1

2

任务被跳过,因为ansible_distribution_major_version它是一个字符串,你将它与一个整数值进行比较。

您应该将您的条件修复为:

when: ansible_distribution_major_version == "12"

或转换值:

when: ansible_distribution_major_version | int == 12

解决了这个问题后,剩下的代码就没有什么意义了,并且会产生语法错误。

于 2018-08-20T07:45:08.887 回答