Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting and using environment variable in Ansible

I want to set one value as an environment variable in Ansible and then use it another playbook. Below is my playbook:

get_cmd.yaml

[root@a6296ab33a34 test_code]# vi get-cwd.yaml 

- hosts: localhost
  connection: local
  gather_facts: False

  tasks:

  #- name: Get directory
  #  shell: export ACWD="{{ playbook_dir }}"
  #  when: platform == 'jenkins'

  - name: Get CWD
    shell: "export ACWD=/test_code_demo"
    when: platform != 'jenkins'

  - name: DEMO
    shell: echo $ACWD

Output

[root@a6296ab33a34 test_code]# vi get-cwd.yaml 
[root@a6296ab33a34 test_code]# ansible-playbook get-cwd.yaml --extra-vars="@deploy-vars.yaml" -vv
 [WARNING] Ansible is being run in a world writable directory (/test_code), ignoring it as an ansible.cfg source. For more information see https://docs.ansible.com/ansible/devel/reference_appendices/config.html#cfg-in-world-writable-dir
ansible-playbook 2.8.4
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 2.7.5 (default, Jun 20 2019, 20:27:34) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
Using /etc/ansible/ansible.cfg as config file
 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


PLAYBOOK: get-cwd.yaml *********************************************************************************************************************************************************************************************************************
1 plays in get-cwd.yaml

PLAY [localhost] ***************************************************************************************************************************************************************************************************************************
META: ran handlers

TASK [Get CWD] *****************************************************************************************************************************************************************************************************************************
task path: /test_code/get-cwd.yaml:11
changed: [localhost] => {"changed": true, "cmd": "export ACWD=/test_code_demo", "delta": "0:00:00.713703", "end": "2019-12-13 14:43:37.054390", "rc": 0, "start": "2019-12-13 14:43:36.340687", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}

TASK [DEMO] ********************************************************************************************************************************************************************************************************************************
task path: /test_code/get-cwd.yaml:15
changed: [localhost] => {"changed": true, "cmd": "echo $ACWD", "delta": "0:00:00.705605", "end": "2019-12-13 14:43:37.919962", "rc": 0, "start": "2019-12-13 14:43:37.214357", "stderr": "", "stderr_lines": [], "stdout": "/test_code_dinesh", "stdout_lines": ["/test_code_dinesh"]}
META: ran handlers
META: ran handlers

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

[root@a6296ab33a34 test_code]#

You can see, though I have tried to set the value to test_code_demo, still old value test_code_dinesh is reflecting.

Please let me know way to resolve the above issue.

like image 927
Dinesh Pundkar Avatar asked Feb 14 '26 16:02

Dinesh Pundkar


1 Answers

Remember that when you set an environment variable (anywhere, not just in Ansible), it only effects the current process and its children.

When you run something like this:

  - name: Get CWD
    shell: "export ACWD=/test_code_demo"
    when: platform != 'jenkins'

You are:

  1. Spawning a shell
  2. Setting the environment variable ACWD in that shell
  3. Exiting the shell

At this point, the environment is destroyed. There's no way to set an environment variable in one task and have it effect another task. You can set per-task environment variables using the environment key on your task, like this:

  - name: DEMO
    shell: echo $ACWD
    environment:
      ACWD: '/test_code_demo'

If you need to apply the environment setting to multiple tasks, you can set it on a play instead:

- hosts: localhost
  environment:
    ACWD: '/test_code_demo'
  tasks:
    - command: 'echo $ACWD'
      register: output1

    - command: 'echo $ACWD'
      register: output2

    - debug:
        msg:
          - "{{ output1.stdout }}"
          - "{{ output2.stdout }}"
like image 184
larsks Avatar answered Feb 16 '26 16:02

larsks