Facebooktwittergoogle_plusredditpinterestlinkedinmail

While developing Ansible playbook for my on project I have some one usecase regarding use variable of variable in ansible playbook. So I found some workaround as I described in my this article.

Problem :-

I am trying to access variables which is defined in `group_vars`.  As we know variable defined in group_vars are accessible from ansible playbook.

group_vars/all

parent1:
  child1: somevalue1
  child2: somevalue2
parent2:
  child1: somevalue1
  child2: somevalue2

Now I am passing parent detail from ansible playbook from extra vars like
this

ansible-playbook playbook.yml -e "parent=parent1"

Now How can I access parent1.child1 value where parent1 comes in {{ parent }} vars?

Answer is no.

Solution:-

I have added group_vars variables under one variable called data like below

data:
  parent1:
   child1: somevalue1
   child2: somevalue2
  parent2:
   child1: somevalue1
   child2: somevalue2

In my playbook I am able to access parent1.child1 below in variable

{{ data[parent].child1 }}

My playbook code look like this

- hosts: local
  user: roop
  gather_facts: no
  connection: local
  vars:
    parent: ""
  tasks:
  #get parent value
  - debug: msg={{ parent }}
  #trying to access parent1.child1 value here
  - debug: msg={{ data[parent].child1 }}
Variable of variable in ansible playbook Roopendra AnsibleDevOPS,
While developing Ansible playbook for my on project I have some one usecase regarding use variable of variable in ansible playbook. So I found some workaround as I described in my this article. Problem :- I am trying to access variables which is defined in `group_vars`.  As we know variable defined...
While developing <a href="http://techieroop.com/category/ansible/" target="_blank">Ansible </a>playbook for my on project I have some one usecase regarding use variable of variable in ansible playbook. So I found some workaround as I described in my this article. <h4>Problem :-</h4> I am trying to access variables which is defined in `group_vars`.  As we know variable defined in group_vars are accessible from <a href="http://techieroop.com/category/ansible/" target="_blank">ansible</a> playbook.group_vars/all <pre><code>parent1: child1: somevalue1 child2: somevalue2 parent2: child1: somevalue1 child2: somevalue2 </code></pre> Now I am passing parent detail from <a href="http://techieroop.com/category/ansible/" target="_blank">ansible </a>playbook from extra vars like this <pre><code>ansible-playbook playbook.yml -e "parent=parent1"</code></pre> Now How can I access <strong>parent1.child1</strong> value where <strong>parent1</strong> comes in <strong>{{ parent }}</strong> vars?Answer is no.<span id="more-76"></span> <h4>Solution:-</h4> I have added group_vars variables under one variable called data like below <pre><code>data: parent1: child1: somevalue1 child2: somevalue2 parent2: child1: somevalue1 child2: somevalue2</code></pre> In my playbook I am able to access parent1.child1 below in variable<strong>{{ data[parent].child1 }}</strong>My playbook code look like this <pre><code>- hosts: local user: roop gather_facts: no connection: local vars: parent: "" tasks: #get parent value - debug: msg={{ parent }} #trying to access parent1.child1 value here - debug: msg={{ data[parent].child1 }} </code></pre>
(Visited 2,905 times, 128 visits today)
Facebooktwittergoogle_pluslinkedinrss