This looks wrong
Your example adds another path to your playbook. If systemd is not installed, the output will be different than if it is installed. And after the first run it will be installed. This goes against the ansible priciple that says:
Idempotency
An operation is idempotent if the result of performing it once is exactly the same as the result of performing it repeatedly without any
intervening actions.
If you still do it, make it as explicit as possible
I suggest you run a command which systemctl
and register the output. Check the output to install systemd and fail with a fail task.
This is still a very interesting question
I guess there is no real documentation and we have to investigate.
I hope I catched all cases :) but I cannot fill the chart right now.
playbook.yml:
---
- name: test some stuff
hosts: all
tasks:
- include_tasks: tasks.yml
with_items:
- { data: ping, changed: true }
- { data: ping, changed: false }
- { data: crash, changed: true }
- { data: crash, changed: false }
tasks.yml
---
- name: Check for command_result is defined and command_result
ping:
data: "{{ item.data }}"
register: command_result
changed_when: item.changed
failed_when: command_result is defined and command_result
ignore_errors: true
- name: Check for command_result is defined and command_result
file:
path: ./file
register: command_result
changed_when: item.changed
failed_when: command_result is defined and command_result
ignore_errors: true
- name: Check for command_result
ping:
data: "{{ item.data }}"
register: command_result
changed_when: item.changed
failed_when: command_result
ignore_errors: true
- name: Check for command_result
file:
path: ./file
register: command_result
changed_when: item.changed
failed_when: command_result
ignore_errors: true
- name: Check for command_result.changed is defined and command_result.changed
ping:
data: "{{ item.data }}"
register: command_result
changed_when: item.changed
failed_when: command_result.changed is defined and command_result.changed
- name: Check for command_result.changed is defined and command_result.changed
ping:
data: "{{ item.data }}"
register: command_result
changed_when: item.changed
failed_when: command_result.changed is defined and command_result.changed
ignore_errors: true
- name: Check for command_result.changed
ping:
data: "{{ item.data }}"
register: command_result
changed_when: item.changed
failed_when: command_result.changed
ignore_errors: true
- name: Check for command_result.changed
file:
path: ./file
register: command_result
changed_when: item.changed
failed_when: command_result.changed
ignore_errors: true
.changed
while referencing the command example quoted from the docs, given that commands show as changed by default. But I hope the second example I added mostly clears that up. – sourcejedi Feb 27 '18 at 17:54assert
, it raised this question about how well-defined these Ansible features really are. – sourcejedi Feb 28 '18 at 08:23