【发布时间】:2021-04-02 22:01:18
【问题描述】:
我有一个简单的 ansible 测试手册可以在 locahost 和一个远程主机上运行。
该剧本运行需要以 root 身份运行的 linux 命令“pvs”。
我在 ansible.cfg 中变成了=true。
在 localhost 上,用户 test1 在 /etc/sudoers 中配置为“ugly” ALL=(ALL:ALL) NOPASSWD: ALL - 因为显然没有其他解决方案可以在不提供密码的情况下以批处理模式以 root 身份启动命令,正如 Vladimir 所证实的那样:Restrict Ansible script module using sudoers on the remote node
(我可以做 Cmnd_Alias,但最后,ansible 需要 /bin/sh .. 所以没有必要用其他命令来限制它)
而且,当我运行下面的剧本时,我还必须在远程机器上配置相同的 sudoers 配置。
这是否意味着,在我们管理的所有机器上,我们需要让这个“ansible”用户通过 sudo 获得 root 访问权限?我希望不会,否则这是一个非常大的安全问题..
您有什么建议的解决方法或想法可以与我分享以限制 root 访问权限?
我的 ansible.cfg :
[privilege_escalation]
become = true # (this I need otherwise command "pvs" will not run)
become_method = sudo
become_user = root
become_ask_pass = false # (this also I need, do not want any manuel providing)
default_become = true
剧本:
1 ---
2 - name: test remote machine connection
3 #hosts: localhost
4 hosts: all
5 gather_facts: no
6
7 tasks:
8 - name: get physical volumes
9 #shell: pvs # OK for localhost, but NOT for remote host, as sudo is not configured
10 #command: # Same as above for remote host : "Missing sudo password"
11 #cmd: pvs
12 raw: pvs # Same as above for remote host : "Missing sudo password"
13 register: output
14
15 - name: Show output
16 debug:
17 msg: "{{ output.stdout }}"
18
结果:
PLAY [test remote machine connection] *************************************************************************************
TASK [get physical volumes] *******************************************************************************************************
fatal: [sh2]: FAILED! => {"msg": "Missing sudo password"}
changed: [localhost]
TASK [Show output] ********************************************************************************************************
ok: [localhost] => {
"msg": " PV VG Fmt Attr PSize PFree \n /dev/xvdc prdvvg lvm2 a-- <10.00g <2.00g\n /dev/xvde prdvvg lvm2 a-- <10.00g <10.00g\n /dev/xvdf clone_prdtm1vg lvm2 a-- <10.00g <2.00g\n /dev/xvdg clone_prdtm1vg lvm2 a-- <10.00g <10.00g\n"
}
PLAY RECAP ****************************************************************************************************************
localhost : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
sh2 : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
谢谢
【问题讨论】:
-
如果您希望
ansible执行需要root访问权限的任务,那么ansible需要root 权限,通过sudo或通过ssh帐户的适当ssh凭据.您不能将 Ansible 限制为特定命令,因为 Ansible 没有运行特定命令;它正在运行(通常)python。如果您需要更精细的访问控制,则需要查看其他工具(或者限制自己使用 ansible 的raw模块,我猜)。 -
感谢 larsks,但是很抱歉,我向您展示了即使原始模块也不起作用。我相信这个问题是 RedHat Ansible 应该改进的,因为他们吹嘘 Ansible 是自动化的瑞士刀......但可能很危险。当然,在管理一大群服务器时,我们需要 root 才能传递一些命令,但我认为 Ansible 会提供更多的安全性......无论如何,谢谢。