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?