Reputation: 2931
I am new to Ansible and have to update existing playbook tasks. Initially, there are 2 tasks to copy yml variable values to files - this runs in a loop per each cert and keys values. I want if the copy tasks are not changed - basically if the content is same the files are not overwritten, then the remaining tasks in the playbook should not be executed.
- name: Copy cert files
copy:
content: "{{item.value}}"
dest: /home/copyTest/dest/{{item.key}}.crt
owner: test
group: test
mode: 0644
with_dict: '{{certs_dict}}'
- name: Copy key files
copy:
content: "{{item.value}}"
dest: /home/copyTest/dest/{{item.key}}.key
owner: test
group: test
mode: 0644
with_dict: "{{keys_dict}}"
- name: Stop server
...
...
If both these copy tasks results are unchanged, i.e, none of certs and keys files are unchanged, then the further tasks(Stop server, Start server, etc.) should not be executed.
Upvotes: 3
Views: 19680
Reputation: 311635
Use the register
keyword to store the results of your first two tasks:
- name: Copy cert files
copy:
content: "{{item.value}}"
dest: /home/copyTest/dest/{{item.key}}.crt
owner: test
group: test
mode: 0644
with_dict: '{{certs_dict}}'
register: cert_results
- name: Copy key files
copy:
content: "{{item.value}}"
dest: /home/copyTest/dest/{{item.key}}.key
owner: test
group: test
mode: 0644
with_dict: "{{keys_dict}}"
register: key_results
Place you remaining tasks in a separate task list file, and then in your main playbook conditionally include those additional tasks only if there were changes:
- include_tasks: additional_tasks.yml
when: cert_results is changed or key_results is changed
You can read about including tasks here.
You can achieve something similar using a block
in your playbook:
- when: cert_results is changed or key_results is changed
block:
- name: Stop server
- name: Do something else
...
You can read about blocks here.
Upvotes: 12