[Ansible] Install dnf on Centos7 with Python3 interpreter

[Ansible] Install dnf on Centos7 with Python3 interpreter

What is dnf

fedora official page

--It seems to be called Dandified Yum --Fedora's package management system (default from version 22) --Can also be used with RHEL7 and CentOS7 (yum install -y dnf) --Installed as standard on RHEL8 and CentOS8

Simply put, dnf is the successor to yum, and from CentOS 8 it defaults to dnf instead of yum. You can use the yum command in CentOS8, but it's just a symbolic link to dnf.

Why use dnf instead of yum

Support for Python 2.X ended on January 1, 2020.

--yum only works with python2.X --dnf works with python 2.x or 3.X

So from now on, dnf will become the mainstream.

(First) Try installing on CentOS 7 manually

Install dnf in the CentOS 7.X Docker container.

[dnfuser@localhost ~]$ docker run -it --rm centos:7 bash

[root@3b4a088d7e6f /]# yum update -y
[root@3b4a088d7e6f /]# yum install -y epel-release
[root@3b4a088d7e6f /]# yum install -y dnf

[root@3b4a088d7e6f /]# head -n1 /usr/bin/dnf
#!/usr/bin/python2

Apparently CentOS 7.X's dnf is running on python 2.X. I tried it after installing python3.X, but dnf becomes 2.X.

Install dnf with Ansible

https://docs.ansible.com/ansible/latest/modules/dnf_module.html

If the interpreter is Python 2.X, you can install it easily.

playbook.yml


- name: Install epel-release
  yum:
    name: epel-release
    state: present
  become: yes

- name: Install dnf
  yum:
    name: dnf
    state: present
  become: yes
  1. Install dnf with yum module
  2. Subsequent tasks can be used by changing the yum module to the dnf module

(Main subject) When the interpreter of Ansible is python3.X

ansible.cfg


[default]
ansible_python_interpreter=/usr/bin/python3

As mentioned above, if you are using the Python 3.X interpreter by default, you will need to force it back to 2.X.

playbook.yml


- name: Install dnf for Centos7
  block:
    - name: Change interpreter
      set_fact:
        ansible_python_interpreter: "/usr/bin/python"

    - name: Install epel-release
      yum:
        name: epel-release
        state: present
      become: yes

    - name: Install dnf
      yum:
        name: dnf
        state: present
      become: yes
  when:
    - ansible_distribution_major_version | float < 8

The interpreter will be 2.X for subsequent tasks as well.

Finally (Ansible interpreter best practices)

In Ansible 2.12 ver., The behavior of the default interpreter seems to change. (Maybe Python 3.X will be the preferred choice)

reference) https://docs.ansible.com/ansible/latest/reference_appendices/interpreter_discovery.html https://rheb.hatenablog.com/entry/ansible_interpreter_discovery

In the current ver. (2.9), / usr / bin / python is specified by default, so the Python ver. Used may differ depending on the OS. The best practices for those who can't wait for 2.12 ver. And want to use the dnf command are as follows.

Give up and use the default (Python 2.X) interpreter

  1. For Ansible, use the default / usr / bin / python and continue to use Python 2.X
  2. Unify to dnf module instead of yum module

For those who say "Python 2.X is fine", you can use the default interpreter.

If you really want to use the Pyhton 3.X interpreter

** Specify the Python 2.X interpreter in inventory for the CentOS 7.X host **

inventory.yml


all:
  children:
    centos7:
      hosts: centos7-1
      vars:
        ansible_python_interpreter: /usr/bin/python # /usr/bin/python2 is also OK

If only CentOS7 is grouped, it seems easy to implement.

** Force the interpreter to change to 2.X in the playbook **

playbook.yml


- name: Change interpreter
  set_fact:
    ansible_python_interpreter: /usr/bin/python

By the way, I'm using this in case Python 3.X becomes the default.

Recommended Posts

[Ansible] Install dnf on Centos7 with Python3 interpreter
Install Python3.4 on CentOS 6.6
Install Python 2.7.3 on CentOS 5.4
Build a python environment with ansible on centos6
Install Python 3.8 on CentOS 7 (SCL)
Install Python 3.8 on CentOS 8 (AppStream)
Install Python from source with Ansible
Install Python on CentOS using Pyenv
Install Python on CentOS using pyenv
CentOS8 --Install --Python3
A memo with Python2.7 and Python3 on CentOS
scipy stumbles with pip install on python 2.7.8
Install Mecab on Linux (CentOS) with brew
How to install python3 with docker centos
Install Faiss on CentOS 7
Install Python on Pidora.
Install Scrapy on python3
Install numba on CentOS 7.2
Install Ansible on Mac
Install Python on Mac
Install OpenCV 4.0 and Python 3.7 on Windows 10 with Anaconda
Install Python 3 on Mac
Install Voluptuous with Python 2.5
Install python on windows
Install mecab-python on CentOS
Run Flask on CentOS with python3.4, Gunicorn + Nginx.
Install python with pyenv
Install Python 3.3 on Ubuntu 12.04
Install Python 3 on MacOS Catalina (with Homebrew only)
Install Python 3.4 on Mac
Install awscli on centos7
Install Chainer on CentOS 6.7
Install Python 3.6 on Docker
Install selenium on Mac and try it with python
Install watchdog on Windows + Python 3.3
Install Python environment with Anaconda
Install pygame on python3.4 on mac
Install ImageMagick-6.2.x series on CentOS7.7
Install external libraries with Python
Install pandas 0.14 on python3.4 [on Mac]
Debug with PEPPER python interpreter
Install Python 3.7 and Django 3.0 (CentOS)
Install OpenCV on Ubuntu + python
Install Chrome on CentOS 7 series
Python --Install MySQLDB on EC2
[Python] [Chainer] [Windows] Install Chainer on Windows
Notes on installing Python on CentOS
Install Python Pillow on Amazon Linux
Install Python 3.8 on Ubuntu 18.04 (OS standard)
Steps to install MySQL 8.0 on CentOS 8.1
Install Python3 on Sakura server (FreeBSD)
raspberry pi 4 centos7 install on docker
I can't install python3 with pyenv-vertualenv
Install Python 3.8 on Ubuntu 20.04 (OS standard)
Install xgboost (python version) on Windows
Install Python on Windows + pip + virtualenv
Steps to install VirtualBox on CentOS
Install Pytorch on Blender 2.90 python on Windows
Install Python 3.7 Anaconda on MAC, but Python 2
Install python3 on Mac (El Capitan)
Install java (Oracle JDK14) on CentOS7