Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to define path variable in playbook #14959

Closed
yoshiwaan opened this issue Mar 14, 2016 · 8 comments
Closed

Unable to define path variable in playbook #14959

yoshiwaan opened this issue Mar 14, 2016 · 8 comments
Labels
affects_2.0 This issue/PR affects Ansible v2.0 bug This issue/PR relates to a bug. needs_info This issue requires further information. Please answer any outstanding questions. needs_template This issue/PR has an incomplete description. Please fill in the proposed template correctly.

Comments

@yoshiwaan
Copy link

Issue Type:

Bug Report

Ansible Version:
[vagrant@localhost vagrant]$ ansible --version
/usr/lib64/python2.6/site-packages/Crypto/Util/number.py:57: PowmInsecureWarning: Not using mpz_powm_sec.  You should rebuild using libgmp >= 5 to avoid timing attack vulnerability.
  _warn("Not using mpz_powm_sec.  You should rebuild using libgmp >= 5 to avoid timing attack vulnerability.", PowmInsecureWarning)
ansible 2.0.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
Ansible Configuration:

Nothing

Environment:

CentOS 6.7 on Vagrant

Summary:

On previous versions of Ansible I would use a statement like the following in a playbook to define a global path change for a playbook run.

environment:
      PATH: "/usr/local/bin:{{ ansible_env.PATH }}"

In Ansible 2.0.1.0 this behaviour broke, which is supposedly 'working as intended' according to #14655.

Using the suggested fix from #14655 I changed my statement to:

environment:
      PATH: "/usr/local/bin:{{ (ansible_env|default({})).PATH|default('') }}"

Which is supposed to create a default initilisation of the variable if it's unset, so that things don't break in the setup task. However this causes issue for other modules, like yum.

Steps To Reproduce:

On a Centos or RHEL box:

testplay.yml


---
  - hosts: localhost
    connection: local
    environment:
      PATH: "/usr/local/bin:{{ (ansible_env|default({})).PATH|default('') }}"
    tasks:
      - name: yum update
        yum:
          name: '*'
          state: latest

Then run

sudo ansible-playbook -vvvv testplay.yml

If you comment out the PATH line then the play works.

Expected Results:

The yum module would run and update packages

Actual Results:
TASK [yum update] **************************************************************
task path: /vagrant/testplay.yml:8
ESTABLISH LOCAL CONNECTION FOR USER: vagrant
127.0.0.1 EXEC /bin/sh -c '( umask 22 && mkdir -p "` echo $HOME/.ansible/tmp/ansible-tmp-1457973532.38-261134906018159 `" && echo "` echo $HOME/.ansible/tmp/ansible-tmp-1457973532.38-261134906018159 `" )'
127.0.0.1 PUT /tmp/tmp2A3k_q TO /home/vagrant/.ansible/tmp/ansible-tmp-1457973532.38-261134906018159/yum
127.0.0.1 EXEC /bin/sh -c 'LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 PATH=/usr/local/bin:/usr/local/bin: /usr/bin/python -tt /home/vagrant/.ansible/tmp/ansible-tmp-1457973532.38-261134906018159/yum; rm -rf "/home/vagrant/.ansible/tmp/ansible-tmp-1457973532.38-261134906018159/" > /dev/null 2>&1'
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
  File "/home/vagrant/.ansible/tmp/ansible-tmp-1457973532.38-261134906018159/yum", line 3795, in <module>
    main()
  File "/home/vagrant/.ansible/tmp/ansible-tmp-1457973532.38-261134906018159/yum", line 1028, in main
    disablerepo, disable_gpg_check, exclude, repoquery)
  File "/home/vagrant/.ansible/tmp/ansible-tmp-1457973532.38-261134906018159/yum", line 951, in ensure
    res = latest(module, pkgs, repoq, yum_basecmd, conf_file, en_repos, dis_repos)
  File "/home/vagrant/.ansible/tmp/ansible-tmp-1457973532.38-261134906018159/yum", line 762, in latest
    rc, out, err = module.run_command(yum_basecmd + ['check-update'])
  File "/home/vagrant/.ansible/tmp/ansible-tmp-1457973532.38-261134906018159/yum", line 2806, in run_command
    args = [ os.path.expandvars(os.path.expanduser(x)) for x in args ]
  File "/usr/lib64/python2.6/posixpath.py", line 251, in expanduser
    if not path.startswith('~'):
AttributeError: 'NoneType' object has no attribute 'startswith'

fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "invocation": {"module_name": "yum"}, "parsed": false}

