【问题标题】:Configure VyOS vm with ansible vmware_shell使用 ansible vmware_shell 配置 VyOS 虚拟机
【发布时间】:2020-12-19 01:41:27
【问题描述】:

我正在尝试配置我从模板构建的 VyOS 虚拟机。该模板是全新安装的,没有任何配置。

vm 没有配置 IP,所以我不能使用 ssh 选项或 vyos ansible 模块。所以我正在尝试使用 vmware_vm_shell 模块,它可以让我执行命令,但我无法进入 VyOS 的 conf 模式。

我已经为我的 shell 尝试过 bash 和 vbash。我尝试将 conf 命令设置为环境变量来执行,我尝试过 with_item 但它似乎不适用于 vmware_vm_shell。

我需要的最低限度是配置一个 IP 地址,这样我就可以 ssh 或使用 vyos ansible 模块来完成配置。

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: "administrator@vsphere.local"
    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

这会引发错误:

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

【问题讨论】:

    标签: ansible vcenter


    【解决方案1】:

    我有一个 EdgeRouter,它使用 Vyatta 派生的操作系统。您遇到的问题是由于conf(或我的configure)实际上不是命令的名称。 cli 功能是通过一组复杂的 bash 函数实现的,这些函数在您以非交互方式登录时不会加载。

    有一个wiki page on vyos.net 提出了解决方案。通过在 /opt/vyatta/etc/functions/script-template 中进行采购,您可以准备好 shell 环境,以便 vyos 命令按预期工作。

    也就是说,你需要执行一个看起来像这样的 shell 脚本(使用 vbash):

    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
    

    我不熟悉 vmware_vm_shell 模块,所以我不知道你会如何做到这一点,但例如这对我来说运行单个命令:

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

    注意上面的换行符。这表明这可能有效:

        - 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
    

    【讨论】:

    • 谢谢,试过了,但没有运气......但如果它没有内置的外壳,我不能获取登录时加载的配置文件......我简要地看了,但该睡觉了。明天我会继续努力。
    • 睡了几个星期之后。我意识到你没有采购你构建的脚本......你所做的 ansible 任务就像一个魅力!谢谢!
    猜你喜欢
    • 2022-01-25
    • 1970-01-01
    • 2019-11-06
    • 1970-01-01
    • 2022-06-10
    • 2017-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多