Ansible playbook not working, web VM's will not install/remove/update

A 0 Reputation points
2024-09-05T18:35:29.76+00:00

I am trying to run an ansible playbook to setup one of my web VM's to remove and/or install certain packages so we can start our week of web applications (cyber blog project). I use Microsoft Azure. SSH and HTTP are allowed.

Below is the playbook:


---

- name: Config Web VM with Docker

  hosts: webservers

  become: true

  tasks:

    - name: Uninstall apache if needed

      ansible.builtin.apt:

        update_cache: yes

        name: apache2

        state: absent

    - name: docker.io

      ansible.builtin.apt:

        update_cache: yes

        name: docker.io

        state: present

    - name: Install pip3

      ansible.builtin.apt:

        force_apt_get: yes

        name: python3-pip

        state: present

    - name: Install Docker python module

      pip:

        name: docker

        state: present

        extra_args: --break-system-packages

    - name: revert requests to 2.31.0 to bypass https://github.com/docker/docker-py/issues/3256

      ansible.builtin.command:

        cmd: pip install --force-reinstall requests==2.31.0

    - name: download and launch a docker web container

      docker_container:

        name: dvwa

        image: cyberxsecurity/dvwa

        state: started

        published_ports: 80:80

        restart_policy: always

    - name: Enable docker service

      systemd:

        name: docker

        enabled: yes

When running this playbook, I get:


root@2b61bbccc5f2:/etc/ansible# ansible-playbook pentest.yml

[WARNING]: ansible.utils.display.initialize_locale has not been called, this may result in incorrectly calculated text

widths that can cause Display to print incorrect line lengths

PLAY [Config Web VM with Docker] ***************************************************************************************

TASK [Gathering Facts] *************************************************************************************************

ok: [10.0.0.5]

ok: [10.0.0.6]

TASK [Uninstall apache if needed] **************************************************************************************

fatal: [10.0.0.6]: FAILED! => {"changed": false, "msg": "Failed to update apt cache: unknown reason"}

fatal: [10.0.0.5]: FAILED! => {"changed": false, "msg": "Failed to update apt cache: unknown reason"}

PLAY RECAP *************************************************************************************************************

10.0.0.5                   : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

10.0.0.6                   : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

The errors:

fatal: [10.0.0.6]: FAILED! => {"changed": false, "msg": "Failed to update apt cache: unknown reason"}

fatal: [10.0.0.5]: FAILED! => {"changed": false, "msg": "Failed to update apt cache: unknown reason"}

Below is a list of the steps I have taken to create my virtual machines:

  1. Create resource group
  2. Create virtual network
  3. Create network security group
  4. Create jumpbox VM
  5. Create and add SSH id_rsa.pub key to jumpbox VM from personal computer
  6. Add inbound rule to allow SSH from personal computer to jumpbox in network security group
  7. Test if SSH works to jumpbox VM from personal computer (it does)
  8. Update the jumpbox VM doing sudo apt-get update (it completes fine)
  9. Install docker.io using sudo apt-get install docker.io
  10. Pull the "cyberxsecurity/ansible" image using sudo docker pull cyberxsecurity/ansible
  11. Run an ansible container using sudo docker run -it cyberxsecurity/ansible /bin/bash
  12. Create and add SSH id_rsa.pub key from within the ansible container
  13. Create web1 VM, and create an availability set
  14. Add SSH id_rsa.pub key to web1 VM from ansible container
  15. Add inbound rule to allow SSH from jumpbox within the virtual network
  16. Test if SSH works to web1 VM (it does)
  17. Repeat and create web2 VM with same SSH id_rsa.pub key and with same availability set
  18. Test if SSH works to web2 VM (it does)
  19. From ansible container, nano ansible.cfg to add remote_user using the admin of azure's username
  20. From ansible container, nano "hosts" to add the web(s) VM' internal IP, as well as add ansible_python_interpreter=/usr/bin/python3
  21. From ansible container, nano "pentest.yml" with custom playbook (it works)
  22. "Checking Facts" works and connects through SSH
  23. Tasks then fail and hang

I tried the playbook without the apache2 task, starting with docker.io - I get this error:


root@2b61bbccc5f2:/etc/ansible# ansible-playbook pentest.yml

[WARNING]: ansible.utils.display.initialize_locale has not been called, this may result in incorrectly calculated text

widths that can cause Display to print incorrect line lengths

PLAY [Config Web VM with Docker] ***************************************************************************************

TASK [Gathering Facts] *************************************************************************************************

ok: [10.0.0.6]

ok: [10.0.0.5]

TASK [docker.io] *******************************************************************************************************

fatal: [10.0.0.6]: FAILED! => {"changed": false, "msg": "Failed to lock apt for exclusive operation: Failed to lock directory /var/lib/apt/lists/: E:Could not get lock /var/lib/apt/lists/lock. It is held by process 2324 (apt-get)"}

