Search code examples
linuxansiblevmware

Become su when using vmware_vm_shell module to configure Linux VM using ansible?


I have a working ansible playbook which uses vmware_vm_shell module to push network configuration commands to a Linux VM running in VMware. This only works if I use the vm_username as 'root'. I want to use a different user other than 'root' and become 'su' to run network configuration cli commands. Please refer to the working ansible playbook below:

  - 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'"

I was unsuccessful when using a different username, it was unable to issue those commands since I need to have root privilege (I need to become su using that different username). Is there a way to become su when using a different user with vmware_vm_shell module?


Solution

  • Turning the comment into an answer:

    If the user is allowed to execute those sudo commands w/o an extra password, you should be able to replace vm_shell: "/bin/nmcli" with vm_shell: "/path/to/sudo" (for me it's /usr/bin/sudo and add network mangler to the arguments vm_shell_args: "/bin/nmcli {{ item }}".