Ansible脚本为何看到不同于SSH的环境?

huangapple go评论72阅读模式
英文:

Why ansible script sees different environment than SSH?

问题

我创建了一个简单的check.py脚本:

import getpass
import sys
import os

print(getpass.getuser())
print(sys.executable)
print(os.popen("which python3").read())
print(os.popen("cmake --version").read())

我在mac机器上使用ansible运行这个脚本并读取输出。Ansible显示的输出如下:

administrator
/usr/local/bin/python3
/usr/bin/python3
/bin/sh: cmake: command not found

但是,如果我使用相同的管理员帐户通过SSH连接到机器,同样的脚本返回:

administrator
/usr/local/bin/python3
/usr/local/bin/python3 # 不同!
cmake version 3.25.2 # 不同!

为什么os.popen的输出存在差异,以及如何说服ansible/python返回与SSH中看到的相同的输出?

以下是我的ansible文件:

ansible.cfg:

[defaults]
inventory = hosts
host_key_checking = False
action_warnings = False
deprecation_warnings = False

main.yml:

- name: Run a check script
  ansible.builtin.script: check.py
  args:
    executable: /usr/local/bin/python3
  register: output
- debug: var=output.stdout_lines

group_vars\all.yml:

ansible_user: administrator
ansible_password: admin_password
ansible_become:
ansible_become_user: root
ansible_become_pass: root_password
workdir: /Users/Shared/Jenkins
node_type: bare_metal
env: production
英文:

I created simple check.py script:

import getpass
import sys
import os

print(getpass.getuser())
print(sys.executable)
print(os.popen("which python3").read())
print(os.popen("cmake --version").read())

I use ansible the script inside of mac machine and read output. Ansible shows output:

administrator
/usr/local/bin/python3
/usr/bin/python3
/bin/sh: cmake: command not found

... but if I SSH to the machine with the same administrator account, the same script returns:

administrator
/usr/local/bin/python3
/usr/local/bin/python3 # different!
cmake version 3.25.2 # different!

Why is there a difference in os.popen output and how can I persuade ansible/python to return the same output as I see in the SSH?

Here are my ansible files:

ansible.cfg:

[defaults]
inventory = hosts
host_key_checking = False
action_warnings = False
deprecation_warnings = False

main.yml:

- name: Run a check script
  ansible.builtin.script: check.py
  args:
    executable: /usr/local/bin/python3
  register: output
- debug: var=output.stdout_lines

group_vars\all.yml:

ansible_user: administrator
ansible_password: admin_password
ansible_become:
ansible_become_user: root
ansible_become_pass: root_password
workdir: /Users/Shared/Jenkins
node_type: bare_metal
env: production

答案1

得分: 0

我在Python脚本中使用了print(os.environ)行来查找ansible和ssh环境之间的差异。然后我在main.yml中添加了环境设置来同步路径:

- name: 运行检查脚本
  ansible.builtin.script: check.py
  args:
    executable: /usr/local/bin/python3
  register: output
  environment:
    PATH: /usr/local/bin:{{ ansible_env.PATH }}:/Applications/CMake.app/Contents/bin
- debug: var=output.stdout_lines
英文:

I used print(os.environ) line in python script to find out differences between ansible and ssh environment. Then I added environment setting to main.yml to synchronize paths:

- name: Run a check script
  ansible.builtin.script: check.py
  args:
    executable: /usr/local/bin/python3
  register: output
  environment:
    PATH: /usr/local/bin:{{ ansible_env.PATH }}:/Applications/CMake.app/Contents/bin
- debug: var=output.stdout_lines

huangapple
  • 本文由 发表于 2023年5月10日 19:42:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定