我需要登录Oracle Linux系统并以root身份运行供应商应用程序的命令。测试SSH并手动运行流程后,我现在使用Ansible进行编排。
我必须使用证书以另一个用户“admin”身份登录,然后根据供应商的说明,在我运行shell脚本之前键入“sudo -s”成为root用户。因为我使用的是证书,所以我不需要输入密码来提升
我发现“成为:是”不适用于“sudo -s”。
我已经尝试使用“原始”,“shell”和“命令”ansible模块来绕过这个问题,但是他们可能会超时,因为他们可能会期待一个答案。
这是输出
TASK [run bash system status] **************************************************
fatal: [server-sandpit]: FAILED! => {"changed": true, "msg": "non-zero return code", "rc": 3, "stderr": "Shared connection to server-sandpit closed.\r\n", "stderr_lines": ["Shared connection to server-sandpit closed."], "stdout": "This script cannot be run under sudo, try running under 'sudo -s'\r\n", "stdout_lines": ["This script cannot be run under sudo, try running under 'sudo -s'"]}
这是剧本:
---
- hosts: sl-aio-sandpit
tasks:
- name: run bash system status
script: system_status.sh
args:
executable: bash
become: yes
请注意,我无法在ssh中仅使用sudo运行脚本:
[admin@server-sandpit tmp]$ sudo ./system_status.sh
This script cannot be run under sudo, try running under 'sudo -s'
我要么得到上面的错误,要么使用“shell”或“command”这样的解决方法,剧本永远不会解决,需要取消。
当使用become: true
和become_method: sudo
(默认)时,我不知道有任何方法可以控制传递给sudo的选项。
由于您首先需要sudo -s
然后在第二个命令中运行您的脚本,您可以尝试以下解决方案
---
- hosts: sl-aio-sandpit
remote_user: admin
tasks:
- name: copy script to remote machine
copy:
src: system_status.sh
dest: /path/to/your/system_status.sh
mode: 0750
- name: run bash system status with sudo -s
shell: |
sudo -s
/path/to/your/system_status.sh