Adrian Bautista
Adrian Bautista

Reputation: 9

When condition is just skipping

I have an Ansible playbook as shown below. The only problem is every time I run it is just skipping/ignoring the task.

Can you please help me figure out what is the problem?


- 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]

Upvotes: 0

Views: 461

Answers (1)

techraf
techraf

Reputation: 68449

The tasks get skipped because ansible_distribution_major_version is a string and you compare it to an integer value.

You should either fix your conditions to:

when: ansible_distribution_major_version == "12"

Or cast the value:

when: ansible_distribution_major_version | int == 12

Having fixed that, the remaining code makes little sense and will produce a syntax error.

Upvotes: 2

Related Questions