Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use Ansible nested variable?

Tags:

ansible

jinja2

I have an Ansible playbook like the one below, I want use nested variable like this: msg={{{{Component}}.community_release_num}}, but when I run playbook:

ansible-playbook vartest.yml -e 'version=version_402', it not work 

[es@vpn-server nested-var]$ tree
.
├── vars
│   ├── horizon.yml
│   └── version_402.yml
└── vartest.yml

1 directory, 3 files
[es@vpn-server nested-var]$ cat vartest.yml 
---

- name: test 
  hosts: localhost
  vars_files:
    - vars/{{version}}.yml
  tasks:
    - debug: msg={{{{Component}}.community_release_num}}
    - debug: msg={{{{Component}}.release_num}}

[es@vpn-server nested-var]$ cat vars/horizon.yml 
Component: horizon

[es@vpn-server nested-var]$ cat vars/version_402.yml 
- horizon:
   community_release_num: '9.0.1'
   release_num: '4.0.2'
[es@vpn-server nested-var]$

error messages

[es@vpn-server nested-var]$ ansible-playbook vartest.yml -e 'version=version_402'
/usr/lib64/python2.6/site-packages/cryptography/__init__.py:25: DeprecationWarning: Python 2.6 is no longer supported by the Python core team, please upgrade your Python.
  DeprecationWarning

PLAY [test] *******************************************************************************************************
/usr/lib64/python2.6/site-packages/Crypto/Util/number.py:57: PowmInsecureWarning: Not using mpz_powm_sec.  You should rebuild using libgmp >= 5 to avoid timing attack vulnerability.
  _warn("Not using mpz_powm_sec.  You should rebuild using libgmp >= 5 to avoid timing attack vulnerability.", PowmInsecureWarning)

TASK [debug] ******************************************************************************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "template error while templating string: expected token 'colon', got '}'. String: {{{{Component}}.community_release_num}}"}
    to retry, use: --limit @/data/wangqian/artemis-code-test/artemis/ansible/update/nested-var/vartest.retry

PLAY RECAP ********************************************************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=1  

Can Ansible use nested variable, if yes, how to use it?

like image 322
qianwang Avatar asked Sep 14 '17 02:09

qianwang


People also ask

How do you pass multiple variables in Ansible?

Ansible treats values of the extra variables as strings. To pass values that are not strings, we need to use JSON format. To pass extra vars in JSON format we need to enclose JSON in quotation marks: ansible-playbook extra_var_json.

How do you use variables in Ansible?

You can define these variables in your playbooks, in your inventory, in re-usable files or roles, or at the command line. You can also create variables during a playbook run by registering the return value or values of a task as a new variable.

How do I use dynamic variables in Ansible?

To solve this problem, Ansible offers us one module named “include_vars” which loads variables from files dynamically within the task. This module can load the YAML or JSON variables dynamically from a file or directory, recursively during task runtime.

How can variables be defined within a role in Ansible?

Variables defined from included files and roles As described in Playbook Roles and Include Statements, variables can also be included in the playbook via include files, which may or may not be part of an “Ansible Role”. Usage of roles is preferred as it provides a nice organizational system.


1 Answers

Per Ansible FAQ:

Another rule is ‘moustaches don’t stack’. We often see this:

{{ somevar_{{other_var}} }} 

The above DOES NOT WORK, if you need to use a dynamic variable use the hostvars or vars dictionary as appropriate:

{{ hostvars[inventory_hostname]['somevar_' + other_var] }}

So in your case:

- debug: msg={{hostvars[inventory_hostname][Component].community_release_num}}

Or:

- debug: msg={{vars[Component].community_release_num}}

Or (since Ansible 2.5):

- debug: msg={{(lookup('vars', Component)).community_release_num}}
like image 162
techraf Avatar answered Sep 29 '22 13:09

techraf