问题描述
我正在尝试配置从模板构建的VyOS vm。该模板是全新安装,没有任何配置。
虚拟机未配置IP,因此无法使用ssh选项或vyos ansible模块。因此,我尝试使用vmware_vm_shell模块,该模块将允许我执行命令,但无法为VyOS进入conf模式。
我已经为我的shell尝试了bash和vbash。我已经尝试将conf命令设置为环境vars来执行,我已经尝试过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: "[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
这会引发错误:
/bin/vbash: conf: No such file or directory
解决方法
我有一个EdgeRouter,它使用Vyatta衍生的操作系统。您遇到的问题是由于conf
(或对我而言是configure
)实际上不是命令名称引起的。 cli功能是通过bash函数的复杂集合来实现的,这些bash函数是非交互式登录时不会加载的。
有一个wiki page on vyos.net提出了解决方案。通过采购/opt/vyatta/etc/functions/script-template
,您可以准备外壳环境,以便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