How to set up apache with Ansible in Ubuntu

This tutorial explains how to install, start, stop Apache on Managed nodes with Ansible, this tutorial assumes that you have basic set up ready for Control and Manged node, in case you don't have set up ready follow links in pre requisite section.

Prerequisite for following this tutorial
  1. Ansible Installation on Control Node : How to install Ansible with PIP in Ubuntu
  2. Control and Managed Node Set up : How to set up Control and Managed nodes in Ansible

Below are the steps for managing Apache with Ansible

On control Node switch to ansible user and navigate to ansible project directory test-ansible


sudo su - ansible;
cd /home/ansible/test-ansible;

Execute below command to install apache2 on Managed Node


ansible@control-node:~/test-ansible$ ansible all -i hosts  -m apt  -a "name=apache2 state=present"

If you are following all the tutorials from pre-requisite section, then will get below error on executing the command


managed-node-1 | FAILED! => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "cache_update_time": 1589737852,
    "cache_updated": false,
    "changed": false,
    "msg": "'/usr/bin/apt-get -y -o \"Dpkg::Options::=--force-confdef\" -o \"Dpkg::Options::=--force-confold\"      install 'apache2'' failed: E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)\nE: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?\n",
    "rc": 100,
    "stderr": "E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)\nE: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?\n",
    "stderr_lines": [
        "E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)",
        "E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?"
    ],
    "stdout": "",
    "stdout_lines": []
}

For fixing this error append below entries in ansible.cfg


vi /home/ansible/test-ansible/ansible.cfg

Lines to be appended in ansible.cfg


[privilege_escalation]
become = True
become_method = sudo
become_ask_pass = False

Run the below command again


ansible@control-node:~/test-ansible$ ansible all -i hosts  -m apt  -a "name=apache2 state=present"

This time also you will get below error if you are following all the tutorials from pre-requisite


managed-node-1 | FAILED! => {
    "msg": "Missing sudo password"
}

For fixing this issue we need to add ansible user in sudoers file on Managed Node

Login to managed-node-1 and write below entry in sudoers file, notice this activity is being done on managed node


ubuntu@managed-node-1:~$ sudo visudo;

Write below line in sudoers file


ansible  ALL=(ALL) NOPASSWD:ALL

Run the below command again and this time it should work


ansible@control-node:~/test-ansible$ ansible all -i hosts  -m apt  -a "name=apache2 state=present"

You should see output for managed nodes similar to the below one


managed-node-1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "cache_update_time": 1589737852,
    "cache_updated": false,
    "changed": true,
    "stderr": "",
    "stderr_lines": [],

Start Apache with Ansible ad-hoc command


ansible@control-node:~/test-ansible$ ansible all -i hosts  -m service -a "name=apache2 state=started"

You should see output for managed nodes similar to the below one


managed-node-1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "name": "apache2",
    "state": "started",

Stop Apache with Ansible ad-hoc command


ansible@control-node:~/test-ansible$ ansible all -i hosts  -m service -a "name=apache2 state=stopped"

You should see output for managed nodes similar to the below one


managed-node-1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": true,
    "name": "apache2",
    "state": "stopped",

If you are getting any error while following this tutorial, please post errors in comments, we will try to resolve those as soon as possible.

Setting up Control and Managed nodes < Prev

Category: Linux