2017-07-03 13 views
2

我想知道是否可以這樣做,因爲我需要在交換機內部運行ios命令來測試我的主/備份環境。是否可以在本地打開一個文件並使用ansible將其推送到遠程主機?

命令的副本,不得不像波紋管:

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命令「關機」這樣的:

interface FastEthernet0/0 
shutdown 

interface FastEthernet0/1 
shutdown 

我一直在尋找的「劇本」和「期望」的命令,但我的努力都沒有奏效。

+0

你需要捕獲文件嗎?迭代接口並在不首先創建文件的情況下關閉它們就足夠了嗎?你打算關閉包括Vlan1在內的所有接口嗎? – jscott

+0

@jscott確實如此。這是必要的,因爲在關閉接口之後,我應該在經過很多測試後再使用另一個劇本。 output.log將像新的「輸入」一樣執行新任務來運行「no shutdown」命令。 –

回答

0

我使用 「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 
0

with_lines是你在找什麼。它遍歷程序執行輸出的每一行。上述

- shell: interface {{ item }} && shutdown 
    with_lines: awk '{print $1}' ~/ANSIBLE/{{ inventory_hostname }}.cfg 

的例子使用awk打印的文件內容的第一列。

+0

老師克里斯林,它可能工作。我要測試,我會在幾個小時後發佈結果。提前致謝! –

+0

@DaniloBraga任何更新? –

+0

克里斯林先生,我很惋惜我的遲到,但是我被很多項目佔用,就在今天我重新討論了這個問題。 –

相關問題