Ansible vmware_vm_shell SED命令添加额外字符
我很难把这个弄对 我收集了虚拟机事实,并进行了测试调试,为虚拟机提供了正确的mac地址。但是,当我将查询添加到sed命令时,会添加额外的字符 这很有效Ansible vmware_vm_shell SED命令添加额外字符,shell,sed,ansible,virtual-machine,vmware,Shell,Sed,Ansible,Virtual Machine,Vmware,我很难把这个弄对 我收集了虚拟机事实,并进行了测试调试,为虚拟机提供了正确的mac地址。但是,当我将查询添加到sed命令时,会添加额外的字符 这很有效 - name: Get MAC address of VMs to add to eth0 configuration debug: msg: "{{ vm_guest_facts.results | json_query(s_query) }}" vars: s_query: "[?instance.hw_name ==
- name: Get MAC address of VMs to add to eth0 configuration
debug:
msg: "{{ vm_guest_facts.results | json_query(s_query) }}"
vars:
s_query: "[?instance.hw_name == '{{ item }}'].instance.hw_eth0.macaddress"
with_items: "{{ inventory_hostname }}"
输出
ok: [server1] => (item=server1) => {
"msg": [
"00:50:56:80:e0:a1"
]
这失败了
- name: fix network phase 2 - replace template MAC
vars:
s_query: "[?instance.hw_name == '{{ item }}'].instance.hw_eth0.macaddress"
vmware_vm_shell:
hostname: '{{ deploy_vsphere_host }}'
username: '{{ deploy_vsphere_user }}'
password: '{{ deploy_vsphere_password }}'
datacenter: "{{ vsphere_datacenter }}"
validate_certs: no
vm_id: "{{ item }}"
vm_username: xxx
vm_password: xxx
vm_shell: '/bin/sed'
vm_shell_args: " -i.bak 's/^HWADDR.*/HWADDR={{ vm_guest_facts.results | json_query(s_query) }}/' /etc/sysconfig/network-scripts/ifcfg-eth0"
with_items:
- "{{ inventory_hostname }}"
delegate_to: localhost
输出(截取)
最后一行中的mac地址在开头和结尾分别获得额外的[u]
有没有办法解决这个问题,或者有人可以帮我用sed行在额外的步骤中删除这些额外的字符
谢谢
凯恩。任何带有_项的
任务总是有一个结果列表,您可以在msg:
输出中看到,其中包含python列表字符[
和]
。因此,vm_guest_facts.results | json_query()
需要将[0]
或|首先
附加到它,以便将它解析为一件事我放弃了在多个地方多次尝试[0]
以下是针对此特定问题的欺骗/肮脏修复:
" -i.bak 's/^HWADDR.*/HWADDR={{ vm_guest_facts.results | json_query(s_query)| regex_replace(']') | regex_replace('\\[u') }}/' /etc/sysconfig/network-scripts/ifcfg-eth0"
谢谢你,马修,但我一定是做错了什么。当我在vm_shell_args |[0]中添加到查询时,我在输出中得到的只是[],并且|首先告诉我有一个未声明的变量。还有其他建议吗?
" -i.bak 's/^HWADDR.*/HWADDR={{ vm_guest_facts.results | json_query(s_query)| regex_replace(']') | regex_replace('\\[u') }}/' /etc/sysconfig/network-scripts/ifcfg-eth0"