我有一个ansibleangular色
- name: add virtual interface configs for vlan template: src=ifcfg-eth.vlan.j2 dest=/etc/sysconfig/network-scripts/ifcfg-{{ item.device }} with_items: "{{ vlan_interfaces }}" tags: firewall_d-public-network-vlans notify: restart vlan interfaces
和处理它的处理程序
- name: restart vlan interfaces command: bash -c "ifdown {{ item.device }} && ifup {{ item.device}}" with_items: "{{ vlan_interfaces }}"
但我不想启动和停止所有的接口,只有那些实际上已经改变的接口。
有没有办法只推“item.device”到我可以在处理程序中使用的列表或有办法使处理程序在每个项目上运行?
所以,你不可能在playbook上做一些特定的事情来处理with-items任务中的每个调用。 但是你得到的结果,你可以注册,然后在处理程序中做的东西。
所以添加“注册”命令到任务
- name: add virtual interface configs for vlan template: src=ifcfg-eth.vlan.j2 dest=/etc/sysconfig/network-scripts/ifcfg-{{ item.device }} with_items: "{{ vlan_interfaces }}" tags: firewall_d-public-network-vlans register: add_virtual_interface_output notify: restart vlan interfaces
并在处理程序中循环。 因为输出如下所示:
"results": [ { "_ansible_notify": [ "restart vlan interfaces" ], "changed": false, "gid": 0, "group": "root", "item": { "device": "eth1.405", "ipaddr": "172.16.55.5", "lladr": "00:50:56:ab:5d:7c", "network": "172.16.55.0", "prefix": 24 }, "mode": "0644", "owner": "root", "path": "/etc/sysconfig/network-scripts/ifcfg-eth1.405", "secontext": "system_u:object_r:net_conf_t:s0", "size": 100, "state": "file", "uid": 0 }, ... ]
您可以检查每个结果中的“更改”状态,只重新启动它们:
- name: restart vlan interfaces # debug : msg="{{ item.changed }}" command: bash -c "ifdown {{ item.item.device }} && ifup {{ item.item.device }}" when: item.changed == true with_items: "{{ add_virtual_interface_output.results }}"