Reputation: 23
I have a variable declared, and I am trying to get its value to show up in the key, when attempting to use the ec2_instance_info ansible module. I want the value to show up under tags. Please view the dummy code below.
vars:
tag_key: Key
tag_value: Value
tasks:
- name:
ec2_instance_info:
filters:
"tag: {{ tag_key }}": "{{ tag_value }}"
I want the above to output as:
tag:Key:Value
But instead it comes out as:
tag:{{ tag_key }}:Value
As a result, when I run the commands, it doesn't call any instances, since they're searching for the wrong thing. The code works fine when I swap the variables out for regular strings. (I'm aware the syntax is probably wrong in the dummy code, I've tried a bunch of things now.)
I attempted the following: Ansible variable in key/value key And while it works in displaying the variables, it now registers as a dict and I get the error:
Invalid type for parameter Filters[0].Values, value: {'Key': 'Value'}, type: <type 'dict'>, valid types: <type 'list'>, <type 'tuple'>"
So I guess I'm looking for either a way to use variables in key names without it turning to a dict, and if that's not available, to transform that into a list. Thanks in advance.
Upvotes: 0
Views: 618
Reputation: 7340
The filters
of ec2_instance_info
module requires a "dict". So one way to supply that "dict" is to create one in vars:
.
Something like:
vars:
ec2_filters:
"tag:Name": "my-instance-1"
tasks:
- ec2_instance_info:
filter: "{{ ec2_filters }}"
register: ec2_out
- debug:
var: ec2_out
Or call the nested variables as a dict inside filters:
vars:
tag_key: Key
tag_value: Value
tasks:
- ec2_instance_info:
filter:
'{ "tag:{{ tag_key }}": "{{ tag_value }}" }'
register: ec2_out
Upvotes: 1