Reputation: 887
I am automating stack deployment with ansible. I am using "shell" or "command" module to execute most of the task. So In order to make playbook idempotent with shell and module, I am modifying playbook like this..
- name: Task1
shell: 'source /etc/nova/openrc && heat stack-show myne01 | tee stack_show.log'
args:
creates: stack_show.log
register: list
- debug: var=list.stdout_lines
ignore_errors: yes
- name: Delete stack-show.log
file: path=/home/wrsroot/stack_show.log state=absent
when: "list.rc != 0"
- name: Failed the stack
shell: "echo 'stack is failed'"
when: "list.rc != 0"
failed_when: "list.rc != 0"
Here the flow is :
1) Display stack status
2) If stack execution has failed, ignore errors and delete the "stack_show.log" file, So on the rerun anisble won't skip this task.
3) If stack execution has failed, Failed the task.
Please suggest if any better way to do this.
To add idempotency in playbook I am adding 9 lines of code for each "shell" module. It is making my playbook very big.
Upvotes: 1
Views: 7780
Reputation: 5648
You just need changed_when: false
to be idempotent.
Also I think you can do this more simply:
- name: Task1
shell: bash -c 'set -o pipefail;source /etc/nova/openrc && heat stack-show myne01 | tee stack_show.log'
changed_when: false
args:
creates: stack_show.log
register: list
- name: Delete stack-show.log
file: path=/home/wrsroot/stack_show.log state=absent
changed_when: false
# You don't need this because file will deleted if exists
# when: "list.rc != 0"
# You don't need it because command will failed
# set -o pipefail
#- name: Failed the stack
# shell: "echo 'stack is failed'"
# when: "list.rc != 0"
# failed_when: "list.rc != 0"
You cat try Ansible 2.x Blocks
tasks:
- block:
- shell: bash -c 'set -o pipefail;source /etc/nova/openrc && heat stack-show myne01 | tee stack_show.log'
changed_when: false
args:
creates: stack_show.log
register: list
always:
- debug: msg="this always executes"
- name: Delete stack-show.log
file: path=/home/wrsroot/stack_show.log state=absent
changed_when: false
Upvotes: 1