Bug 1696404 - Force Ansible to use Python 3 (python2-dnf used instead of python3-dnf)
Summary: Force Ansible to use Python 3 (python2-dnf used instead of python3-dnf)
Keywords:
Status: CLOSED NOTABUG
Alias: None
Product: Fedora
Classification: Fedora
Component: ansible
Version: 30
Hardware: All
OS: Linux
unspecified
high
Target Milestone: ---
Assignee: Kevin Fenzi
QA Contact: Fedora Extras Quality Assurance
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2019-04-04 19:14 UTC by Kees de Jong
Modified: 2019-11-03 01:17 UTC (History)
6 users (show)

Fixed In Version:
Doc Type: If docs needed, set a value
Doc Text:
Clone Of:
Environment:
Last Closed: 2019-04-04 19:39:25 UTC


Attachments (Terms of Use)


Links
System ID Priority Status Summary Last Updated
Github ansible ansible issues 54855 None None None 2019-04-04 19:15:39 UTC

Description Kees de Jong 2019-04-04 19:14:56 UTC
Description of problem: Fedora 30 obsoleted the package python2-dnf. When the module package or dnf is used, it wants to run the command dnf install -y python2-dnf. This fails since this package is not present anymore. This should point to python3-dnf. Solution provided at the bottom.


Version-Release number of selected component (if applicable):
ansible 2.7.9
  config file = /home/user/git/ansible/ansible.cfg
  configured module search path = ['/home/user/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.7.2 (default, Mar 21 2019, 10:09:12) [GCC 8.3.1 20190223 (Red Hat 8.3.1-2)]


How reproducible:
$ ansible --vault-id @prompt defiant -m dnf -a "name=vim state=present" -vv

defiant | FAILED! => {
    "changed": false,
    "cmd": "dnf install -y python2-dnf",
    "msg": "Error: Unable to find a match",
    "rc": 1,
    "stderr": "Error: Unable to find a match\n",
    "stderr_lines": [
        "Error: Unable to find a match"
    ],
    "stdout": "Last metadata expiration check: 0:02:19 ago on do 04 apr 2019 15:00:35 CEST.\nNo match for argument: python2-dnf\n",
    "stdout_lines": [
        "Last metadata expiration check: 0:02:19 ago on do 04 apr 2019 15:00:35 CEST.",
        "No match for argument: python2-dnf"
    ]
}


Additional info:
Ansible 2.7 still defaults to executing /usr/bin/python which would point to python2. You would have to explicitly set ansible_python_interpreter=/usr/bin/python3 to use Python3.

The dnf module selects the appropriate dnf python bindings package that needs installed based on the version of python that is being executed on the remote side.

Comment 1 Kevin Fenzi 2019-04-04 19:25:00 UTC
For Fedora30+ machines (or any machines where python3 is installed and python2 is not) you should tell ansible to use python3 for that target: 

https://docs.ansible.com/ansible/latest/reference_appendices/python_3_support.html

In an upcoming ansible release, ansible may be able to auto detect what python you want to use, but thats not currently implemented.

I don't think there's much we can do here...

Comment 2 Toshio Kuratomi 2019-04-04 19:39:25 UTC
Yeah, this is working as designed in Ansible-2.7.  Ansible-2.8+ will have a feature that you can turn on to autodetect the python interpreter to use on the remote machine: https://docs.ansible.com/ansible/devel/reference_appendices/interpreter_discovery.html

The convenience comes at the cost of having to make additional round trips to the server.  Depending on your network latency, it might be worthwhile to continue to set ansible_python_interpreter explicitly.

I've tested that the feature chooses /usr/bin/python3 on Fedora 29 with multiple versions of python installed from the versioned python rpms.  There could be bugs in other configurations, though, (for instance locally installing python from source or changing what /usr/bin/python3 refers to, or other platforms than Fedora) as there are a multitude of different scenarios out there.

Comment 3 Chad 2019-11-03 01:17:59 UTC
Ran into a similar issue with RHEL 7.7 Server.  My issue is there doesn't appear to be a python3-dnf listed in the repository.  Thoughts/ideas?


Note You need to log in before you can comment on or make changes to this bug.