Saturday, November 5, 2022

[FIXED] How to use environment variable for Ansible shell's creates?

Issue

I am using the built-in shell module, and want to use an environment variable inside the creates arg.

Something like the following:

- name: Run pyenv install of Python 3.10.7
  shell:
    cmd: |
      exec $SHELL
      pyenv install 3.10.7
    # Is something like this possible?
    creates: $PYENV_ROOT/versions/3.10.7

My questions are:

  • Is something like this possible?
  • Would it expand the environment variable from the Ansible control node or Ansible target?

Solution

Possible and the ansible way of doing this would be

- name: Run pyenv install of Python 3.10.7
  shell:
    cmd: |
      exec "{{ ansible_env.SHELL }}"
      pyenv install 3.10.7
    creates: "{{ ansible_env.PYENV_ROOT }}/versions/3.10.7"

In this case, the variable will be from the managed node (target). Provided gather_facts is set to true.

To use the values from the control node, use lookup

    creates: "{{ lookup('env','PYENV_ROOT') }}/versions/3.10.7"


Answered By - franklinsijo
Answer Checked By - Marilyn (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.