Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

How to Fix “Shared connection to x.x.xx closed” Ansible Error - DesignLinux

Sep 03 2020
designlinux 0 Comments

In this short article, we will explain how to solve the: “module_stderr“: “Shared connection to x.x.x.x closed.\r\n”, “module_stdout”: “/bin/sh: /usr/bin/python: No such file or directory\r\n”, while running Ansible commands.

The following screenshot shows the Ansible module error. We encountered this error while running an Ansible command to execute commands on two newly deployed CentOS 8 servers.

Ansible Module Error
Ansible Module Error

From the error details, the connection failed because the shell(s) in the remote system couldn’t find the Python interpreter (/usr/bin/python) as indicated by the line: “module_stdout”: “/bin/sh: /usr/bin/python: No such file or directory\r\n“.

After checking the remote hosts, we discovered that the systems don’t have Python 2 installed.

Check Python Binary
Check Python Binary

They have Python 3 installed by default and its binary is /usr/bin/python3.

Check Python3 Binary
Check Python3 Binary

According to the Ansible documentation, Ansible (2.5 and above) works with Python version 3 and above only. Also, Ansible is supposed to automatically detect and use Python 3 on many platforms that ship with it.

However, if it fails to, then you can explicitly configure a Python 3 interpreter by setting the ansible_python_interpreter inventory variable at a group or host level to the location of a Python 3 interpreter as described below.

Passing Python Interpreter to Ansible on the Command-line

To fix the above error temporarily, you can use the -e flag to pass the Python 3 interpreter to Ansible as shown.

$ ansible prod_servers  -e 'ansible_python_interpreter=/usr/bin/python3' -a "systemctl status firewalld" -u root
Set Python Interpreter via Command-line
Set Python Interpreter via Command-line

Setting Python Interpreter for Ansible in the Inventory

To fix the error permanently, set the ansible_python_interpreter inventory variable in your inventory /etc/ansible/hosts. You can open it for editing using the v/im or nano text editor as shown.

$ sudo vim /etc/ansible/hosts
OR
# vim /etc/ansible/hosts

Append the following line to each host or hosts in a group:

ansible_python_interpreter=/usr/bin/python3

So, your hosts’ definitions can look like this:

[prod_servers]
192.168.10.1			ansible_python_interpreter=/usr/bin/python3
192.168.10.20			ansible_python_interpreter=/usr/bin/python3.6
Set Python Interpreter for Ansible
Set Python Interpreter for Ansible

Alternatively, set the same Python interpreter for a group of hosts as shown.

[prod_servers]
192.168.10.1		
192.168.10.20		

[prod_servers:vars]
ansible_python_interpreter=/usr/bin/python3
Set Python Interpreter-for All Hosts in a Group
Set Python Interpreter-for All Hosts in a Group

Setting Default Python Interpreter in Ansible Configuration

To set the default Python interpreter, you can set the ansible_python_interpreter inventory variable in Ansible’s main configuration file /etc/ansible/ansible.cfg.

$ sudo vim /etc/ansible/ansible.cfg

Add the following line under the the [defaults] section.

ansible_python_interpreter=/usr/bin/python3
Set Python Interpreter in Ansible Configuration
Set Python Interpreter in Ansible Configuration

Save the file and close it.

Now try to run the Ansible command once more:

$ ansible prod_servers -a "systemctl status firewalld" -u root
Check Ansible Command
Check Ansible Command

For more information about this topic, see Python 3 support in the official Ansible documentation.

Related

Tags: Ansible Tips, Linux FAQ's

How to Setup Sublime Text sFTP for Remote Development

Prev Post

FrostWire – A Cloud Downloader, BitTorrent Client and Media Player

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 707
  • 579,794

DesignLinux.com © All rights reserved

Go to mobile version