Cook
Cook

Reputation: 493

Ansible: How to change Python Version

Trying to use GNS3 to practice ansible script, there is a docker instance called "Network Automation" with built-in ansible. However, it still uses Python 2.7 as the interpreter:

root@Network-Automation:~# ansible --version
ansible 2.7.11
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.12 (default, Nov 12 2018, 14:36:49) [GCC 5.4.0 20160609]

I understand I can use "ansible-playbook --version -e 'ansible_python_interpreter=/usr/bin/python3'" command to run a playbook with Python version 3, or I can specifiy var within the playbook:

- name: Common package
  hosts: all
  gather_facts: no
  vars:
    ansible_python_interpreter: /usr/bin/python3
  roles:
    - { role: python, tags: [ init, python, common, addusers] }
...
...

However, I would like to have a permanent way to force ansible to use Python3 version. How can I achieve this? Thanks.

Upvotes: 28

Views: 135766

Answers (4)

Dave
Dave

Reputation: 254

I think you can set the variable in the config file, /etc/ansible/ansible.cfg, that should apply it to everything (unless a higher level variable overwrites it).

ansible_python_interpreter = /usr/bin/python3

INTERPRETER_PYTHON

Upvotes: 4

Johnny.X
Johnny.X

Reputation: 61

Adding some points that you might overlook based on comments above:

  • In the original post, the ansible was installed under root account, which in many other environment, you won't use root. In this case, you need to sudo su then install the ansible with pip3, otherwise, it will end up installing for you account only under: ~/.local/bin
  • By new pip version, it's recommended to use python3 -m pip install xxx than directly execute pip3 install xxx

Upvotes: 1

MMM
MMM

Reputation: 163

Why not use the var directory in your role...

├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml

in vars/main.yml just add....

---                                                                                                                                                                        
# vars file for XXXX
  ansible_python_interpreter: /usr/bin/python3

Upvotes: 14

Josip Rodin
Josip Rodin

Reputation: 809

Per https://docs.ansible.com/ansible/latest/reference_appendices/interpreter_discovery.html you could simply set it in the inventory for that host, or in your configuration file for ansible (which can also be shipped in the same directory as the playbooks and/or inventory):

To control the discovery behavior:

  • for individual hosts and groups, use the ansible_python_interpreter inventory variable
  • globally, use the interpreter_python key in the [defaults] section of ansible.cfg

Upvotes: 2

Related Questions