Eli
Eli

Reputation: 38899

resolve dictionary key or parameter variable in Ansible

In Ansible, if I try to use a variable as a parameter name, or a key name, it is never resolved. For example, if I have {{ some_var }}: true, or:

template: "{{ resolve_me_to_src }}": "some_src"

the variables will just be used literally and never resolve. My specific use case is using this with the ec2 module, where some of my tag names are stored as variables:

- name: Provision a set of instances
  ec2:
    group: "{{ aws_security_group }}"
    instance_type: "{{ aws_instance_type }}"
    image: "{{ aws_ami_id }}"
    region: "{{ aws_region }}"
    vpc_subnet_id: "{{ aws_vpc_subnet_id }}"
    key_name: "{{ aws_key_name }}"
    wait: true
    count: "{{ num_machines }}"
    instance_tags: { "{{ some_tag }}": "{{ some_value }}", "{{ other_tag }}": "{{ other_value }}" }

Is there any way around this? Can I mark that I want to force evaluation somehow?

Upvotes: 21

Views: 18129

Answers (2)

Thales Ceolin
Thales Ceolin

Reputation: 2644

Another option - you can try something like:

  module_name: "{{ item.key }}={{ item.value }}"
  with_items:
  - { key: "option", value: "{{ any_value }}" }

Please note that everything is inline and I'm using an equal (=) and everything is wrapped with double quotes.

Upvotes: 0

Kashyap
Kashyap

Reputation: 17401

Will this work for you?

(rc=0)$ cat training.yml
- hosts: localhost
  tags: so5
  gather_facts: False
  vars: [
      k1: 'key1',
      k2: 'key2',
      d1: "{
        '{{k1}}': 'value1',
        '{{k2}}': 'value2',
      }",
    ]
  tasks:
  - debug: msg="{{item}}"
    with_dict: "{{d1}}"


(rc=0)$ ansible-playbook training.yml -t so5

PLAY [localhost] **************************************************************** 

PLAY [localhost] **************************************************************** 

TASK: [debug msg="{{item}}"] ************************************************** 
ok: [localhost] => (item={'key': 'key2', 'value': 'value2'}) => {
    "item": {
        "key": "key2", 
        "value": "value2"
    }, 
    "msg": "{'value': 'value2', 'key': 'key2'}"
}
ok: [localhost] => (item={'key': 'key1', 'value': 'value1'}) => {
    "item": {
        "key": "key1", 
        "value": "value1"
    }, 
    "msg": "{'value': 'value1', 'key': 'key1'}"
}

PLAY RECAP ******************************************************************** 
localhost                    : ok=1    changed=0    unreachable=0    failed=0   

(rc=0)$

Trick is to wrap dict declaration with double quotes. Ansible applies this undocumented (but consistant) and crappy translation (ansible's equivalent of shell variable expantion) to most (not all) YAML values (everything RHS of ':') in the playbook. It is some combination putting these strings through Jinja2-engine, Python-interpreter and ansible-engine in some unknown order.

Upvotes: 35

Related Questions