0

I am trying to preserve a command output from one dynamic host group to another one but get a variable is undefined error in my ansible playbook.

My playbook is dynamically getting the primary and secondary hosts by running a command. I want to use the output that I get from the secondary host on the primary with a when statement. Here's what I have.

- hosts: all
  serial: 1
  tasks:
   - name: run curl command to determine primary/secondary
     command: /tmp/status.sh
     register: apis_op
   - name: run curl command to determine primary/secondary
     debug: var=apis_op.stdout_lines

   - name: If node is primary add to primary
     add_host:
       name: "{{ ansible_hostname }}"
       groups: temp_primary
     when: apis_op.stdout == "primary"
   - name: run curl command to determine primary/secondary
     debug: var=groups

   - name: If node is secondary add to secondary
     add_host:
       name: "{{ ansible_hostname }}"
       groups: temp_secondary
     when: apis_op.stdout == "secondary"
   - name: run curl command to determine primary/secondary
     debug: var=groups

- hosts: temp_secondary
  tasks:

   - name: Run schell script
     command: /tmp/runthis.sh
     become: yes
     become_user: root
     register: op

- hosts: temp_primary
  tasks:

   - name: Run schell script
     command: /tmp/testthis.sh
     become: yes
     become_user: root
     when: hostvars['temp_secondary']['op'] == "{false}"

When I run this I get a hostvars['temp_secondary'] is undefined error. Can I not use hostvars with temporary/dynamic host groups?

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
user3164720
  • 73
  • 1
  • 7
  • Task you register never persist from one play to another. You have to use a `set_fact` for this. So, run you command, register the output then use a `set_fact` to assign the registered output to a "new" variable. Then you're good to go. – β.εηοιτ.βε Jul 26 '21 at 19:48
  • thanks for the response. So, do i just do ```- hosts: temp_secondary tasks: - name: Run schell script command: /tmp/runthis.sh become: yes become_user: root register: op set_fact: sec_state: "{{hostvars['localhost']['op']}}" - hosts: temp_primary tasks: - name: Run schell script command: /tmp/testthis.sh become: yes become_user: root when: "{{ sec_state }}" == "{false}" ``` – user3164720 Jul 26 '21 at 21:11
  • Comments are not meant to receive large portions or code not to add specific info meeded to answer your question. Please [edit](/posts/68535394/edit) your question instead. – Zeitounator Jul 27 '21 at 09:43
  • Thanks for the suggestion. @Zeitounator. I'll correct that as soon as I have a good direction to proceed. – user3164720 Jul 27 '21 at 12:11
  • @β.εηοιτ.βε Would you be able to help with the set_facts, please? – user3164720 Jul 27 '21 at 18:16
  • Hostvsrs is a dictionary which keys are hostnames. Temp_secondary is a group name so you won't get an entry for it. For set_fact, you can push the result in the same variable name 'op' for simplicity. – Zeitounator Jul 27 '21 at 19:08

0 Answers0