0

In the below task:

- name: Create a data container
  docker_container:
    name: mydeploycontainer
    image: 1111112222.dkr.ecr.us-east-1.amazonaws.com/someteam/app-deploy:v.1
    env:
      name1: {{name1_var}}
      name2: {{name2_var}}
      name3: {{name3_var}}

env: section only allows value with double quotes, something like:

    env:
      name1: "value1"

But these are dynamic values based on the inputs passed to playbook:

--extra-vars '{ "name1_var": "value_1", "name2_var": "value_2", "name3_var": "value_3" }'


How to pass variable to environment section?

  • 1
    What error do you get with ansible? – muru Aug 7 at 15:15
1

TLDR;

All jinja2 template expressions must be quoted in your playbook when they start with double curly braces else you will get a syntax error (Which is what you get I guess)

You should write your task like the following to clear your error and set your envs to your passed extra vars:

- name: Create a data container
  docker_container:
    name: mydeploycontainer
    image: 1111112222.dkr.ecr.us-east-1.amazonaws.com/someteam/app-deploy:v.1
    env:
      name1: "{{ name1_var }}"
      name2: "{{ name2_var }}"
      name3: "{{ name3_var }}"

As a good practice, you should always quote your strings when they contain jinja2 templating expressions even when they don't start with double curly braces (e.g. path: "/fix/path/{{ var_file_name }}")

POC

Here is a proof of concept I wrote and played with ansible 2.8.1 to show you it works as expected. To replay, make sure you pip install jmespath as I used json_query for debugging values.

The test.yml playbook:

---
- name: Test passing vars to docker from command line
  hosts: localhost
  gather_facts: false

  tasks:

    - name: Spin up a container
      docker_container:
        state: started
        image: busybox:latest
        command: sh -c "while true; do sleep 1000; done"
        name: test_for_so
        env:
          oneenv: "{{ oneenv_value }}"
          otherenv: "{{ otherenv_value }}"

    - name: Get env values from container
      command: >-
        docker exec test_for_so sh -c 'echo ${{ item }}'
      register: env_out
      loop:
        - oneenv
        - otherenv

    - name: Display collected values
      debug:
        msg: "{{ env_out.results | json_query('[].{env: item, value: stdout}') }}"

The command:

ansible-playbook test.yml \
  -e '{"oneenv_value": "This is value 1", "otherenv_value": "This is value 2"}'

And the result:

PLAY [Test passing vars to docker from command line] **********************************************************************************************************************************************************************

TASK [Spin up a container] ************************************************************************************************************************************************************************************************
changed: [localhost]

TASK [Get env values from container] *************************************************************************************************************************************************************************************
changed: [localhost] => (item=oneenv)
changed: [localhost] => (item=otherenv)

TASK [Display collected values] *******************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        {
            "env": "oneenv",
            "value": "This is value 1"
        },
        {
            "env": "otherenv",
            "value": "This is value 2"
        }
    ]
}

PLAY RECAP ****************************************************************************************************************************************************************************************************************
localhost                  : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Counter POC

If you remove the quotes around the jinja2 templates strings in the env section (as in your example), this is what happens:

ERROR! Syntax Error while loading YAML.
  found unacceptable key (unhashable type: 'AnsibleMapping')

The error appears to be in '/home/user/test.yml': line 15, column 20, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

        env:
          oneenv: {{ oneenv_value }}
                   ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:

    with_items:
      - {{ foo }}

Should be written as:

    with_items:
      - "{{ foo }}"
  • Can I get a reason for the downvote ? – Zeitounator Aug 9 at 14:24
  • Doubled checked and added a proof of concept playbook and run. Unless I totally missed the question, it looks like it totally does what is expected. – Zeitounator Aug 9 at 15:08
  • Your example doesn't prove what you say (you say it won't work unless it's quoted, but there's no unquoted instance in your example to show that it doesn't work unless quoted). And in Ansible docs, you can find instances of unquoted usage. Example: docs.ansible.com/ansible/latest/user_guide/… – muru Aug 9 at 15:23
  • @muru: is it clearer like this ? – Zeitounator Aug 9 at 15:33
  • 1
    Yes, that's better. It'll be even better if you mention which version of ansible you tested with. – muru Aug 9 at 15:36
0

This might do what you want (ansible 2.8)

-e 'name1_var=""testa"" name2_var=""testb"" name1_var3=""testc""'
  • What about name1: "{{name1_var}}"? – overexchange Aug 7 at 15:11
  • @user103944 I made the same mistake as you when overlooking the question... passing variables is not the issue here and works correctly with json. The problem is the wrong syntax for templating those vars in the playbook. See my answer. – Zeitounator Aug 8 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.