Apt install with ansible

3.6k views Asked by At

I use ansible scripts to set up my environment (Ubuntu) in Amazon EC2 and a vagrant box. Then I try to set up the same environment at GermanVPS (Ubuntu Minimal). Installing packages with Apt doesn't seem to work.

I run

ansible-playbook -i ansible/live -u priidu ansible/caselaw.yml -s -vvvv --start-at-task="install"

which gives the following error.

failed: [master] => (item=postgresql-9.4,postgresql-contrib-9.4,postgresql-server-dev-9.4,python-psycopg2) => {"failed": true, "item": "postgresql-9.4,postgresql-contrib-9.4,postgresql-server-dev-9.4,python-psycopg2"}
stderr: E: dpkg was interrupted, you must manually run 'sudo dpkg --configure -a' to correct the problem. 

msg: '/usr/bin/apt-get -y -o "Dpkg::Options::=--force-confdef" -o "Dpkg::Options::=--force-confold"   install 'postgresql-server-dev-9.4' 'python-psycopg2'' failed: E: dpkg was interrupted, you must manually run 'sudo dpkg --configure -a' to correct the problem. 


FATAL: all hosts have already failed -- aborting

Then I try

ansible-playbook -i ansible/live -u priidu ansible/caselaw.yml -s -vvvv --start-at-task="dpkg configure"

and I get the following error message

failed: [master] => (item=postgresql-9.4,postgresql-contrib-9.4,postgresql-server-dev-9.4,python-psycopg2) => {"failed": true, "item": "postgresql-9.4,postgresql-contrib-9.4,postgresql-server-dev-9.4,python-psycopg2", "parsed": false}
BECOME-SUCCESS-grensagvuewehoylwpjytnkzpwwqgsre
Killed
OpenSSH_6.2p2, OSSLShim 0.9.8r 8 Dec 2011
debug1: Reading configuration data /Users/priidukull/.ssh/config
debug1: Reading configuration data /etc/ssh_config
debug1: /etc/ssh_config line 51: Applying options for *
debug1: auto-mux: Trying existing master
debug2: fd 3 setting O_NONBLOCK
debug2: mux_client_hello_exchange: master version 4
debug3: mux_client_forwards: request forwardings: 0 local, 0 remote
debug3: mux_client_request_session: entering
debug3: mux_client_request_alive: entering
debug3: mux_client_request_alive: done pid = 54431
debug3: mux_client_request_session: session request sent
debug1: mux_client_request_session: master session id: 2
debug3: mux_client_read_packet: read header failed: Broken pipe
debug2: Received exit status from master 0
Shared connection to <<hostname>> closed.


FATAL: all hosts have already failed -- aborting

What is going on there and how can I start solving the puzzle?

Edit: The relevant part of my playbook

- name: dpkg configure
  shell: dpkg --configure -a

- name: install
  apt: name={{ item }} update_cache=yes
  with_items:
    - postgresql-9.4
    - postgresql-contrib-9.4
    - postgresql-server-dev-9.4
    - python-psycopg2

Edit 2: Added the playbook commands.

Edit 3: The issue vanished when I increased RAM from 64MB to 512MB.

3

There are 3 answers

0
Zach.Z On

I met this issue when i try to install docker via ansible-playbook.

I'v try to increase the RAM from 512Mb to 1024Mb. But it didn't work.

And then I found the solution as below:

Delete all the files in /var/lib/dpkg/updates and try again !!

Anyway it works for me!

0
GANESH CHOKHARE On

I added some tasks to remove dpkg problem of Ubuntu 16.04 and It works well

tasks:
- name: configure dpkg 1
  command: sudo rm /var/lib/apt/lists/lock
  become: true

- name: configure dpkg 2
  command: sudo rm /var/cache/apt/archives/lock
  become: true

- name: configure dpkg 3
  command: sudo rm /var/lib/dpkg/lock  
  become: true

- name: configure dpkg 4
  command: sudo dpkg --configure -a
  become: true

- name: Update and upgrade apt packages 
  become: true
  apt:
    upgrade: yes
    update_cache: yes
    cache_valid_time: 86400 #One day
1
dshenoy On

There is the yum module within Ansible that can be used to install packages. I was stuck for a while as I found that I had to install other dependencies e.g. unzip. But this has been very useful to keep my code elegant and simple.

This link provides documentation to module: http://docs.ansible.com/yum_module.html

For example, to install the latest version of Apache:

yum: name=httpd state=latest