ansible yum packages displayed in both available and installed result - ansible

ansible yum list shows some packages as installed as well available. how this can be fixed. when i try run yum list package-name it displays either as installed/avaiable.
hosts: localhost
vars:
pkgs: [ 'python36', 'python38' ]
tasks:
- name: installed packages
yum:
list: installed
register: installed
- name: available packages
yum:
list: available
register: avlbl
When I see output of both register variable i can see python36 & python38 displayed in both register variable.

Related

Static URL that always links to the latest Go binaries? [duplicate]

Is there is a permanent URL that points to current latest linux binary release of Go?
I am writing an ansible script which should download the latest Go release and install it. In the Go download site "https://golang.org/dl/" I could see only release specific download links.
I am wondering if there a link like "https://dl.google.com/go/latest.linux-amd64.tar.gz" available?
If not any suggestion on how to script fetching the latest golang version and install it?
You can download the latest stable Go version in one go :)
wget "https://dl.google.com/go/$(curl https://golang.org/VERSION?m=text).linux-amd64.tar.gz"
You can generate the latest url with :
https://dl.google.com/go{{ version }}.{{ os }}-{{ arch }}.tar.gz
os: linux, darwin, windows, freebsd
arch: amd64, 386, armv6l, arm64, s390, ppc64le
and for the latest stable version you can fetch the value with curl or something else from the url :
https://golang.org/VERSION?m=text
Here is an ansible playbook as an example :
---
- hosts: server
gather_facts: no
vars:
version : "latest"
arch: arm64
os: linux
latest_version_url: https://golang.org/VERSION?m=text
archive_name: "{{ filename_prefix }}.{{ os }}-{{ arch }}.tar.gz"
download_url: https://dl.google.com/go/{{ archive_name }}
bin_path: /usr/local/go/bin
tasks:
- name: Get filename prefix with latest version
set_fact:
filename_prefix: "{{ lookup('url', latest_version_url, split_lines=False) }}"
when: version == "latest"
- name: Get filename prefix with fixed version
set_fact:
filename_prefix: go{{ version }}
when: version != "latest"
- name: Try to get current go version installed
command: go version
register: result
changed_when: false
- name: Set current_version var to the current
set_fact:
current_version: "{{ result.stdout.split(' ')[2] }}"
when: result.failed == false
- name: Set current_version var to none
set_fact:
current_version: "none"
when: result.failed == true
- debug:
var: current_version
- name: Download and extract the archive {{ archive_name }}
become: true
unarchive:
src: "{{ download_url }}"
dest: /usr/local
remote_src: yes
when: current_version != filename_prefix
As found here, Google has a Linux installer to install Go on linux:
https://storage.googleapis.com/golang/getgo/installer_linux
This installer fetches the latest version of Go and installs it. Seems like this is the easiest way as of now to install the latest go version on Linux.
The newest official method to fetch the file and execute it is:
curl -LO https://get.golang.org/$(uname)/go_installer
chmod +x go_installer
./go_installer
rm go_installer
I use the below for Linux:
wget "https://go.dev/dl/$(curl 'https://go.dev/VERSION?m=text').linux-amd64.tar.gz" && sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go*.linux-amd64.tar.gz
There is also a cross-platform solution built by the Go Community:
curl -LO https://get.golang.org/$(uname)/go_installer && chmod +x go_installer && ./go_installer && rm go_installer
I would suggest to defer to something like jlund/ansible-go, or copy over the parts of that role that you need.

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

How to pass options to dnf when using 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

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

Ansible: prevent 'changed' when looping

I like this pattern in Ansible:
tasks:
- name: install packages
apt:
name: "{{ item }}"
update_cache: yes
cache_valid_time: 3600
state: latest
with_items:
- build-essential
- git
- libjpeg-dev
However, I get a changed notification every time, even though nothing has been installed. I don't want to set changed_when: False. Is there a way to get the proper changed status from this loop?
Update If anything was installed by apt, I want changed to be True. If everything was already installed, and apt did not do any work when looping over this list, then I want changed to be False.
I'm using Ansible 2.0.1.0.
The changed indication is due the apt cache (apt-get update) being executed each time the task is run. If you only want an indication of whether packages were installed then remove the update_cache: yes directive or set to update_cache: no.
Here's what it should be, after applying the accepted answer by #Petro026.
tasks:
- name: install packages
apt:
name: "{{ item }}"
state: latest
with_items:
- build-essential
- git
- libjpeg-dev

Resources