Twitter

Ansible: Error opening terminal: unknown

When ansible does not have the module you want, you can still use the shell module to run any shell command you want which is very cool.

I made my own playbook to be able to run any command / script on a target host from Ansible Tower which is very useful to run rac-status.sh, asmdu.sh, etc ... once on each cluster with one click, or an uptime command on each node of a cluster, and yes, also patching Exadata (:)), etc ... and I recently faced a weird error when running some Exadata pre-requisites:
Error opening terminal: unknown
This was not obvious to me at first and I then discovered that it was coming from the exa-versions.sh script and more precisely this line of code:
NB_PER_LINE=$(bc <<< "`tput cols`/22")          # Number of element to print per line
This command line uses tput to get the size of the user terminal to make a nice a dynamic output not exceeding the border of the user terminal which is cool but ansible does not run with a terminal (nor any environment) thus the error I got ! and you can reproduce it easily from a shell environement:
$ echo $TERM
xterm          
$ tput cols
346                                               <== Number of columns of my terminal
$ unset TERM
$ tput cols
tput: No value for $TERM and no -T specified      <== makes sense, there is no terminal any more as I have unset TERM
$

This is where you (re)discover that ansible runs with no environment and also no terminal. To fix this, you have just have to set the TERM environment variable to the shell ansible module as shown below:
  - name: Run command | Execute {{ l_command }}
    environment:
      PATH: "/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:{{ scripts_dir }}"
      TERM: xterm                                 <== set the TERM to xterm
    shell: "{{ l_command }}"
    register: out
    args:
      executable: /bin/bash
    when: l_command is defined
And you are done ! Also note that depending on what you want to run, you may also need to set a PATH; here, I also add to my path a variable where all my scripts are on the local system: scripts_dir.

No comments:

Post a Comment

CUDA: Getting started on Google Colab

While getting started with CUDA on Windows or on WSL (same on Linux) requires to install some stuff, it is not the case when using Google...