Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...