PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, November 5, 2022

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

 November 05, 2022     ansible, environment-variables     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing