Encountered below error:
ERROR! the field ‘hosts’ has an invalid value, which appears to include a variable that is undefined. The error was: ‘hostvars’ is undefined
I am using below command to define hosts value:
- hosts: “{{ ‘prod-app-servers’ if hostvars[‘127.0.0.1’].result.stdout == ‘prod’ else ‘dr-app-servers’ }}”
I have tried different possibilities but of no use:
- hosts: “{{ ‘prod-app-servers’ if hostvars[‘127.0.0.1’][result].stdout == ‘prod’ else ‘dr-app-servers’ }}”
I have tried below jinja format aswell, but of no use…
{% if {{hostvars[127.0.0.1][result][stdout]}} == prod %}prod-app-servers{% else %}dr-app-servers{% endif %}
My intention is to decide host during runtime based on a result.
I am not sure what is the mistake. prod-app-servers & dr-app-servers are the host groups that i defined in inventory file. ‘result’ is the value that gets populated at the begining of the playbook execution:
- hosts: 127.0.0.1
connection: local
gather_facts: no
tasks: - name: Check Failover or Failback
command: --Executing a script file with some parameters–
register: result
It fails on the syntax check, however the playbook run fine as expected.