How to pass options to dnf when using Ansible - ansible

When installing packages through the Ansible's dnf module. How do I pass options like --nobest to dnf? Is there alternative to using raw shell commands.

I had similar problem (but i'm using yum package manager) and figured a work around here
The issue is that docker-ce has not been packaged for CentOS 8 yet.
An ugly workaround is to install containerd.io manually beforehand:
pre_tasks:
- yum:
name: https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.6-3.3.el7.x86_64.rpm
So, try to set full package url as package name and it should definitely work.

nobest can be passed as a parameter while using the DNF module in the playbook
You can refer here dnf_module for other options/parameters that can be passed to the dnf ansible module
For example :
- name: Install the latest version of Apache from the testing repo
dnf:
name: httpd
enablerepo: testing
state: present
- name: Install the latest version of Apache
dnf:
name: httpd
state: present
nobest: false

Related

Yum module is choosing rpm with wrong os release with "latest" option

Suppose I have the following ansible yum task
- name: install java-1.8.0
yum:
name: java-1.8.0-openjdk
state: latest
Now, yum repo, is not the standard java repo, but local repo on site premises. Differently from standard ones, it contains java packages rpms for different architecture and os release in the same directory.
The behavior that we see is that this task tries to install java for centos8 on centos7 disregarding the architecture that it runs on.
for example instead of
java-1.8.0-openjdk-....el7_5.x86_64.rpm
it will take
java-1.8.0-openjdk-....el8_4.x86_64.rpm
though it running on el7
Is this desired behavior for this ansible yum module when running "latest" or is it bug?

yum_repository module fails properly insert *repo configuration file

there, I am having an issue if anybody had encountered and solved it, please share your knowledge.
Machine:
CentOS Linux release 7.6.1810 (Core)
NAME="CentOS Linux"
epel.yml
- name: Add repository
yum_repository:
name: epel
description: epel-repo
baseurl: https://download.fedoraproject.org/pub/epel/$releasever/$basearch/
ansible-playbook epel.yml (I have removed not necessary part of the epel.yml)
Above, code when run successfully enters epel.repo in /etc/yum.repos.d/ folder. However, when I try to install any package it gives me en error referring "Failed to connect. Network is unreachable"
I have checked #cat /etc/yum.repos.d/epel.repo
baseurl=https://download.fedoraproject.org/pub/epel///
I searched for where $releasever adn $basearch variables come from? Not very concrete answers around.
Please help.
It seems like yum couldn't determine $releasever and $basearch. Check this post for the possible reasons why this wasn't possible.
To workaround the problem, you could try using the yum module instead:
- name: install the latest version of epel
yum:
name: epel-release
state: latest
Or install it directly from the rpm package:
- name: install from url
yum:
name: https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
state: present

Skip package download if already installed

I am using ansible to install a deb package and I do not want to download the remote file if the package is already installed. Currently I am doing like this:
- name: Check if elasticsearch installed
become: true
stat: path=/etc/elasticsearch/elasticsearch.yml
register: st
- name: Install elasticsearch
become: yes
apt:
deb: https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.12.deb
update_cache: true
state: present
when: not st.stat.exists
Is there a better way to skip the download of the deb package if it is already installed?
You'll want package_facts or, of course, to just cheat and shell out something like command: dpkg --search elasticsearch
- name: gather installed packages
package_facts:
- name: Install elasticsearch
when: elasticsearch not in ansible_facts.packages
Unless your question is about how to do that when elasticsearch might have been installed by hand, and not through dpkg, in which case your stat: and register: approach is a sensible one. You may even want to use a with_items: to check a few places the file might have been installed, depending on your circumstance

No package matching 'maven' found available, installed or updated.

When i use yum module in an ansible playbook to install maven on the target server, i get this error message: No package matching 'maven' found available, installed or updated
But when i log on to the target server, i could successfully install maven with yum install maven
Not sure what is wrong with the playbook. Where should i look for any further logs or how to resolve this issue?
playbook task looks like:
- name: install maven (and other packages if needed)
yum: name=maven state=latest
become: true
Oh, I found that yum module doesn't update cache by default, but rpm (on command line) does.
Try to add update_cache: yes to yum module parameters.

Is it safe to use ansible's package module to upgrade all packages?

I have a numer of ansible playbooks I use to perform certain actions on Debian and CentOS VMs. Until now, when I needed to handle packages I would use ansible modules apt and yum. Consequently, I had to check which OS was installed and then use the correct ansible module.
However, I've recently learned there's a module called package which somehow unifies package managers, reducing playbook's complexity, so I'm now trying to use this module only.
One of the operations I want to perform is to update all packages. The problem is that ansible's documentation doesn't say how to do this using package. It does say, however, how to do it with apt and yum: the first one uses and upgrade operation and the latter suggests writing something like:
yum: name=* state=latest
For this reason I thought I could use package to do the following:
package: name=* state=latest
When I ran this playbook I didn't come across any error but this isn't documented anywhere and apt and yum don't work exactly the same way. So my question is: is the command above a safe one as in does it actually do what I want it to do?
Thanks in advance!
EDIT:
I have found out that using package: name=* state=latest will try to install or update all available packages instead of just updating installed ones. Therefore this isn't a valid solution for me. Alternatively, I can keep using yum and apt for this particular action, but I do wonder why is it not possible to do this using package, or if I'm missing something.
Well after further testing I can now answer the question by myself, and the answer is no. Using package: name=* state=latest on a Debian machine will try to install or update every package available in the repositories, so it can't be used as a replacement for an apt-get upgrade.
From the docs below i think its clear that the use you are describing can be considered safe.
From package documentation:
This module actually calls the pertinent package modules for each system (apt, yum, etc).
So whatever you pass to it should be supported by the underlined packagers yum or apt.
From apt documentation:
Name wildcards (fnmatch) like apt* and version wildcards like foo=1.0* are also supported.
From yum->name documentation:
When using state=latest, this can be '*' which means run: yum -y update.
EDIT: After #Xavier Alvarez testing the apt: name=* state=latest will install all packages in repo.

Resources