0

I have my Ansible inventory file as:

[dev_web:children]
dev_www
dev_auto

[dev_www]
web01_dev    ansible_host=web01-dev.company.com
web02_dev    ansible_host=web02-dev.company.com
web03_dev    ansible_host=web03-dev.company.com

[dev_auto]
auto_dev     ansible_host=auto-dev.company.com

Ansible playbook "/deploys/patch.yml" as:

- name: patch
  hosts: "{{ configs_target | default('test_servers') }}"
  serial: 1
  vars:
    slack_token: 'slack-token'
    slack_channel: 'slack-channel'
  roles:
    - patch

Main Ansible playbook file "/deploys/roles/patch/tasks/main.yml” as:

---
- name: Include vars of patch.yml
  include_vars:
    file: backend_vars.yml
    name: web_servers

- name: set needed facts
  set_fact:
    backend_name: "{{ [cache_mgmt][configs_target]['backend_name'] }}"

Variable file “/deploys/roles/patch/vars/backend_vars.yml” as:

cache_mgmt:
  dev_web:
    backend_name: www_http_be
    backend_hosts: dev_www
  dit_web:
    backend_name: www_http_be
    backend_hosts: dit_www

The run command I execute is:

ansible-playbook /deploys/patch.yml -e '{"configs_target":"dev_web"}'

When I run the playbook, I expect the value of variable to be backend_name: www_http_be. But, it fails with below error.

TASK [patch : set needed facts] *****************************************************************************************
fatal: [web01_dev]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'dev_web'\n\nThe error appears to be in '/deploys/roles/patch/tasks/main.yml': line , column , but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: set needed facts\n  ^ here\n"}

I believe the issue is with the syntax for set_fact. If that is true, what should be the correct form in this case?

0

Since the vars are being called from a variable file, we'll have to mention the assigned name as well. The correct syntax is "{{ web_servers['cache_mgmt'][configs_target]['backend_name'] }}"

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.