0
votes

Ansible playbook only run against one host

root@osboxes:~/ansible_project/second_p# cat conditionals_3.yml 
---
- name: show hostname
  hosts: ios-devices
  connection: local

  tasks:
     - name: show hostname
       ios_command:
               commands: "show run | in hostname"

       register: output

     - debug:
        var: output

     - name: pause with render
       pause:
        prompt: "{{lookup('template', 'conditional.j2')}}"
root@osboxes:~/ansible_project/second_p# cat conditional.j2 
output = {{output}}

{% if "las-a" in output.stdout[0]%}
I am "las-a"
{%else%}
I am "las-b"
{%endif%}
root@osboxes:~/ansible_project/second_p# cat hosts
# Ansile hosts file for lab
[all:vars]
ansible_user=xxx
ansible_ssh_pass=xxx
[ios-devices]
las-a
las-b

The actual output is

TASK [pasuse with render] ************************************************************************************* [pasuse with render] output = {'failed': False, u'changed': False, u'stdout_lines': [[u'hostname las-a', u'logging origin-id hostname']], u'stdout': [u'hostname las-a\nlogging origin-id hostname']}

I am "las-a" : ok: [las-a]

PLAY RECAP **************************************************************************************************** las-a : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
las-b : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

But I want I am "las-a" and I am "las-b"

1
Lookups do only happen on the ansible host if I recall that correctly. That might be the cause of your problem.dgw

1 Answers

0
votes

pause "Pauses playbook execution ..." i.e. whoever reaches it first pauses all forks.

Try debug and you'll see both "las-a" and "las-b".

- name: "Debug (was: pause with render)"
  debug:
    msg: "{{ lookup('template', 'conditional.j2') }}"


How pause works

Test with inventory

test_jails:
  hosts:
    test_01:
    test_02:
    test_03:

Playbook below with run_once: yes

- hosts: test_jails
  tasks:
    - pause:
        prompt: "Please enter the value"
        echo: yes
      register: result
      run_once: yes
    - debug:
        var: result.user_input

gives

PLAY [test_jails] ***************************************************************************

TASK [pause] ********************************************************************************
[pause]
Please enter the value:
[[ok: [test_01]

TASK [debug] ********************************************************************************
ok: [test_02] => {
    "result.user_input": "TEST"
}
ok: [test_01] => {
    "result.user_input": "TEST"
}
ok: [test_03] => {
    "result.user_input": "TEST"
}

PLAY RECAP **********************************************************************************
test_01                    : ok=2    changed=0    unreachable=0    failed=0   
test_02                    : ok=1    changed=0    unreachable=0    failed=0   
test_03                    : ok=1    changed=0    unreachable=0    failed=0

But the same playbook with run_once: no gives

PLAY [test_jails] ***************************************************************************

TASK [pause] ********************************************************************************
[pause]
Please enter the value:
[[ok: [test_01]

TASK [debug] ********************************************************************************
ok: [test_01] => {
    "result.user_input": "TEST"
}
ok: [test_02] => {
    "result.user_input": "VARIABLE IS NOT DEFINED!"
}
ok: [test_03] => {
    "result.user_input": "VARIABLE IS NOT DEFINED!"
}

PLAY RECAP **********************************************************************************
test_01                    : ok=2    changed=0    unreachable=0    failed=0   
test_02                    : ok=1    changed=0    unreachable=0    failed=0   
test_03                    : ok=1    changed=0    unreachable=0    failed=0
  • Note ok=2 in test_01 PLAY RECAP. In both cases, pause executed only once.
  • Without run_once: yes the registered variable was not available to other hosts.