Ashar
Ashar

Reputation: 3065

Condition based execution in ansible

I have a scenario if first condition is successful then execute condition two and if condition two is successful then execute condition three.

Below is what i tried.

vi testme.yml
---
- hosts: all

  tasks:
    - name: run this command and ignore the result
      shell: "grep -ci Hello /tmp/data.out"
      register: pingout
      ignore_errors: yes

    - debug: msg="{{ pingout.rc }}"

    - name: run the server if Hello is found in the above task
      command: echo "The server is UP since `uptime`"
      register: output1
      when:  pingout.rc == 0
    - debug: "{{ output1.stdout }}"

When the string is found I was expecting to see this executed and shown in the output: The server is UP since uptime

However, I do not see this printed in the output.

ansible-playbook -i /tmp/myhost /root/testme.yml

Output:

PLAY [all] ******************************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************
ok: [95.76.121.113]

TASK [run this command and ignore the result] *******************************************************************************************************************************
changed: [95.76.121.113]

TASK [debug] ****************************************************************************************************************************************************************
ok: [95.76.121.113] => {
    "msg": "0"
}

TASK [run the server if Hello is found in the above task] *******************************************************************************************************************
changed: [95.76.121.113]

TASK [debug] ****************************************************************************************************************************************************************
ok: [95.76.121.113] => {
    "msg": "Hello world!"
}

PLAY RECAP ******************************************************************************************************************************************************************
95.76.121.113               : ok=5    changed=2    unreachable=0    failed=0

Upvotes: 1

Views: 169

Answers (2)

Vladimir Botka
Vladimir Botka

Reputation: 68254

Correct syntax is

- debug: msg="{{ output1.stdout }}"

, or

- debug: var=output1.stdout

Upvotes: 1

ceving
ceving

Reputation: 23871

You do not need to check rc. Ansible knows, when a command failed.

---
- hosts: localhost
  connection: local

  tasks:
    - name: First
      shell: "true"
      register: first
      ignore_errors: yes

    - name: Second
      shell: "true"
      register: second
      ignore_errors: yes
      when: first is not failed

    - name: Third
      shell: "true"
      register: third
      ignore_errors: yes
      when: second is not failed

Upvotes: 2

Related Questions