23

I have a task that checks if my process is listening on port 8080 and only when the exit code is not zero, would I run fail with a message.

When the service is running, it reports that the status has changed. I want it to say ok. What does a changed status actually mean in Ansible ?

2 Answers 2

13

Anything that is dependent on something on the target will get the status "changed" when executed, even if it's just a shell command to echo something.

To suppress the "changed" status, you can add the following line to the task:

changed_when: false

This and other relevant things are listed on the Ansible doc page for error handling in playbooks.

36

Ansible tasks should be idempotent. Eg. if the task does not modify anything, it should return ok instead of changed. Most builtin modules and tasks are already, but for tasks such as command and shell you need to help ansible a bit.

For a task that does purely checking and does not modify anything, you should add:

changed_when: False
always_run: yes

The latter allows the task to run even in check mode.

For the sake of completeness, such tasks are usually combined with another that does the actual modification, eg:

- command: check command that returns true when no change needed
  register: result
  changed_when: False
  always_run: yes
- command: modify command
  when: result.rc != 0
1
  • 2
    this should verified answer
    – mastier
    Jul 12, 2017 at 7:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .