Search code examples
ansiblevcenter

Configure VyOS vm with ansible vmware_shell


I am trying to configure a VyOS vm that I"ve built from a template. The template is a fresh install without any configuration.

The vm doesn't have an IP configured, so I can't use the ssh options or the vyos ansible module. So I'm trying to use the vmware_vm_shell module, which will let me execute commands but I can't enter conf mode for VyOS.

I've tried bash and vbash for my shell. I've tried setting the conf commands to environment vars to execute, I've tried with_item but it doesn't seem that will work with vmware_vm_shell.

The bear minimum I need is to configure an IP address so that I can then ssh or use the vyos ansible module to complete the configuration.

conf
set interfaces ethernet eth0 address 192.168.1.251/24
set service ssh port 22
commit
save
---
- hosts: localhost
  gather_facts: no
  connection: local
  vars:
    vcenter_hostname: "192.168.1.100"
    vcenter_username: "[email protected]"
    vcenter_password: "SekretPassword!"
    datacenter: "Datacenter"
    cluster: "Cluster"
    vm_name: "router-01"
  tasks:
    - name: Run command inside a virtual machine
      vmware_vm_shell:
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        datacenter: "{{ datacenter }}"
        validate_certs: False
        vm_id: "{{ vm_name }}"
        vm_username: 'vyos'
        vm_password: 'abc123!!!'
        vm_shell: /bin/vbash
        vm_shell_args: 'conf 2> myFile'
        vm_shell_cwd: "/tmp"
      delegate_to: localhost
      register: shell_command_output

This throws the error:

/bin/vbash: conf: No such file or directory

Solution

  • I have an EdgeRouter, which uses a Vyatta-derived operating system. The issue you're having is caused by the fact that conf (or configure for me) isn't actually the name of a command. The cli features are implement through a complex collection of bash functions that aren't loaded when you log in non-interactively.

    There is a wiki page on vyos.net that suggests a solution. By sourcing in /opt/vyatta/etc/functions/script-template, you prepare the shell environment such that vyos commands will work as expected.

    That is, you need to execute a shell script (with vbash) that looks like this:

    source /opt/vyatta/etc/functions/script-template
    
    conf
    set interfaces ethernet eth0 address 192.168.1.251/24
    set service ssh port 22
    commit
    save
    
    exit
    

    I'm not familiar with the vmware_vm_shell module, so I don't know exactly how you would do that, but for example this works for me to run a single command:

    ssh ubnt@router 'vbash -c  "source /opt/vyatta/etc/functions/script-template
    configure
    show interfaces
    "'
    

    Note the newlines in the above. That suggests that this might work:

        - name: Run command inside a virtual machine
          vmware_vm_shell:
            hostname: "{{ vcenter_hostname }}"
            username: "{{ vcenter_username }}"
            password: "{{ vcenter_password }}"
            datacenter: "{{ datacenter }}"
            validate_certs: False
            vm_id: "{{ vm_name }}"
            vm_username: 'vyos'
            vm_password: 'abc123!!!'
            vm_shell: /bin/vbash
            vm_shell_cwd: "/tmp"
            vm_shell_args: |-
              -c "source /opt/vyatta/etc/functions/script-template
              configure
              set interfaces ethernet eth0 address 192.168.1.251/24
              set service ssh port 22
              commit
              save"
    
          delegate_to: localhost
          register: shell_command_output