kbang
kbang

Reputation: 704

Ansible not setting environment variable correctly

I trying to set an environment variable for an ansible play. Based on this, I should be able to do something like:

---
- hosts: localhost
  connection: local
  environment:
    test_var: "a vault here"
  tasks:
    - debug:
        msg: "Test var is: {{lookup('env', 'test_var')}}"
    - debug:
        msg: "Home is here {{lookup('env', 'HOME')}}"

But I am clearly missing something or encountered a bug?

osboxes@osboxes:~$ ansible-playbook --version
ansible-playbook 2.9.6
<snip...>
osboxes@osboxes:~$ ansible-playbook test.yaml
<snip...>
TASK [debug] ****************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "Test var is: "    # where is the env variable?
}

TASK [debug] ****************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "Home is here /home/osboxes"   
}

Upvotes: 3

Views: 2364

Answers (1)

P....
P....

Reputation: 18351

When you set env using environment plugin, you do not affect the ansible session which is already running.

You would have to do as follow:

---
- hosts: localhost
  connection: local
  environment:
    test_var: "a vault here"
  tasks:
    - shell: echo $test_var
      register: test_var

    - debug:
        msg: "Test var is: {{ test_var.stdout }}"
    - debug:
        msg: "Home is here {{lookup('env', 'HOME')}}"

The above playbook would return the following output:

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

TASK [Gathering Facts] **********************************************************************************************************************************************
ok: [localhost]

TASK [shell] ********************************************************************************************************************************************************
changed: [localhost]

TASK [debug] ********************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "Test var is: a vault here"
}

TASK [debug] ********************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "Home is here /home/ps"
}

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

Note: See this link to official documents.

When you set a value with environment: at the play or block level, it is available only to tasks within the play or block that are executed by the same user. The environment: keyword does not affect Ansible itself, Ansible configuration settings, the environment for other users, or the execution of other plugins like lookups and filters. Variables set with environment: do not automatically become Ansible facts, even when you set them at the play level. You must include an explicit gather_facts task in your playbook and set the environment keyword on that task to turn these values into Ansible facts.

Important points to highlight in the above text:

The environment: keyword does not affect Ansible itself, Ansible configuration settings, the environment for other users, or the execution of other plugins like lookups and filters

Upvotes: 2

Related Questions