是否可以使用ansible在本地打开文件并将其推送到远程主机?
我想知道是否有可能做到这一点,因为我需要在交换机内运行ios命令来测试我的主/备份环境 命令的副本如下所示:是否可以使用ansible在本地打开文件并将其推送到远程主机?,ansible,Ansible,我想知道是否有可能做到这一点,因为我需要在交换机内运行ios命令来测试我的主/备份环境 命令的副本如下所示: tasks: - name: capturing the command "show ip int br" on {{ inventory_hostname }} ios_command: commands: - sh ip int br | i up provider: "{{ cli }}" register: result tags:
tasks:
- name: capturing the command "show ip int br" on {{ inventory_hostname }}
ios_command:
commands:
- sh ip int br | i up
provider: "{{ cli }}"
register: result
tags: inft
- debug: var=result
tags: result_debug
- name: copy interface status up to a temp file
copy:
content: "{{ result.stdout[0] }}"
dest: "~/ANSIBLE/{{ inventory_hostname }}.cfg"
tags: copy
这是文件的输出
FastEthernet0/0 169.255.0.1 YES NVRAM up up
FastEthernet1/1 unassigned YES unset up up
FastEthernet1/6 unassigned YES unset up up
FastEthernet1/10 unassigned YES unset up up
Vlan1 unassigned YES NVRAM up up
捕获命令后,我需要打开文件,逐行读取,然后像这样运行ios命令“shutdown”:
interface FastEthernet0/0
shutdown
interface FastEthernet0/1
shutdown
我一直在寻找“script”和“expect”命令,但我的尝试都没有成功。正是您要寻找的。它迭代程序执行输出的每一行
- shell: interface {{ item }} && shutdown
with_lines: awk '{print $1}' ~/ANSIBLE/{{ inventory_hostname }}.cfg
上面的示例使用
awk
打印文件内容的第一列。我使用“with_file”解决了问题,注册了项的内容。结果[0]。项并将其推送到设备,如下所示:
- 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
您需要捕获到文件吗?在不首先创建文件的情况下,迭代up接口并就地关闭它们是否足够?您是否打算关闭包括Vlan1在内的所有接口?@jscott确实如此。这是必要的,因为在关闭接口后,我应该使用另一个ansible playbook在大量测试后将其带回来。output.log将作为一个新的“输入”来执行这个新任务,以运行命令“no shutdown”.Sr。克里斯·林,这可能有用。我将进行测试,并在几个小时后公布结果。提前谢谢!林瑞麟先生,很抱歉我迟到了,但我忙于很多项目,就在今天,我又重温了这个话题。