我使用 “with_file” 解决了这个问题,登记item.results [0] .item的内容和它推到设备象下面这样:
- name: Looping file
debug:
msg: "{{ item }}"
register: items
with_file:
- ~/ANSIBLE/{{ inventory_hostname }}.cfg
- debug: var=items.results[0].item
- name: Applying The Shutdown Template
ios_config:
lines:
- "{{ items.results[0].item }}"
provider: "{{cli}}"
register: shut
运行剧本:
TASK [Looping file] *******************************************************************************************************************************
ok: [169.255.0.1] => (item=interface FastEthernet1/0
shutdown
interface FastEthernet1/1
shutdown
interface FastEthernet1/3
shutdown
interface FastEthernet1/4
shutdown
interface FastEthernet1/5
shutdown
interface FastEthernet1/6
shutdown) => {
"item": "interface FastEthernet1/0 \nshutdown\ninterface FastEthernet1/1 \nshutdown\ninterface FastEthernet1/3 \nshutdown\ninterface FastEthernet1/4 \nshutdown\ninterface FastEthernet1/5 \nshutdown\ninterface FastEthernet1/6 \nshutdown",
"msg": "interface FastEthernet1/0 \nshutdown\ninterface FastEthernet1/1 \nshutdown\ninterface FastEthernet1/3 \nshutdown\ninterface FastEthernet1/4 \nshutdown\ninterface FastEthernet1/5 \nshutdown\ninterface FastEthernet1/6 \nshutdown"
}
TASK [debug] **************************************************************************************************************************************
ok: [169.255.0.1] => {
"items.results[0].item": "interface FastEthernet1/0 \nshutdown\ninterface FastEthernet1/1 \nshutdown\ninterface FastEthernet1/3 \nshutdown\ninterface FastEthernet1/4 \nshutdown\ninterface FastEthernet1/5 \nshutdown\ninterface FastEthernet1/6 \nshutdown"
}
TASK [Applying The Shutdown Template] *************************************************************************************************************
changed: [169.255.0.1]
TASK [debug] **************************************************************************************************************************************
ok: [169.255.0.1] => {
"shut1": {
"banners": {},
"changed": true,
"commands": [
"interface FastEthernet1/0 ",
"shutdown",
"interface FastEthernet1/1 ",
"shutdown",
"interface FastEthernet1/3 ",
"shutdown",
"interface FastEthernet1/4 ",
"shutdown",
"interface FastEthernet1/5 ",
"shutdown",
"interface FastEthernet1/6 ",
"shutdown"
],
"updates": [
"interface FastEthernet1/0 ",
"shutdown",
"interface FastEthernet1/1 ",
"shutdown",
"interface FastEthernet1/3 ",
"shutdown",
"interface FastEthernet1/4 ",
"shutdown",
"interface FastEthernet1/5 ",
"shutdown",
"interface FastEthernet1/6 ",
"shutdown"
]
}
}
PLAY RECAP ****************************************************************************************************************************************
169.255.0.1 : ok=4 changed=1 unreachable=0 failed=0
你需要捕获文件吗?迭代接口并在不首先创建文件的情况下关闭它们就足够了吗?你打算关闭包括Vlan1在内的所有接口吗? – jscott
@jscott确实如此。这是必要的,因为在关闭接口之后,我应该在经过很多测试后再使用另一个剧本。 output.log将像新的“输入”一样执行新任务来运行“no shutdown”命令。 –