2

I have a playbook where I am configuring environment variables for multiple hosts.

these are the global vars set in defaults\main.yml:

environment:
  http_proxy: blabla
  https_proxy: blabla

Now I have a single task where I need to set another environment variable for the python library. However, when I set the environment var for that single task it overwrites the global environment vars.

- name: some task
    command: command
    environment:
      ENV_VAR: "blabla"

I want the ENV_VAR for the single task to be added on top of the global vars. But is that even possible?

https://docs.ansible.com/ansible/latest/user_guide/playbooks_environment.html this page didn't give me anything conclusive.

Could I use the with_items option to achieve this possibly?

1 Answer 1

0

The following playbook demonstrates a possible implementation for your requirement using the combine filter associated with default. Run this with -v option to see output of shell commands:

---
- name: Parse several results as json strings
  hosts: localhost
  gather_facts: false
  # This will automatically combine `default_env` (default to empty if it does not exists)
  # with `more_env` if this latest var is defined somewhere
  environment: "{{ default_env | default({}) | combine(more_env | default({})) }}"

  vars:
    # Define default environment variables
    default_env:
      http_proxy: blabla
      https_proxy: blabla

  tasks:
    - name: Show some vars
      vars:
        # Inject more environment variables for this task
        more_env:
          toto: titi
      shell: |-
        echo $http_proxy
        echo $https_proxy
        echo $toto

    - name: Same with default env
      shell: |-
        echo $http_proxy
        echo $https_proxy
        echo $toto

Which gives:

$ ansible-playbook test.yml -v

PLAY [Default env override] ****************************************************************************************************************************************************************************************************************************

TASK [Show some vars] ***************************************************************************************************************************************************************************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "echo $http_proxy\necho $https_proxy\necho $toto", "delta": "0:00:00.002207", "end": "2020-01-07 09:57:30.178989", "rc": 0, "start": "2020-01-07 09:57:30.176782", "stderr": "", "stderr_lines": [], "stdout": "blabla\nblabla\ntiti", "stdout_lines": ["blabla", "blabla", "titi"]}

TASK [Same with default env] ********************************************************************************************************************************************************************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "echo $http_proxy\necho $https_proxy\necho $toto", "delta": "0:00:00.001875", "end": "2020-01-07 09:57:30.626163", "rc": 0, "start": "2020-01-07 09:57:30.624288", "stderr": "", "stderr_lines": [], "stdout": "blabla\nblabla", "stdout_lines": ["blabla", "blabla"]}

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.