英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论