Go to corresponding directory where the binary is and execute that binary. (This binary is written in c and C++. After execution it ask for the userid & password)
After login into that binary’s shell, it has several commands which displays statistics. Those stats are lengthy and does not fit in current screen resolution. For seen seeing all the stats we have to press Down arrow Key or Enter key multiple time until we reach to last stat line.
How I am doing this:
-hosts: sam gather_facts: no tasks: - name: capture all stats ignore_errors: yes expect: command: /opt/abc/xyz_Platform/bin/go_xyz_cli responses: (.)Username:(.): “admin” (.)Password:(.): “admin” (.)xyz_cli>(.): display all debug stats (.):(.): ‘’ register: stats - local_action: copy content=“{{ stats.stdout}}” dest=“/home/sam/quickstats/stats.txt” ignore_errors: yes
Issues Facing :
Multiple similar line gets copied in stats.txt file when above reaches to ==>>(.):(.): ‘’ (See above code section marked in blue). Using this method to press key.
Actual stats counts are of line 500 hundred but above code returns 2k + stats with repetitive stats and some garbage values appended.
If i removes this portion (.):(.): ‘’ from code, then above script only copies 20-30 lines which currently are displayed on screen or which displayed in current screen size.
I guess I can achieve this by changing remote servers’ screen size. but I could not able to get any such way in ansible.
Also, There is a limitation on remote host - It does not have xrandr module. So, changing screen size with xrandr will
not be available
You need to configure the size of the remote terminal, so xrandr won't be the correct tool. So you can try
to manipulate the terminal with shell commands (resize, stty).
But I wonder why you can't tell this binary to write output in a file.
If the programs that produce them send them to standard output, then one option would be to run the commands and redirect their outputs to a file. When all the programs have been run, copy that file to wherever you need it. Remember to append for all but the first command.
Also, I don’t understand what you mean about (.):(.): ‘’
A better mechanism (IMHO) would be to write a shell script that generates all the stats in the order you want them, and either have that script write to a file or redirect its output to a file. Then just grab that file. If the stats are to be obtained from multiple machines you could copy the script to each with Ansible and then execute it. If it’s just one host, you could leave the script on that host. It depends on where would be the most appropriate place to maintain the script.
As shown, there is some garbage data coming that file, we can remove that using regex. My initial objective it to capture all 500 stats lines in variable - stats
Could you please explain how can use xterm command : xterm --tn dumb -cm -dc -e this is your command
Fustratigly, xterm is not present in remote machine and I am not allowed to install xterm utility on targeted remote machine. Still, is there any way to run it through ansible conroller?
What shell are you using on the remote machine? And what kind of terminal is it running in? I have a suspicion that it is just running on a pty, and that your program is blindly putting out control sequences. If it is, then there’s nothing you can do about it except strip them using regexp or similar.
Is there any way to redirect the output from your program into a file? That bypasses the terminal stuff completely , though as I said, if your program is blindly colourising its output OR blindly waiting after every 24 lines, there’s nothing much you can do about it. The duplicate output is strange - can you provide a sample? It may be the program trying to output bold text or something.
Right about now I would be contacting the people responsible for a) the Debian host and b) this program you are trying to run to see if there is some way to solve this problem neatly.
Were you able to find out about possible additional command line controls on the program? Anything Ansible can do, you can do too, so try manually logging in and seeing what you can find out.
You might be able to convince the console on the remote system to be a dumb terminal by doing this as your command:
TERM-dumb ; sh -c your_command
This works in gnome-terminal as well - it seems to suppress most control sequences except CR/LF and similar.
Or in Ansible, setting the environment variable TERM to “dumb” then running your command in a new shell. In my experiments here, when I set TERM=dumb and ran sh from within a console or from within gnome-terminal, the colourisation went away. However the console still had a fixed number of lines.
Duplicate entries when I provide (.):(.): ‘’But this method atleast return complete data.
When I comment-out (.):(.): ‘’ it shows 23 lines displayed on screen + garbage data + no duplicate lines/stats
Sorry, I mistyped. The minus sign should have been an equals sign.
The idea is to set the environment variable TERM to the value “dumb” immediately before starting the shell in which your program will be run.
Also can you please answer the question about whether you can redirect the output of this program?
I strongly suggest you experiment manually rather than with Ansible. You need to find out how to achieve what you want, then duplicate that in Ansible. Doing both at once is very painful.
Sorry, I mistyped. The minus sign should have been an equals sign.
Can you please help me where I can use this in my code and see the content in a text file ? Here is my anible code…pasting it here again -hosts: sam gather_facts: no tasks: - name: capture all stats ignore_errors: yes expect: command: /opt/abc/xyz_Platform/bin/go_xyz_cli responses: (.)Username:(.): “admin” (.)Password:(.): “admin” (.)xyz_cli>(.): display all debug stats (.):(.): ‘’ register: stats - local_action: copy content=“{{ stats.stdout}}” dest=“/home/sam/quickstats/stats.txt” ignore_errors: yes
The idea is to set the environment variable TERM to the value “dumb” immediately before starting the shell in which your program will be run.
Also can you please answer the question about whether you can redirect the output of this program?
NO. There no support on that binary to copy or to point the output to some file. Shell commads like " >>abc.txt " does not work.
I strongly suggest you experiment manually rather than with Ansible. You need to find out how to achieve what you want, then duplicate that in Ansible. Doing both at once is very painful.
Yes. I have done this manually. For collecting stats manually here is the procedure :
I use mouse > Select the area of stats appeared on screen (after selecting area, termius utility copy the selected area automatically) >
Paste it manually by pressing ctr+v into sublime text editor
Again go to remote machine and press down key/Enter key to collect remaining stats and copy-paste the status in similarly way as mentioed in step 1 and 2.
I have to perform step 1,2,3 multiple times untill I copy all the status, as window can display only 25-30 lines/rows. Some times for doing it quickly, I use zoom-out (Ctr++), by this I can copy more lines.
But I want to do this operation through automation.
this is now really off topic. Try to figure out to wrangle the data out of this rather unhelpful binary
by means of "expect" or similar software, but your problem is not related to Ansible.