fatal: [10.0.0.5]: FAILED! => {"changed": false, "msg": "Failed to lock apt for exclusive operation: Failed to lock directory /var/lib/apt/lists/: E:Could not get lock /var/lib/apt/lists/lock. It is held by process 2659 (apt-get)"}

PLAY RECAP *************************************************************************************************************

10.0.0.5                   : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

10.0.0.6                   : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

The errors:

fatal: [10.0.0.6]: FAILED! => {"changed": false, "msg": "Failed to lock apt for exclusive operation: Failed to lock directory /var/lib/apt/lists/: E:Could not get lock /var/lib/apt/lists/lock. It is held by process 2324 (apt-get)"}

fatal: [10.0.0.5]: FAILED! => {"changed": false, "msg": "Failed to lock apt for exclusive operation: Failed to lock directory /var/lib/apt/lists/: E:Could not get lock /var/lib/apt/lists/lock. It is held by process 2659 (apt-get)"}

I then SSH into a web VM to do these manually; this is what I get when removing apache2 using sudo apt-get remove apache2:


Reading package lists... Done

Building dependency tree

Reading state information... Done

Package 'apache2' is not installed, so not removed

0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

That is okay (or is it?)

I then try to install docker.io, using sudo apt-get install docker.io:


Reading package lists... Done

Building dependency tree

Reading state information... Done

Package docker.io is not available, but is referred to by another package.

This may mean that the package is missing, has been obsoleted, or

is only available from another source

E: Package 'docker.io' has no installation candidate

This doesn't make any sense because on the jumpbox machine, it installs perfectly normal. Everything on the jumpbox machine works fine.

So, I try a suggested command when Googling and it is to use sudo apt-get update from the target VM itself. This is what I get:

  • First, it will hang on this for some time:

0% [Connecting to azure.archive.ubuntu.com (20.53.66.23)]

  • Second, it will spit out these errors:

myadmin@myweb1:~$ sudo apt-get update

Err:1 http://azure.archive.ubuntu.com/ubuntu focal InRelease

  Could not connect to azure.archive.ubuntu.com:80 (20.53.66.23), connection timed out

Err:2 http://azure.archive.ubuntu.com/ubuntu focal-updates InRelease

  Unable to connect to azure.archive.ubuntu.com:http:

Err:3 http://azure.archive.ubuntu.com/ubuntu focal-backports InRelease

  Unable to connect to azure.archive.ubuntu.com:http:

Err:4 http://azure.archive.ubuntu.com/ubuntu focal-security InRelease

  Unable to connect to azure.archive.ubuntu.com:http:

Reading package lists... Done

W: Failed to fetch http://azure.archive.ubuntu.com/ubuntu/dists/focal/InRelease  Could not connect to azure.archive.ubuntu.com:80 (20.53.66.23), connection timed out

W: Failed to fetch http://azure.archive.ubuntu.com/ubuntu/dists/focal-updates/InRelease  Unable to connect to azure.archive.ubuntu.com:http:

W: Failed to fetch http://azure.archive.ubuntu.com/ubuntu/dists/focal-backports/InRelease  Unable to connect to azure.archive.ubuntu.com:http:

W: Failed to fetch http://azure.archive.ubuntu.com/ubuntu/dists/focal-security/InRelease  Unable to connect to azure.archive.ubuntu.com:http:

W: Some index files failed to download. They have been ignored, or old ones used instead.

What in the heck do I do?

EDIT: Extra info

When running the playbook to install pip3:

root@2b61bbccc5f2:/etc/ansible# ansible-playbook pentest.yml
[WARNING]: ansible.utils.display.initialize_locale has not been called, this may result in incorrectly calculated text
widths that can cause Display to print incorrect line lengths

PLAY [Config Web VM with Docker] ***************************************************************************************

TASK [Gathering Facts] *************************************************************************************************
ok: [10.0.0.5]
ok: [10.0.0.6]

TASK [Install pip3] ****************************************************************************************************
fatal: [10.0.0.5]: FAILED! => {"changed": false, "msg": "No package matching 'python3-pip' is available"}
fatal: [10.0.0.6]: FAILED! => {"changed": false, "msg": "No package matching 'python3-pip' is available"}

PLAY RECAP *************************************************************************************************************
10.0.0.5                   : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0
10.0.0.6                   : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

The errors:

fatal: [10.0.0.5]: FAILED! => {"changed": false, "msg": "No package matching 'python3-pip' is available"}

fatal: [10.0.0.6]: FAILED! => {"changed": false, "msg": "No package matching 'python3-pip' is available"}

Steps done to troubleshoot:

  • Manually ran tasks (commands)
  • Restarted all resources on Azure to start fresh
  • Changed regions on Azure to try and see if connection was an issue
  • Tried different Ubuntu LTS versions (24 will not work with ansible for me/ needs to be 20.04 or less)
  • Tried updating VM's manually (jumpbox works fine)
Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,792 questions
Azure
Azure
A cloud computing platform and infrastructure for building, deploying and managing applications and services through a worldwide network of Microsoft-managed datacenters.
1,161 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.