Home > Blockchain >  Retrieve variable values from var file in ansible playbook
Retrieve variable values from var file in ansible playbook

Time:12-09

Need some help to retrieve child values from var file.

Below is my var file

api_user: root
api_password: !vault |
          $ANSIBLE_VAULT;1.2;AES256;isi33835326238346338613761366531376662613865376263656262
          6138
Huston:
  onefs_host: 10.88.55.00

Phoenix:
  onefs_host: 10.76.52.01

Below is my playbook

---
- name: isi_increase
  hosts: localhost
  connection: local

  vars_files:
         - isilonvars.yml

  tasks:
    - name: Print
      debug:
        msg:
          - "{{ Huston.onefs_host }}"
          - "{{ api_user }}"
          - "{{ api_password }}"

This code works perfectly

TASK [Print] ******************************************************************************************************************************************************************************** ok: [localhost] => { "msg": [ "10.88.55.00", "root", "Sh00tm3!" ] }

But as per my requirement i have to retrieve onefs_host IP as per location in my playbook. I am using extra vars here -e "location=Huston"

    - name: Print
      debug:
        msg:
#          - "{{ wcc.onefs_host }}"
          - "{{ {{location}}.onefs_host }}"
          - "{{ api_user }}"
          - "{{ api_password }}"

I am getting the below error.

fatal: [localhost]: FAILED! => {"msg": "template error while templating string: expected token ':', got '}'. String: {{ {{isilon_location}}.onefs_host }}"}

CodePudding user response:

Can you try this way

- name: Print
  debug:
    msg:
    - "{{ vars[location]['onefs_host'] }}"
    - "{{ api_user }}"
    - "{{ api_password }}"
  • Related