CentOS 6
Sponsored Link

Ansible : Use Playbook#32015/04/22

To use "when", "failed_when", it's possible to write branch condition in Ansible Playbook.
[1] For example, create a Playbook which only if it does not exist "/var/www/html/index.html", create it on target hosts.
[cent@dlp ~]$
vi playbook_sample.yml
# set boolean with failed_when

# create "index.html" if result of boolean is "1"

- hosts: target_servers
  become: yes
  become_method: sudo
  - name: index file exists or not
    shell: test -f /var/www/html/index.html
    ignore_errors: true
    register: file_exists
    failed_when: file_exists.rc not in [0, 1]

  - name: put index.html
    shell: echo "httpd index" > /var/www/html/index.html
    when: file_exists.rc == 1

[cent@dlp ~]$
ansible-playbook playbook_sample.yml --ask-become-pass

SUDO password:

PLAY [target_servers] *********************************************************

GATHERING FACTS ***************************************************************
ok: []
ok: []

TASK: [index file exists or not] **********************************************
changed: []
changed: []

TASK: [put index.html] ********************************************************
skipping: []
changed: []

PLAY RECAP ********************************************************************                  : ok=3    changed=2    unreachable=0    failed=0                  : ok=2    changed=1    unreachable=0    failed=0
Matched Content