簡體   English   中英

使用 ansible vmware_shell 配置 VyOS vm

[英]Configure VyOS vm with ansible vmware_shell

我正在嘗試配置我從模板構建的 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

我有一個 EdgeRouter,它使用 Vyatta 派生的操作系統。 您遇到的問題是由於conf (或為我configure )實際上不是命令的名稱。 cli 功能是通過復雜的 bash 函數集合實現的,這些函數在您以非交互方式登錄時不會加載。

vyos.net 上有一個wiki 頁面,提供了一個解決方案。 通過在/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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM