Problems when installing git on my targets - ansible

I have some problems when I try to install git on my target node.
1st method: I used the ansible command
ansible <ip-node> -u root -b -K -m raw -a "apt install -y git"
and I have this error on my controller node terminal:
E: Impossible de récupérer certaines archives, peut-être devrez-vous lancer apt-get update ou essayer avec --fix-missing ? )
2nd method: I played the following playbook
name: This sets up an git
hosts: vm2
tasks:
- name: install git
apt:
name: git
state: present
cache_update: True
I get an other error
fatal: [192.168.57.10]: FAILED! => {"changed": false, "msg": "Unsupported parameters for (apt) module: cache_update. Supported parameters include: allow_downgrade (allow-downgrade, allow-downgrades, allow_downgrades), policy_rc_d, autoremove, force_apt_get, update_cache_retry_max_delay, fail_on_autoremove, install_recommends (install-recommends), update_cache_retries, default_release (default-release), state, autoclean, cache_valid_time, only_upgrade, deb, purge, allow_unauthenticated (allow-unauthenticated), lock_timeout, upgrade, dpkg_options, package (name, pkg), force, update_cache (update-cache)."})
My questions are:
How can I debug the above errors?
I'm not sure but I suspect my errors happen because my target node does not have internet access. For example, ping google.fr fails. Could this be the issue?
What should I change in my target node network configuration to fix my issues?

Note that you tried cache_update, it should be update_cache as the error message tells. Tip: the package module chooses the package manager on your target machines (apt, rpm, etc.)
- name: Install git
become: true
become_method: sudo
become_user: root
package:
name: git
state: present
update_cache: true

Related

Ansible Run Shell Command Upon Condition in Multiple Hosts

I have the following script that attempts to install a package on a node only when not already installed.
- name: check if linux-modules-extra-raspi is installed # Task 1
package:
name: linux-modules-extra-raspi
state: present
check_mode: true
register: check_raspi_module
- name: install linux-modules-extra-raspi if not installed # Task 2
shell: |
sudo dpkg --configure -a
apt install linux-modules-extra-raspi
when: not check_raspi_module.changed
But the problem here is that if I have a set of hosts, the Task 1 runs for node n1 and registeres check_raspi_module to false and then Task 1 runs for node n2 and then sets it to true because that package is already available in node n2. So how can I throttle this and have the check_raspi_module local to a task and not global like it is now?
If you need to install package, you have just to use the first bloc like below. You haven't need to use block of check and install separatly.
Even if your package is installed, Ansible will detect it and not reinstall it. It’s the principe of Ansible
The documentation: here
(definition) state: present (mean install if not present)
- name: install if not present if linux-modules-extra-raspi
ansible.builtin.package:
name: linux-modules-extra-raspi
state: present

Install apache2 sqlite3 git - Ansible Loop

I have read all the similar questions here and yet I don't see a solution that fixed my issue.
This is my code:
---
- name: install apache2, sqlite3, git
tasks:
- name: Install list of packages
apt:
name: "{{ item }}"
state: installed
with_items:
- apache2
- sqlite3
- git
Here is the error:
ERROR! unexpected parameter type in action: <class 'ansible.parsing.yaml.objects.AnsibleSequence'>
The error appears to be in '/projects/challenge/fresco_loops/tasks/main.yml': line 2, column 3, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
---
- name: install apache2, sqlite3, git
^ here
I am not really an ansible expert and this is just one of the trainings we have to take. Thank you in advance.
firstly the best way to install multiple packages is shown below:
---
- name: install apache2, sqlite3, git
hosts: localhost # run locally
become: yes
tasks:
- name: Install list of packages
apt:
state: present
name:
- apache2
- sqlite3
- git
And a couple of final points:
"installed" is not a valid option for apt, try "present".
package installation requires sudo (become: yes).
To run the code above, which is locally on the node where the playbook sits, you need to use the command: ansible-playbook playbook.yml --ask-become-pass and enter the sudo password when prompted.
Secondly when I tried to run your code I got the following error
[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: ['apache2', 'sqlite3',
'git']` and remove the loop. This feature will be removed in version 2.11. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
failed: [localhost] (item=['apache2', 'sqlite3', 'git']) => {"ansible_loop_var": "item", "changed": false, "item": ["apache2", "sqlite3", "git"], "msg": "value of state must be one of: absent, build-dep, fixed, latest, present, got: installed"}
When using Ansible 2.9.6, if you are using a different version that might explain it? Its always worth telling people what version you are using, in case the problem is version-specific.
Otherwise your code snippet is not representative of what is actually giving you the error.

Ansible playbook fails to lock apt

I took over a project that is running on Ansible for server provisioning and management. I'm fairly new to Ansible but thanks to the good documentation I'm getting my head around it.
Still I'm having an error which has the following output:
failed: [build] (item=[u'software-properties-common', u'python-pycurl', u'openssh-server', u'ufw', u'unattended-upgrades', u'vim', u'curl', u'git', u'ntp']) => {"failed": true, "item": ["software-properties-common", "python-pycurl", "openssh-server", "ufw", "unattended-upgrades", "vim", "curl", "git", "ntp"], "msg": "Failed to lock apt for exclusive operation"}
The playbook is run with sudo: yes so I don't understand why I'm getting this error (which looks like a permission error). Any idea how to trace this down?
- name: "Install very important packages"
apt: pkg={{ item }} update_cache=yes state=present
with_items:
- software-properties-common # for apt repository management
- python-pycurl # for apt repository management (Ansible support)
- openssh-server
- ufw
- unattended-upgrades
- vim
- curl
- git
- ntp
playbook:
- hosts: build.url.com
sudo: yes
roles:
- { role: postgresql, tags: postgresql }
- { role: ruby, tags: ruby }
- { role: build, tags: build }
I just had the same issue on a new VM. I tried many approaches, including retrying the apt commands, but in the end the only way to do this was by removing unattended upgrades.
I'm using raw commands here, since at this point the VM doesn't have Python installed, so I need to install that first, but I need a reliable apt for that.
Since it is a VM and I was testing the playbook by resetting it to a Snapshot, the system date was off, which forced me to use the date -s command in order to not have problems with the SSL certificate during apt commands. This date -s triggered an unattended upgrade.
So this snippet of a playbook is basically the part relevant to disabling unattended upgrades in a new system. They are the first commands I'm issuing on a new system.
- name: Disable timers for unattended upgrade, so that none will be triggered by the `date -s` call.
raw: systemctl disable --now {{item}}
with_items:
- 'apt-daily.timer'
- 'apt-daily-upgrade.timer'
- name: Reload systemctl daemon to apply the new changes
raw: systemctl daemon-reload
# Syncing time is only relevant for testing, because of the VM's outdated date.
#- name: Sync time
# raw: date -s "{{ lookup('pipe', 'date') }}"
- name: Wait for any possibly running unattended upgrade to finish
raw: systemd-run --property="After=apt-daily.service apt-daily-upgrade.service" --wait /bin/true
- name: Purge unattended upgrades
raw: apt-get -y purge unattended-upgrades
- name: Update apt cache
raw: apt-get -y update
- name: If needed, install Python
raw: test -e /usr/bin/python || apt-get -y install python
Anything else would cause apt commands to randomly fail because of locking issues caused by unattended upgrades.
This is a very common situation when provisioning Ubuntu (and likely some other distributions). You try to run Ansible while automatic updates are running in background (which is what happens right after setting up a new machine). As APT uses semaphore, Ansible gets kicked out.
The playbook is ok and the easiest way to verify is to run it later (after automatic update process finishes).
For a permanent resolution, you might want to:
use an OS image with automatic updates disabled
add an explicit loop in the Ansible playbook to repeat the failed task until it succeeds

Ansible Yum Module pending transactions error

I'm very new to Ansible.
I am trying to follow a tutorial on the concept of Roles in Ansible.
I have the following Master Playbook:
--- # Master Playbook for Webservers
- hosts: apacheweb
user: test
sudo: yes
connection: ssh
roles:
- webservers
Which refers to the webservers role that has the following task/main.yml:
- name: Install Apache Web Server
yum: pkg=httpd state=latest
notify: Restart HTTPD
And a handler/main.yml:
- name: Restart HTTPD
service: name=httpd state=started
When I execute the Master Playbook, mentioned above, I get the following error:
TASK [webservers : Install Apache Web Server] **********************************
fatal: [test.server.com]: FAILED! => {"changed": false, "failed": true, "msg": "The following packages have pending transactions: httpd-x86_64", "rc": 128, "results": ["The following packages have pending transactions: httpd-x86_64"]}
I cannot understand what this error corresponds to. There does not seem to be anything similar, based on my research, that could suggest the issue with the way I am using the Yum module.
NOTE: Ansible Version:
ansible 2.2.1.0
config file = /etc/ansible/ansible.cfg
It seems there are unfinished / pending transactions on the target host.
Try installing yum-utils package to run yum-complete-transaction to the target hosts giving the error.
# yum-complete-transaction --cleanup-only
Look at Fixing There are unfinished transactions remaining for more details.
yum-complete-transaction is a program which finds incomplete or
aborted yum transactions on a system and attempts to complete them. It
looks at the transaction-all* and transaction-done* files which can
normally be found in /var/lib/yum if a yum transaction aborted in the
middle of execution.
If it finds more than one unfinished transaction it will attempt to
complete the most recent one first. You can run it more than once to
clean up all unfinished transactions.
Unfinished transaction remaining
sudo yum install yum-utils
yum-complete-transaction --cleanup-only
I am using for ansible this type of config for the playbooks:
- name: Install Apache Web Server
yum: name=httpd state=latest
notify: Restart HTTPD
As far as i know there is no such option as yum: pkg=httpd in ansbile for the yum module (if I'm not wrong, that pkg=httpd is for apt-get on debian based distros)
If you need to install multiple packages you could use something like:
- name: "Install httpd packages"
yum: name={{ item }} state=present
with_items:
- httpd
- httpd-devel
- httpd-tools
Of course you can change the state=present to state=latest or whatever option might suits you best
http://docs.ansible.com/ansible/yum_module.html - ansible documentation for yum module

Ansible apt-get install output

I am using vagrant with an ansible playbook to automatically install a bunch of programs on an ubuntu image. One program is failing to install on the vagrant VM. In the Vagrant file I have
config.vm.provision :ansible do |ansible|
ansible.verbose = "vvv"
ansible.playbook = "provisioning/playbook.yml"
end
but the verbose output does not include the apt-get output. My playbook.yml looks like
---
- hosts: all
sudo: true
tasks:
- name: get vi
apt: state=latest name=vim
How can I see the console output of an individual (or all) apt-get install's on the VM since ansible instead outputs each install in the format
TASK: [Install vim] ***********************************************************
failed: [default] => {"failed": true}
...
Reproducing the stdout of apt
Here is how to reproduce the stdout of apt…
---
- name: 'apt: update & upgrade'
apt:
update_cache: yes
cache_valid_time: 3600
upgrade: safe
register: apt
- debug: msg={{ apt.stdout.split('\n')[:-1] }}
…with nice line breaks, thanks to .split('\n'), and omitting the last empty string with [:-1], all of which is Python string manipulation, of course.
"msg": [
"Reading package lists...",
"Building dependency tree...",
"Reading state information...",
"Reading extended state information...",
"Initializing package states...",
"Building tag database...",
"No packages will be installed, upgraded, or removed.",
"0 packages upgraded, 0 newly installed, 0 to remove and 0 not upgraded.",
"Need to get 0 B of archives. After unpacking 0 B will be used.",
"Reading package lists...",
"Building dependency tree...",
"Reading state information...",
"Reading extended state information...",
"Initializing package states...",
"Building tag database..."
]
You can register to a variable the output of the apt module execution and then print it.
- hosts: localhost
sudo: true
tasks:
- name: get vi
apt: state=latest name=vim
register: aptout
# show the content of aptout var
- debug: var=aptout
You can use directly stdout_lines, after registering the output as also shown above, but you must make sure that apt did output something, for example, it installed something, otherwise that element is not defined. Here's a possible example:
- name: Install Gnome Packages
become: yes
apt:
update_cache: yes
state: latest
pkg:
- gnome-tweaks
- dconf-editor
- guake
register: aptout
- debug: msg="{{ aptout.stdout_lines }}"
when: aptout.stdout_lines is defined
In the version of ansible I'm using at the moment, ansible-playbook -v seems sufficient to get apt output. Admittedly I haven't tested failures. The output is in the form of JSON, which makes it a bit hard to read (as the other answer works around).
The Ansible version I tested was 2.3.2.0.

Resources