我写了一本剧本,里面有两个文件的内容。第一种是负责动态保存具有CDP协议的交换机接口。
example.cdp
0/0
14/0第二个(.cfg)是一个文件,它还动态地包含了一些接口,我需要使用cisco命令“关机”将这些接口推送到设备上,以测试我的主/备份环境。如果example.cdp的接口在这里,我需要删除它们,因为我不能失去与此设备的通信,因为管理是在带内的。
example.cfg
interface FastEthernet0/0
shutdown
interface FastEthernet1/0
shutdown
interface FastEthernet2/0
shutdown
interface FastEthernet2/1
shutdown
...
interface FastEthernet14/0
shutdown剧本
- name: Looping file
debug:
msg: "{{ item }}"
register: items
with_file:
- ~/ANSIBLE/{{ inventory_hostname }}.cfg
- debug: var=items.results[0].item
- name: capturing interfaces with cdp
raw: egrep '[0-9]+\/[0-9]+ ' -o ~/ANSIBLE/{{ inventory_hostname }}.cdp
register: cdp
- debug: var=cdp.stdout_lines
- set_fact:
cdp: "{{cdp.stdout_lines}}"
- debug: var=cdp
- name: Removing interfaces with cdp
raw: sed 's/interface FastEthernet{{item}}//' ~/ANSIBLE/{{ inventory_hostname }}.cfg
with_items:
- "{{cdp}}"
register: items
- debug: var=items
- name: Applying The Shutdown Template
ios_config:
lines:
- "{{ items.results[0].item }}"
provider: "{{cli}}"
register: shut1
- debug: var=shut1
tags: shut1运行剧本:
<169.255.0.1> EXEC sed 's/interface FastEthernet0/0 //' ~/ANSIBLE /169.255.0.1.cfg
failed: [169.255.0.1] (item=0/0 ) => {
"changed": true,
"failed": true,
"item": "0/0 ",
"rc": 1,
"stderr": "sed: -e expression #1, char 30: unknown option to `s'\n",
"stdout": "",
"stdout_lines": []
}
<169.255.0.1> EXEC sed 's/interface FastEthernet14/0 //' ~/ANSIBLE/169.255.0.1.cfg
failed: [169.255.0.1] (item=14/0 ) => {
"changed": true,
"failed": true,
"item": "14/0 ",
"rc": 1,
"stderr": "sed: -e expression #1, char 31: unknown option to `s'\n",
"stdout": "",
"stdout_lines": []
}如您所见,问题在于var "cdp“的内容。这些接口的符号是"/",在"sed“命令中使用,我应该用ansible反斜杠来解决我的问题。有什么方法可以打开变量并对其进行调整吗?
发布于 2017-07-29 08:17:48
sed可以使用任意字符作为正则标记器,因此可以快速解决问题,将其转换为(例如使用#字符):
sed 's#interface FastEthernet{{item}}##' ~/ANSIBLE/{{ inventory_hostname }}.cfg我觉得模板是写任务的更好的方式。
https://stackoverflow.com/questions/45382388
复制相似问题