I am trying to update packages on my VM image using ansible apt module.
Currently I have a patches file which has name : patches.txt which has the following content:
patch old_version new_version
My yml file for the patch_role looks like this:
- set_fact:
patch_file: "{% if vm_patch_file == 'patches.txt' %}{{ lookup('file', '{{ vm_patch_file }}').split(',') }}{% else %}{{ lookup('file', '{{ vm_patch_file }}'.splitlines() }}{% endif %}"
# print file contents
- debug:
msg: "{{ patch_file }}"
# Run dpkg configure to resolve any package deployment issues
- name: run dpkg configure
shell: :dpkg --configure -a"
# Parse through each line of the file and update patches
- name: patch packages
apt:
name: "{{ item.split()[0] }}={{ item.split[2] }}"
with_items:
- "{{ patch_file }}"
After running this,I would like solution to two types of errors:
Unable to correct problems, you have held broken packages - This should have been fixed by dpkg configure
Version 'x' for 'package_y' was not found - I would like to use the latest available version instead of the one mentioned in patches.txt in this case.
I appreciate any suggestions to mitigate both the issues. I do not have direct access to the vm on which this is executed. I can only make changes to the yml file or any other code changes .
My Linux box (busybox) using read-only filesystems mostly. I have option to install my programs different path like PATH=/opt/bin:/opt/sbin. The package manager also sitting in this folder (exec. file name: /opt/bin/opkg) .
When I want to use Ansible opkg module I got the following error:
"Failed to find required executable opkg in paths: /bin:/usr/bin:/bin:/usr/sbin:/sbin"
Question: How can I say to my Ansible to look for opkg package in different path?
Any ideas are welcome!
Thank you!
I found some useful link:
https://docs.ansible.com/ansible/latest/reference_appendices/faq.html
Ansible - Set environment path as inventory variable
And here is my example:
---
- hosts: CBOX-0001
gather_facts: True
gather_subset:
- "!all"
environment:
PATH: "/opt/bin:/opt/sbin:/usr/bin:/usr/sbin:{{ ansible_env.PATH }}"
collections:
- community.general
tasks:
- name: "install opkg packages"
opkg:
name: "{{ item }}"
state: present
with_items:
- screen
- mc
- rclone
I have a inventory file with below details.This pkgs-list should be call by ansible module in main.yml.But its not calling.What am i doing wrong here?
[target:vars]
pkgs_list = vim cloudnetconfig
Main.yml has below module
- name: Install all packages
zypper:
name:"{{ item }}"
state:present
with_items: '{{ pkgs_list }}"
error says "No provider found".
It looks like you've made pkgs_list a string. To use compose data types (dicts and lists) it's better to use yaml inventory.
inventory.yaml
target:
hosts:
example:
vars:
pkgs_list: [vim, cloudnetconfig]
I have a simple playbook and return available update package name as below. I would like to filter the output start with specific letter example to get package name start with 'n' letter. Any thoughts would be much appreciated :-)
---
- name: yum list updates
hosts: all
tasks:
- name: get updates list
yum:
list=updates
register: yum
- name: set fact
set_fact:
package_name: "{{ yum.results | map(attribute='name')| list }}"
Try
package_name: "{{ yum.results|selectattr('name', 'search', '^n')|list }}"
(not tested)
I'm running a playbook which defined several packages to install via apt:
- name: Install utility packages common to all hosts
apt:
name: "{{ item }}"
state: present
autoclean: yes
with_items:
- aptitude
- jq
- curl
- git-core
- at
...
A recent ansible update on my system now renders this message concerning the playbook above:
[DEPRECATION WARNING]: Invoking "apt" only once while using a loop via squash_actions is deprecated. Instead of
using a loop to supply multiple items and specifying `name: {{ item }}`, please use `name: [u'aptitude',
u'jq', u'curl', u'git-core', u'at', u'heirloom-mailx', u'sudo-ldap', u'sysstat', u'vim', u'at', u'ntp',
u'stunnel', u'sysstat', u'arping', u'net-tools', u'lshw', u'screen', u'tmux', u'lsscsi']` and remove the loop.
If I'm understanding this correctly, Ansible now wants this list of packages as an array which leaves this:
name: [u'aptitude', u'jq', u'curl', u'git-core', u'at','heirloom-mailx', u'sudo-ldap', u'sysstat', u'vim', u'at', u'ntp',u'stunnel', u'sysstat', u'arping', u'net-tools', u'lshw', u'screen', u'tmux', u'lsscsi']
Is there a better way? Just seems like I'll be scrolling right forever in VIM trying to maintain this. Either that, or word wrap it and deal with a word-cloud of packages.
You can code the array in YAML style to make it more readable:
- name: Install utility packages common to all hosts
apt:
name:
- aptitude
- jq
- curl
- git-core
- at
state: present
autoclean: yes
I had this same question and it looks like each set of packages with the same states will have to be their own block. Looking at Ansible's documentation, they have a block for each state as an example so I took that example, cut up my packages based off their states and followed ignacio's example and it ended up working perfectly.
So basically it would look like this
- name: Install packages required for log-deployment
apt:
name:
- gcc
- python-devel
state: latest
autoclean: yes
- name: Install packages required for log-deployment
apt:
name:
- python
- mariadb
- mysql-devel
state: installed
Hope that makes sense and helps!
I came across this exact same problem , but with a much longer list of apps , held in a vars file. This is the code I implemented to get around that problem. The list of the apps is placed into the "apps" variable and Ansible iterates over that.
- name: Install default applications
apt:
name: "{{item}}"
state: latest
loop: "{{ apps }}"
when: ansible_distribution == 'Ubuntu' or ansible_distribution == 'Debian'
tags:
- instapps
The file holding the list of apps to install is in the Defaults directory in the role directory for this task - namely the "common" role directory.
roles
- common
- Defaults
- main.yml
For a record, you can have this written this way:
- name: Install utility packages common to all hosts
apt:
state: present
autoclean: yes
pkg: [
"aptitude",
"jq",
"curl",
"git-core",
"at",
]
however if you are upgrading existing roles to new apt's requirements, Ignacio's accepted answer is far better as all you need is just to add some indentation to already existing entries.