is it possible to open a file locally, read the content and push it to a remote host?

35 views
Skip to first unread message

Danilo Braga

unread,
Jul 3, 2017, 8:51:51 AM7/3/17
to Ansible Project
Hi all,

I have a script that was wrote in tcl/expect to shutdown ports. This is necessary to test our master/backup environment. Now I have to convert this one to ansible. I'm learning this new one language and I'm stuck in this step:

tcl code to shutdown ports:
The tcl/expect capture the cisco ios command "sh ip int br | up" and save it in a temp file.

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      

This is the part of code responsable for this step:

proc full_buffer {} {
while {1} {
expect {
-re "(\[^\r]*\)\r\n" {
set output "$expect_out(buffer)"
set buffer [open "output.log" a]
puts $buffer $output
close $buffer
}
timeout {
set output "$expect_out(buffer)"
break
}
}
}
}

send "sh ip int br \| up\r" ; full_buffer

set f [open "output.log" r]
foreach a [split [read -nonewline $f] \n] {
set intf [lindex $a 0]

if { [regexp {^F|^G|^P} $0] } {  
send "conf t\r"
send "interface $0\r"
send "shutdown\r"
}
}
close $f


tcl code to bring back interfaces:
set f [open "output.log" r]
foreach a [split [read -nonewline $f] \n] {
set intf [lindex $a 0]

if { [regexp {^F|^G|^P} $0] } {  
send "conf t\r"
send "interface $0\r"
send "no shutdown\r"
}
}
close $f


At the ansible code, I run just until the copy of the command "sh ip int br | i up" and it's working.

  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



What I need now, is a form way to open the file 'output.log', read it line by line and run the command "shutdown" at the remote host. After that, I have to bring back again the interfaces when the test of environment is finished with the command "no shutdown". I'm looking for the "script" command and even more, the "expect" command, but none of that work for me until now.

Kai Stian Olstad

unread,
Jul 3, 2017, 12:28:35 PM7/3/17
to ansible...@googlegroups.com
On 03. juli 2017 13:44, Danilo Braga wrote:
> What I need now, is a form way to open the file 'output.log', read it line
> by line and run the command "shutdown" at the remote host.

To read a file line by line you can use "with_lines: cat output.log"

--
Kai Stian Olstad
Message has been deleted

Danilo Braga

unread,
Jul 3, 2017, 2:14:57 PM7/3/17
to Ansible Project, ansible-pr...@olstad.com
But with the "with_lines", is it possible to run the command remotely at the switch?

Kai Stian Olstad

unread,
Jul 4, 2017, 3:10:01 AM7/4/17
to ansible...@googlegroups.com
On 03.07.2017 20:14, Danilo Braga wrote:
> But with the "with_lines", is it possible to run the command remotely
> at
> the switch?

with_lines do not have this ability, it only read one line at a time and
hand it over to the module.
So if the module run on the remote, it will run on the remote.

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages