Reputation: 425
I am using the ec2_asg cloud module in ansible to update an Auto-Scaling group. I am trying to make this a role so I can apply this to multiple playbooks, and I was wondering if could pass in the tags values as variables.
Without variables the asg task looks like:
ec2_asg:
name: "test_app"
tags:
- Environment: Production
- Name: test_app
And I know I could also do:
ec2_asg:
name: {{asg_name}}"
tags:
- Environment: "{{asg_tag_env}}"
- Name: "{{asg_tag_name}}"
But I'm curious if in my group_vars I can pass all of the tagging info in as a var, i.e.
asg_tags: "{'tags': {'Environment': Production, 'Name': test_app}}" #option1
or
asg_tags: "{'Environment': Production, 'Name': test_app}" #option2
When I try can call that in the ec2_asg module via
#option1
ec2_asg
name: "{{asg_name}}"
"{{asg_tags}}"
this doesn't seem to work, as the module doesn't understand the root index of the dictionary is the tags option you specify in the ec2_asg module.
I just get a syntax error while loading YAML.
and if I try
#option2
ec2_asg
name: "{{asg_name}}"
tags: "{{asg_tags}}"
I get back an error from ansible stating:
for k,v in tag.items():\nAttributeError: 'str' object has no attribute 'items'\n"
Either this isn't supported, or I'm not sure how to structure the variable so the ec2_asg module reads in the tags correctly and knows how to apply them.
Upvotes: 2
Views: 3032
Reputation: 312098
First, note that that the values of tags
is a list of dictionaries, each with a single key:
tags:
- Environment: Production
- Name: test_app
If you paste that into a YAML parser like this one you'll see that the corresponding JSON is:
{
"tags": [
{
"Environment": "Production"
},
{
"Name": "test_app"
}
]
}
So whatever value you assign to a variable it will need to be a similar list.
I'm not able to test out the ec2* modules myself at the moment, but if you define a variable appropriately, like this:
asg_tags:
- Environment: Production
- Name: test_app
Or alternately (although I find the former easier toread):
asg_tags: [{"Environment": "Production"}, {"Name": "test_app"}]
You should then be able to do:
- ec2_asg:
name: "test_app"
tags: "{{ asg_tags }}"
Upvotes: 3