Any time the yum module invokes yum to install or upgrade a module I get this error with the suggested PATH settings above (if the current state of the yum package is correct then there's no error)

@jimi-c jimi-c added this to the stable-2.0 milestone Mar 18, 2016
@bcoca
Copy link
Member

bcoca commented Mar 19, 2016

try with this patch:

diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py
index fe96472..ab0be36 100644
--- a/lib/ansible/module_utils/basic.py
+++ b/lib/ansible/module_utils/basic.py
@@ -1546,6 +1546,8 @@ class AnsibleModule(object):
             if p not in paths and os.path.exists(p):
                 paths.append(p)
         for d in paths:
+            if not d:
+                continue
             path = os.path.join(d, arg)
             if os.path.exists(path) and is_executable(path):
                 bin_path = path

@yoshiwaan
Copy link
Author

Doesn't seem to make a difference for me. I'm on Centos and installed via epel yum package (/usr/lib/python2.6/site-packages/ansible/module_utils/basic.py) and pip (/usr/lib/python2.7/site-packages/ansible/module_utils/basic.py) on different systems. I made the patch to both and neither behaved differently using the test play I posted above.

I can't find anywhere else I could patch this so I'm guessing that's correct?

@yoshiwaan
Copy link
Author

If it's not just me patching incorrectly then I've found out some more info.

I changed the test play to the following (with or without the patch it's the same):

---
  - hosts: localhost
    connection: local
    environment:
      PATH: "/usr/local/bin:{{ (ansible_env|default({})).PATH|default('') }}"
    tasks:
      - name: debug
        debug: var=ansible_env
      - name: yum update
        yum:
          name: '*'
          state: latest

and get this result from the debug output

ok: [localhost] => {
    "ansible_env": {
        "HOME": "/root",
        "HOSTNAME": "1ba8bda64185",
        "LANG": "en_US.UTF-8",
        "LC_ALL": "en_US.UTF-8",
        "LC_MESSAGES": "en_US.UTF-8",
        "LS_COLORS": "rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:",
        "PATH": "/usr/local/bin:",
        "PWD": "/root",
        "SHLVL": "3",
        "TERM": "xterm",
        "_": "/usr/bin/python"
    }
}

The path is set to what I set in the play and nothing else. It's never going to find the yum binary if that's the case, so somehow this error is also masking some sort of file not found error (or that error is just not implemented for yum).

The suggested work around posted in #14655 of using a zero initialised ansible_env.PATH clearly doesn't work. By changing the logic of the setup module to allow definition of environment variables and by forcing us to specify a default initialisation of the ansible_env.PATH variable if we want a non default it's breaking the default initialisation. This is clearly a bug.

Either the setup task needs to define facts before looking at the environment section of the play or perhaps this was just not a good change to make in the first place.

@zonca
Copy link

zonca commented Aug 16, 2016

same issue here, using {{ (ansible_env|default({})).PATH|default('') }} at playbook level caused an error on the hostname module not being able to find the hostname command.

This workaround fixed that for me:
#14655 (comment)

zonca added a commit to zonca/jupyterhub-deploy-teaching that referenced this issue Aug 16, 2016
@bcoca bcoca modified the milestone: stable-2.0 Sep 6, 2016
@ansibot ansibot added the affects_2.0 This issue/PR affects Ansible v2.0 label Sep 8, 2016
@ansibot
Copy link
Contributor

ansibot commented Apr 11, 2017

@yoshiwaan Greetings! Thanks for taking the time to open this issue. In order for the community to handle your issue effectively, we need a bit more information.

Here are the items we could not find in your description:

  • component name

Please set the description of this issue with this template:
https://raw.githubusercontent.com/ansible/ansible/devel/.github/ISSUE_TEMPLATE.md

click here for bot help

@ansibot ansibot added needs_info This issue requires further information. Please answer any outstanding questions. needs_template This issue/PR has an incomplete description. Please fill in the proposed template correctly. labels Apr 11, 2017
@bcoca
Copy link
Member

bcoca commented Jun 23, 2017

this was fixed via df5895e

@bcoca bcoca closed this as completed Jun 23, 2017
@PickRelated
Copy link

@bcoca, looks like using {{ ansible_env.PATH }} erases everything else from PATH as of v2.3.1.0

- name: Echo path
  shell: echo $PATH
  environment:
    PATH: "/test:{{ ansible_env.PATH }}"

Results in /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

Whereas

- name: Echo path
  shell: echo $PATH
  environment:
    PATH: "/test"

Results in /test
Any advise, please?

@eduardobaitello
Copy link

@PickRelated I was experiencing the same problem.

The reason was that I have the gather_facts disabled in my Playbook.
Enabling gather_facts and using PATH: "/usr/local/bin:{{ ansible_env.PATH }}" works fine.

@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 7, 2018
@ansible ansible locked and limited conversation to collaborators Apr 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.0 This issue/PR affects Ansible v2.0 bug This issue/PR relates to a bug. needs_info This issue requires further information. Please answer any outstanding questions. needs_template This issue/PR has an incomplete description. Please fill in the proposed template correctly.
Projects
None yet
Development

No branches or pull requests

7 participants