1

When i try to install a list of packages('apache2', 'libapache2-mod-wsgi', 'python-pip', 'python-virtualenv') on on a group of hosts (app01 and app02), it fails randomly either on app01 or app02. After a first few retries the required packages are already installed on the host. Subsequent execution continue to fail randomly on app01 or app02 instead of returning a simple success.

The ansible controller and hosts are all running ubuntu 16.04

The controller has ansible 2.8.4

I have already set "become: yes" in playbook. I have already tried running apt-get clean and apt-get update. on the hosts

Inventory file

[webserver]
app01 ansible_python_interpreter=python3
app02 ansible_python_interpreter=python3

Playbook

---
- hosts: webserver
  tasks:
    - name: install web components
      become: yes
      apt: 
        name: ['apache2', 'libapache2-mod-wsgi', 'python-pip', 'python-virtualenv'] 
        state: present 
        update_cache: yes

In the first run , the execution fails on host app01

In the second run, the execution fails on host app02

1 st Run

shekhar@control:~/ansible$ ansible-playbook -v playbooks/webservers.yml
Using /home/shekhar/ansible/ansible.cfg as config file

PLAY [webserver] ******************************************************************************

TASK [Gathering Facts] ******************************************************************************
ok: [app01]
ok: [app02]

TASK [install web components] ******************************************************************************
fatal: [app01]: FAILED! => {"changed": false, "msg": "Failed to lock apt for exclusive operation"}
ok: [app02] => {"cache_update_time": 1568203558, "cache_updated": false, "changed": false}

PLAY RECAP ******************************************************************************
app01                      : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   
app02                      : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

2nd Run

shekhar@control:~/ansible$ ansible-playbook -v playbooks/webservers.yml
Using /home/shekhar/ansible/ansible.cfg as config file

PLAY [webserver] ******************************************************************************

TASK [Gathering Facts] ******************************************************************************
ok: [app01]
ok: [app02]

TASK [install web components] ******************************************************************************
fatal: [app02]: FAILED! => {"changed": false, "msg": "Failed to lock apt for exclusive operation"}
ok: [app01] => {"cache_update_time": 1568203558, "cache_updated": false, "changed": false}

PLAY RECAP ******************************************************************************
app01                      : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
app02                      : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0  
nitin3685
  • 825
  • 1
  • 9
  • 20
  • Might be very similar to [this question](https://stackoverflow.com/questions/45269225/ansible-playbook-fails-to-lock-apt). Make sure you disabled automatic updates on your hosts. More generally (if automatic update is not the problem), check what is causing apt to launch and acquire a lock on your hosts outside of you running your playbook. – Zeitounator Sep 11 '19 at 12:28
  • 1
    Are you sure that app01 and app02 are 2 different hosts! otherwise it sounds normal that the 2 jobs fight against each other… – xenlo Sep 11 '19 at 12:32
  • @xenlo : You are right. My bad. My /etc/hosts file was populated incorrectly with app01 and app02 pointing to same machine, although in reality they are different machine. Once i fixed that, the issue disappeared. In such cases i do not know the stackoverflow etiquette. Should i delete the question? or you will you post your comment as answer so that i can upvote it and accept the answer. – nitin3685 Sep 12 '19 at 06:58
  • @nitin3685, no idea niether how we should do. But your proposition sounds, me copying my comment as answer, looks good to me. – xenlo Sep 12 '19 at 11:24

1 Answers1

2

The behavior is a bit strange… one succeed, and other failing each time.

My guess is that your app01 and app02 are actually the same host! If so, it sounds normal that the 2 jobs fight against each other.

xenlo
  • 761
  • 1
  • 7
  • 21
  • 1
    You are right. My /etc/hosts file was populated incorrectly with app01 and app02 pointing to same machine, although in reality they are different machine. Once i fixed that, the issue disappeared. – nitin3685 Sep 12 '19 at 11:33