我有一个可用的 ansible 剧本,它使用 vmware_vm_shell module将网络配置命令推送到在 VMware 中运行的 Linux VM。仅当我使用 vm_username 作为'root'时,这才有效。我想使用“root”以外的其他用户并成为“su”来运行网络配置 cli 命令。请参阅下面的工作 ansible 剧本:
- name: Edit Network Interfaces for VM
vmware_vm_shell:
hostname: "{{ vcenter_hostname }}"
username: "{{ vcenter_username }}"
password: "{{ vcenter_password }}"
validate_certs: "{{ vcenter_validate_certs }}"
datacenter: "{{ vcenter_datacenter }}"
folder: "{{ vcenter_destination_folder }}"
vm_id: VM-Test
vm_username: root
vm_password: "{{ vm-password }}"
vm_shell: "/bin/nmcli"
vm_shell_args: "{{ item }}"
with_items:
- "con mod ens192 ipv4.address '{{ vmintip }}'"
- "con mod ens192 ipv4.gateway '{{ intup }}'"
- "con mod ens192 ipv4.dns '{{ dnsip }}'"
- "con mod ens192 ipv4.method manual"
- "con mod ens192 ipv6.method disabled"
- "con up ens192"
- "con mod 'Wired connection 1' ipv4.address '{{ vmmgip}}'"
- "con mod 'Wired connection 1' ipv4.gateway '{{ mgup }}'"
- "con mod 'Wired connection 1' ipv4.dns '{{ dnsip }}'"
- "con mod 'Wired connection 1' ipv4.method manual"
- "con mod 'Wired connection 1' ipv6.method disabled"
- "con up 'Wired connection 1'"
当使用不同的用户名时,我失败了,它无法发出这些命令,因为我需要拥有root权限(我需要使用不同的用户名成为su)。当使用不同的用户和 vmware_vm_shell 模块时,有没有办法变成 su ?
最佳答案
将评论变成答案:
如果允许用户在没有额外密码的情况下执行这些 sudo 命令,您应该能够将 vm_shell: "/bin/nmcli"
替换为 vm_shell: "/path/to/sudo"
(对我来说是/usr/bin/sudo 并将网络管理器添加到参数vm_shell_args: "/bin/nmcli {{ item }}"
。
关于linux - 使用vmware_vm_shell模块使用ansible配置Linux VM时变成su?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75595841/