我目前正在研究 ansible 自动化以在远程主机上运行任务。我已经完成了所有准备工作,例如 SSH 配置,并且一切都运行正常,直到我遇到显示非法指令 /usr/bin/python 的问题。我怀疑这可能是由远程主机上的环境引起的,因此我将以下命令放在文件 /etc/bashrc 上:
"load /etc/bashrc
----SOME----
----ORIGINAL----
----CONFIGURATION----
export PATH=/usr/bin/pyhon:$PATH
echo "${PATH}"
我测试了它并且它有效,但是,我仍然遇到问题,这意味着我的问题仍然存在。
我的源错误显示如下:
Using module file /usr/lib/python2.7/site-packages/ansible/modules/system/setup.py
<10.129.145.65> ESTABLISH SSH CONNECTION FOR USER: appuser
<10.129.145.65> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o User=appuser -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/59a8ca37f8 10.129.145.65 '/bin/sh -c '"'"'sudo -H -S -n-u root /bin/sh -c '"'"'"'"'"'"'"'"'echo BECOME-SUCCESS-wlljicrcltceaiewolenureccrzbsuoy; /usr/bin/python'"'"'"'"'"'"'"'"' && sleep 0'"'"''
Escalation succeeded
<10.129.145.65> (132, 'load /etc/bashrc\n/usr/bin/python:/usr/local/bin:/usr/bin\n', '/bin/sh: line 1: 13008 Illegal instruction /usr/bin/python\n')
fatal: [10.129.145.65]: FAILED! => {
"changed": false,
"module_stderr": "/bin/sh: line 1: 13008 Illegal instruction /usr/bin/python\n",
"module_stdout": "load /etc/bashrc\n/usr/bin/python:/usr/local/bin:/usr/bin\n",
"msg": "MODULE FAILURE",
"rc": 132
}
here I just want to update more about my question and hopefully this can clarify my issue better:
my ansible version is 2.5.0 and python version is 2.7.15
below is my root task:
---
- name: connect to docker build server
hosts: "hostname1"
remote_user: appuser
# hosts: 127.0.0.1
become: yes
become_user: root
vars_files:
- group_vars/{{docker_env}}/all.yml
roles:
- { role: commit-push-docker-images, tags: ['commit-push-docker-images'] }
vars_files contains all variables that are used in my task;
Following code comes from roles : commit-push-docker-images
---
- debug: msg="start to commit docker image and push to harbor"
- name: get json from remote
uri:
url: "http://URL_FOR_GETTING_JSON_OBJECT"
user: "USER_NAME"
password: "USER_PASSWORD"
method: GET
body_format: json
register: json_response
... ( following with some other tasks)
if I try to run my code on 127.0.0.1 and it works!! but if I try to connect to remote host called hostname1, which will incur the error I previouly rendered. What I posted before is the result that I run with -vvv.
Plz tell me if I provided enough info for my question. Thanks a lot!!!
答案1
我们需要分开来看:
- 当你向 $PATH 添加某些内容时,它始终是文件夹,而不是可执行文件。因此,从 bashrc 中删除“export PATH=/usr/bin/pyhon:PATH”
- /usr/bin 默认应该在你的 $PATH 中,所以你不需要添加 bashrc
- 始终包含你的 Ansible 版本和剧本的代码片段
如果您需要指定 Python 可执行文件的自定义路径,Ansible 有一个您可以设置的变量:
ansible_python_interpreter=/mycustomdir/python
这可以进入特定的 host_vars 或 group_vars,或者如果你的所有主机都有这个自定义路径,它可以进入 group_vars/all
如果您已设置该变量但仍然不起作用,请将生成错误的任务复制到单独的剧本中,并使用 -vvvv 运行它。将结果添加到您的帖子中。
在您的 Ansible 控制器上,在 shell 中执行“export ANSIBLE_KEEP_REMOTE_FILES=1”并再次运行测试剧本。这会将 python 文件留在远程主机上。然后尝试运行留在文件夹 ~/.ansible/tmp/(您的 ansible 用户的主文件夹)中的 python 文件。Python 将崩溃,您可以获得更好的错误消息,或者在最坏的情况下使用 gdb 进行调试。