Reputation: 1263
I'm using with_items
iterator to execute command: brew services stop {{ item }}
.
To handle errors I'd like to use changed_when
and use item
value in it.
command: brew services stop {{ item }}
register: stop_services
changed_when:
- "'Error: Service `{{ item }}` is not started.' not in stop_services.stderr"
with_items:
- memcached
- kafka
If service is not started, I get following error
failed: [127.0.0.1] (item=memcached) => {"changed": false, "cmd": ["brew", "services", "stop", "memcached"], "delta": "0:00:00.464519", "end": "2016-12-29 18:02:37.795973", "failed": true, "item": "memcached", "rc": 1, "start": "2016-12-29 18:02:37.331454", "stderr": "Error: Service
memcached
is not started.", "stdout": "", "stdout_lines": [], "warnings": []}
But changed_when
statement is ignored.
Is it possible to inject item
into changed_when
statement?
Ansible version: 2.2
Upvotes: 1
Views: 2100
Reputation: 68459
You've got a failing task in the first place. The command
module will report failure if the return code from the command was other than zero (in your case "rc": 1
) and that's why your changed_when
condition is not taken into consideration.
Add the following to prevent failing:
failed_when: false
Upvotes: 1