_ansible_item_label changes in Ansible 2.8
The Ansible 2.8 upgrade brought a few failing Playbooks. I ran into the following error:
TASK [Demo Task] ************************************************************************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute '_ansible_item_label'\n\nThe error appears to be in '/path/to/ansible/playbook.yml': line 76, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Demo Task\n ^ here\n"}
Well, seems like another upgrade with another major change. Don't you like that?
Ok, let's see, what is the problem here? I'm running the following task, with a loop, and want to control the label which is printed. "demos.results" contains a structure with keys. In Ansible < 2.8, "_ansible_item_label" will just print the key which Ansible is currently looping over.
- name: Demo Task
debug:
loop: "{{ demos.results }}"
loop_control:
loop_var: _demo
label: "{{ _demo._ansible_item_label }}"
However "_ansible_item_label" is no longer available. The "new" way to do this is:
- name: Demo Task
debug:
loop: "{{ demos.results }}"
loop_control:
loop_var: _demo
label: "{{ _demo._variable|default('') }}"
Know and print the key name, and also add a default in case it's empty.
And if you want your playbook to run on both Ansible < 2.8 and Ansible >= 2.8, you need two blocks of code:
- block:
- name: Demo Task
debug:
loop: "{{ demos.results }}"
loop_control:
loop_var: _demo
label: "{{ _demo._ansible_item_label }}"
when: ansible_version.full < "2.8.0"
- block:
- name: Demo Task
debug:
loop: "{{ demos.results }}"
loop_control:
loop_var: _demo
label: "{{ _demo._variable|default('') }}"
when: ansible_version.full >= "2.8.0"
Comments
Display comments as Linear | Threaded