【发布时间】:2015-01-02 16:19:52
【问题描述】:
我正在使用“shell:”通过循环“with_items:”并将其注册为另一个变量来获取一些数据。 稍后使用“lineinfile:”我试图应用早期变量的内容,但无法使用“{{variable.stdout}}”,因为它显示为在“with_items:”中未定义
有没有办法告诉 ansible 对于“variable.stdout”不要查看“with_items:”
---
- include_vars: /root/template.yml
- name: Getting MAC
shell: "cat /sys/class/net/{{item.name}}/address"
register: mac
with_items:
- "{{ interfaces_ipv4 }}"
- name: Setting MAC
lineinfile:
state=present
dest=/etc/sysconfig/network-scripts/ifcfg-{{item.name}}
regexp='^HWADDR=.*'
line="HWADDR={{mac.stdout}}"
with_items:
- "{{ interfaces_ipv4 }}"
tags:
- set_mac
变量文件的内容
#/root/tempplate.yml
- name: ens35
bootproto: dhcp
- name: ens34
bootproto: none
执行时:
任务:[mac |设置MAC] *********************************************** ****** 致命的:[192.168.211.146] => 一个或多个未定义的变量:'dict'对象没有属性'stdout'
致命:所有主机都已失败 -- 正在中止
【问题讨论】:
-
请认真考虑使用 Ansible 的
template模块而不是lineinfile。后者是一种反模式;它倾向于相当复杂和痛苦。