wicemantel
wicemantel

Reputation: 1

Ansible hostvars[''variable_holder'']" is undefined

My Pipeline throws the following error:

TASK [microstrategy.mstrpax_configure : Get Project Names from Rest API] ***
skipping: [] => changed=false 
  false_condition: mstr_role in ("webserver")
  skip_reason: Conditional result was False

TASK [microstrategy.mstrpax_configure : Configure Telemetry Logging] ***
fatal: []: FAILED! => 
  msg: '"hostvars[''variable_holder'']" is undefined. "hostvars[''variable_holder'']" is undefined'

My Questions are.

In my playbook (mstrpax_configure_tasks.yml) I have during my run set conditions to skip some tasks when the condition is not true.

So far so good...but my task which where skipped, I set during the run inside the task before (mstrpax_projects_loaded.yml) over set_fact a hostvars variable.

- name: 'Get Project Names from Rest API'
      ansible.builtin.include_tasks: mstrpax_projects_loaded.yml
      when: 'mstr_role in ("webserver")'

In mstrpax_projects_loaded.yml "variable_holder" will be set during the run. But only inside the Hostgroup "webserver".

- name: 'Register mstr_api_projnames_loaded Var'
  ansible.builtin.set_fact:
    mstr_api_projnames_output: '{{ mstr_api_projnames_loaded }}'

- name: 'Add Variable to Dummy Host'
  ansible.builtin.add_host:
    name: 'variable_holder'
    mstr_api_projnames_output: '{{ mstr_api_projnames_output }}'

Variable hostvars['variable_holder'] will be used down below. But for the Hostgroup "iserver"

- name: 'Enable Telemetry Logging'
  block:

    - name: 'Configure Telemetry Logging'
      ansible.builtin.include_tasks: mstrpax_telemetry_logging.yml
      loop: "{{ hostvars['variable_holder']['mstr_api_projnames_output'].json | map(attribute='name') | list }}"
      loop_control:
        loop_var: mstr_projects
      when: 'mstr_role in ("iserver")'

Note the Mapping of that "Hostgroups" will handled over ansible. That means Servers get a specified Tag "Role" which will be mapped then inside that Variable "_mstr_appcode2role" The tasks will only be executed when a hostname pattern matches to the setted Tag "role" inside Terraform.

mstr_role: '{{ tf_host["role"] }}'

# appcode to role mapping
_mstr_appcode2role:
  mstr: 'iserver'
  mstrweb: 'webserver'

Upvotes: 0

Views: 127

Answers (0)

Related Questions