Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible variable in key/value key

I'm passing env variables to a Docker container in an ansible playbook, how do I set an Ansible variable in the key in the key/value of an env?

So this:

- name: webproxy container
  docker_container:
    name: "webproxy"
    image: "webproxy"
    env:
      SERVICE_443_NAME: "webproxy"

becomes this:

- name: webproxy container
  docker_container:
    name: "webproxy"
    image: "webproxy"
    env:
      SERVICE_{{ port_number }}_NAME: "webproxy"
like image 876
Alex Laverty Avatar asked Nov 08 '17 05:11

Alex Laverty


People also ask

What ansible variable type contains key value pairs?

The key part in the key=value pairs will be converted into lowercase inside the ansible_local variable.

How do you pass variables in ansible?

The easiest way to pass Pass Variables value to Ansible Playbook in the command line is using the extra variables parameter of the “ansible-playbook” command. This is very useful to combine your Ansible Playbook with some pre-existent automation or script.

How do I add VARS to ansible playbook?

The include_vars module can be used in a playbook or role to load variables from a file. Simply set the value of include_vars to a local file to load the variables it contains: --- # ./hello_world. yml - name: print greeting hosts: "*" tasks: - include_vars: name_vars.


1 Answers

Use JSON notation to define a dictionary with environment variables:

- name: webproxy container
  docker_container:
    name: "webproxy"
    image: "webproxy"
    env: '{ "SERVICE_{{ port_number }}_NAME": "webproxy" }' 
like image 168
techraf Avatar answered Oct 20 '22 11:10

techraf