I am trying to load a variable with information derived from the inventory groups that servers are members of. I have an inventory file that looks like the following:
[group1]
server1
server2
server3
[group2]
server2
[group3]
server1
server2
I need to set the variable such that it indicates each of the groups that a server exists in. So for server1, the variable would be set like: ‘variable: “app1,app3”’ because it is a member of group1 and group3. For server2, it would be: ‘variable: “app1,app2,app3”’ because it is a member of all three groups, and for server3 it would be: ‘variable: “app1”’ because it is a member of just group1. The variable will be used in a template to set some parameters there before installing the resulting file on the servers.
This is probably simple for someone with more experience than me, but I am just starting out with this and have not been able to understand the syntax enough to make it work.
Any help would be greatly appreciated.
-Mark
I have been looking at the group_names variable to try to see how I can use that. I am thinking in the template I need to do something like the following:
{% for mygroup in group_names %}
{% if mygroup[application] %}
applications = “applications mygroup[application]”
{% endif %}
{% endfor $}
So the intent is that “application” would be loaded in the groups_vars/groupname files. But not all group files will have this variable set as some of those groups refer to environment groups, not application groups (i.e. development, production, qc, etc.), so I need to test to see if the application variable is loaded for a group. If it is, then it needs to be concatenated to the contents of the applications variable. So, my syntax above is likely wrong; what is the proper syntax if this is indeed possible?
Thanks,
-Mark
Okay, I am having trouble access the variable in the group files from the template. I my group files I have the following:
group file1 -
application: app1
group file 2 -
application: app2
group file 3 -
Then in the template I have:
{{ application | pprint }}
{% for mygroup in group_names %}
{{ mygroup | pprint }}
{{ application | pprint }}
{% if ‘application’ in mygroup %}
{{ application | pprint }}
{% endif %}
{% endfor %}
The ‘mygroup’ pprint statement outputs the group names as expected. The outermost ‘application’ pprint always outputs the last group file’s value (app3), but the next ‘application’ pprint statement inside of the for loop always returns “Undefined”, and the innermost statement is never reached. I have tried things like mygroup.application and mygroup[application], but still nothing. So what is the proper way to get at the variables contained within a group file?
Thanks,
-Mark
That’s correct, I was referring to the group_var/groupname files, and yes, my servers can occupy more than one group. I was hoping for something like a hostvars structure, so that I could do something like mygroup[application] and get the value of application in that group. But it sounds like not. So, in each group_vars/groupname file, I would need to define a variable unique to that group (file group1 contains variable group1_app, file group2 contains variable group2_app, file group3 contains, variable group3_app, etc.), then in the templates I need to check for all possible applications:
{{ applications | default(“”) }}
{% if ‘group1’ in group_names and group1_app is defined %}
{% set applications = applications ~ group1_app ~ " " %}
{% endif %}
{% if ‘group2’ in group_names and group2_app is defined %}
{% set applications = applications ~ group2_app ~ " " %}
{% endif %}
{% if ‘group3’ in group_names and if group3_app is defined %}
{% set applications = applications ~ group3_app ~ " " %}
{% endif %}
And so on for each and every possible group I have in my inventory. The problem with that is that I have many, many groups, so this list can get quite long and ugly. And every time I add a new application to my inventory, I have to go through each of my template files to add a check for that application. Is there any easier method than this?
Thanks,
-Mark
" was hoping for something like a hostvars structure, so that I could do something like mygroup[application] and get the value of application in that group. But it sounds like not. "
We may be communicating on completely different wavelengths.
I view the group_vars/ structure exactly like host_vars/.
In the Ansible documentation there is discussion about “Magic Variables”. Among them are hostvars, group_name, and groups. You can get at variables for other hosts with the structure {{ hostvars[‘server_name’][‘host_variable’] }}. I was hoping for something similar for group information: {{ groupvars[‘group_name’][‘group_variable’] }}. This would return the value of ‘group_variable’ that is set within group_vars/groupname. That would allow me to iterate through the groups that a server is a member of and collect the value of ‘group_variable’ in those groups. Is there any way to do this?
Thanks,
-Mark
Yeah, I finally figured that out. In my group_vars/groupname files, I set a variable whose name is the same as the file name:
group_vars/groupname:
groupname:
app_var: app1
Then in my template I can iterate over the groups:
{%- set applications = [‘’] %}
{%- for mygroup in group_names %}
{%- if hostvars[inventory_hostname][mygroup].app_var is defined %}
{%- do applications.insert(0, applications[0] ~ hostvars[inventory_hostname][mygroup].app_var) %}
{%- do applications.pop() %}
{%- endif %}
{%- endfor %}
This finally does what I have been looking for (trying to dig the value of “applications” out of the middle of the for loop was yet another exercise in frustration!).
Thanks,
-Mark
Yeah, I finally figured that out. In my /group_vars/groupname files, I set a variable whose name is the same as the file name:
group_vars/groupname:
groupname:
app_var: app1
Then in my template I can iterate over the groups:
{%- set applications = [‘’] %}
{%- for mygroup in group_names %}
{%- if hostvars[inventory_hostname][mygroup].app_var is defined %}
{%- do applications.insert(0, applications[0] ~ hostvars[inventory_hostname][mygroup].app_var) %}
{%- do applications.pop() %}
{%- endif %}
{%- endfor %}
This finally does what I have been looking for (trying to dig the value of “applications” out of the middle of the for loop was yet another exercise in frustration!).
Thanks,
-Mark