I want to change ansible to use python3.5 on the controller. I already have ansible and python3.5 installed, is there a way to just change it to use python3?
The ansible doc have suggested testing python3 with ansible with python3 /usr/bin/ansible localhost -m ping
. But don't give any more detail if this doesn't work.
My result is:
Traceback (most recent call last):
File "/usr/bin/ansible", line 32, in <module>
from ansible import context
ImportError: No module named 'ansible'
I have also tried to pip3 install ansible without luck:
File "<string>", line 1, in <module>
File "/tmp/pip-build-eadti4n6/ansible/setup.py", line 12
print "Ansible now needs setuptools in order to build. " \
^
SyntaxError: Missing parentheses in call to 'print'
Following the directions on the Ansible Python 3 Support page, I installed Ansible using pip3
after removing the previous (2.7) version:
$ sudo -H pip uninstall ansible
$ sudo -H pip3 install ansible
When I got the ImportError: No module named 'ansible'
error, I verified Ansible was accessible, and discovered ansible-playbook
is simply a Python script:
$ which ansible
$ less /usr/bin/ansible-playbook
I thought perhaps something had gone wrong with the installation, so I redid it:
$ sudo -H pip uninstall ansible
$ sudo -H pip3 install ansible
The original uninstall had only asked about removing two things, this time a laundry list was presented. The reinstall didn't download anything, apparently collecting everything from its cache.
I tried re-running my playbook and again got the ImportError: No module named 'ansible'
error. This led to an interesting discovery:
$ ansible --version
Traceback (most recent call last):
File "/usr/bin/ansible", line 34, in <module>
from ansible import context
ImportError: No module named ansible
I then ran less /usr/bin/ansible
and discovered the shebang in the script called for #!/usr/bin/python
which I suspected was an error. I edited the file, and changed the shebang to call for the python3 interpreter. The result was very telling:
ansible --version
ansible 2.9.2
config file = /home/MAGICLEAP/fkoschara/.ansible.cfg
configured module search path = ['/home/MAGICLEAP/fkoschara/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.5/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 3.5.2 (default, Oct 8 2019, 13:06:37) [GCC 5.4.0 20160609]
Apparently the pip3
install failed to set the correct shebang for the ansible
executable. Searching all of /usr/bin/ansible*
I discovered ansible-connection
also had a python
shebang, rather than a python3
one, but all of the other files were correct.
After fixing the incorrect shebangs, my playbook ran correctly.
Q: I want to change ansible to use python3.5 on the controller (Ubuntu).
A: Latest ansible 2.8 package in Ubuntu 18.04 uses Python2
> cat /etc/apt/sources.list.d/ppa_launchpad_net_ansible_ansible_2_8_ubuntu.list
deb http://ppa.launchpad.net/ansible/ansible-2.8/ubuntu bionic main
> ansible --version
ansible 2.8.4
[...]
python version = 2.7.15+ (default, Nov 27 2018, 23:36:35) [GCC 7.3.0]
But it is possible to use Python3 at remote hosts if available. For example
> grep test_01 hosts
test_01 ansible_python_interpreter=/usr/local/bin/python3.6
> ansible test_01 -m ping -vvv
ansible 2.8.4
[...]
python version = 2.7.15+ (default, Nov 27 2018, 23:36:35) [GCC 7.3.0]
[...]
SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="admin"' -o ConnectTimeout=30 -o [...] BECOME-SUCCESS-lwccvmomxxdjjpbipvzertvrtfluaqbt ; /usr/local/bin/python3.6'"'"'"'"'"'"'"'"' && sleep 0'"'"'' Escalation succeeded (0, '\n{"ping": "pong", "invocation": {"module_args": {"data": "pong"}}}\n', '')
If you really need "Ansible Python3 at master" you might want to try pip. See pip install ansible. It's available for both Python2 and Python3. Be careful to set the paths properly. To be sure what are you running deinstall Ubuntu ansible packages.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With