-2

Unix: server1:

I have a structure for my var hosts like this:

>ls hosts/

test_servers
uat_servers

>more test_servers

server1
server2
server3

>ls host_vars/

server1.yml
server2.yml
server3.yml

>more server1.yml

envs: [
{ type: "dev",
list_vars: [
{ param: "AMD" },
{ param: "INTEL", param2: "2"}
]
},
{ type: "uat",
list_vars: [
{ param: "AMD" },
{ param: "INTEL", param2: "3"}
]
}
]


template:

more test.j2

{% for host in groups['test_servers'] %}

"{{ host }} "

{% for env in envs %}
{% for par in env.list_vars %}
Alias {{ env.type }}/{{ par.type }}/ "www"
{% endfor %}
{% endfor %}
{% endfor %}

Result:

I always have the same variables for different servers and it always server1 but {{ host }} returns server1, server2,server3.

How can I get the parameters for server2 , server3 if I want to use such structure?

avsun
  • 1
  • 2
  • 1
    Please use [Markdown](http://serverfault.com/editing-help) and/or the formatting options in the edit menu to properly type-set your posts to improve their readability. That improves readability and attracts better answers, which may help people with similar questions. – HBruijn Apr 24 '17 at 13:06

1 Answers1

-2

>more test_servers

[unix]
server1
server2
server3

>more test.j2

{% for host in groups['unix'] %}
{% for env in hostvars[host]['envs'] %}
{% for par in env.list_vars %}
Alias {{ env.type }}/{{ par.type }}/ "www"
{% endfor %}
{% endfor %}
{% endfor %}

ansible-playbook main.yml -i hosts/test_servers

avsun
  • 1
  • 2
  • It looks like you may have the knowledge to provide good Answer here, but please consider reading [How do I write a good Answer?](http://serverfault.com/help/how-to-answer) in our help center and then revise the Answer. Your Commands/Code/Settings may technically be the solution but some explanation and context is welcome. Thanks in advance. – HBruijn Apr 24 '17 at 13:40