How to change remote hosts' terminal screen size/resolution in Ansible

Hi,

Here is the brief about scenario:

  1. login to Debian host machine using ansible
  2. 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)
  3. 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 :

  1. 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.
  2. Actual stats counts are of line 500 hundred but above code returns 2k + stats with repetitive stats and some garbage values appended.
  3. 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.

May I request you to help me in this ?

Also, There is a limitation on remote host - It does not have xrandr module. So, changing screen size with xrandr will not be available :frowning:

Also, There is a limitation on remote host - It does not have xrandr module. So, changing screen size with xrandr will
not be available :frowning:

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.

Regards
         Racke

How exactly are the stats output on the host?

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.

Regards, K.

How exactly are the stats output on the host?

Actual Output received in stats.txt file though method which is I am using.

“OPR>display debug info PP ip 192.168.30.26 port 8686 data”,
“\u001b[H\u001b[JSending command to get debug data from {192.168.30.26:8686} PP”,
“”,
" Current counters in PP:",
“”,
“Data Counters Summary:”,
“\u001b[5GTotal Pkt Received\u001b[6;49H{ 10202784 PPS }”,
“\u001b[7G3G pkt Rvcd\u001b[7;49H{ 5101392 PPS }”,
“\u001b[7G4G pkt Rvcd\u001b[8;49H{ 0 PPS }”,
“\u001b[7Gunknown rat type pkt Rvcd\u001b[9;49H{ 5101392 PPS }”,
“\u001b[5GArp-Rsp Pkt Drop\u001b[10;49H{ 0 PPS }”,
“\u001b[5GArp-Req Pkt process\u001b[11;49H{ 549683392 PPS }”,
“\u001b[5GICMP-Req Pkt process\u001b[12;49H{ 32765 PPS }”,
“”,

Note: there are 2K lines below (as I described in my initial comment)

Expected results – or output displayed when we hit the command manually.

 Current counters in PP:

Data Counters Summary:
    Total Pkt Received                          { 10202784 PPS }
      3G pkt Rvcd                               { 5101392 PPS }
      4G pkt Rvcd                               { 0 PPS }
      unknown rat type pkt Rvcd                 { 5101392 PPS }
    Arp-Rsp Pkt Drop                            { 0 PPS }
    Arp-Req Pkt process                         { 3508911440 PPS }
    ICMP-Req Pkt process                        { 32765 PPS }

  Pkt BB PP License Expired                     { 0 PPS }
 

Pkt BB Configuration not recieved { 5101392 PPS }

Note : There are 500 lines below…not adding all here.

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

H[i, I tried with stty but that could not help. output remained the same.

Here is how i used that :

  • hosts: opr
    gather_facts: no
    tasks:
  • name: set the winsize
    shell: stty cols 500 rows 500
  • name: Get the debug status
    ignore_errors: yes

…and kept rest script same.

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.

Regards, K.

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.

Regards, K.

bash: TERM-dumb: command not found.

gnome-terminal utility not available on remote machine

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

TERM=dumb

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.

Regards, K.

TERM=dumb

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 :

  1. I use mouse > Select the area of stats appeared on screen (after selecting area, termius utility copy the selected area automatically) >
  2. Paste it manually by pressing ctr+v into sublime text editor
  3. 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.
  4. 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.

Hello Samir,

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.

Regards
         Racke

I’ve offered all the help I can. Your best bet is to contact the people who wrote the program you are trying to use.

You may find it easier to do things like set environment variables and redirect output using the shell module:

https://docs.ansible.com/ansible/latest/modules/shell_module.html#shell-module

Also, it would be a VERY good idea to test things out manually before trying to do them in Ansible.

Good luck, K.