|

How to Install Ansible in Kali Linux

how to install ansible in kali linux

To install Ansible on Kali Linux, you can follow these steps. Kali Linux, being based on Debian, uses the apt package manager for installing software. Here’s how to install Ansible:

1. Update the System

Before installing Ansible, it’s always a good idea to update your system.

ShellScript
sudo apt update
sudo apt upgrade

2. Install the Required Dependencies

Ensure you have the necessary packages installed, particularly Python and other dependencies.

ShellScript
sudo apt install software-properties-common

3. Add the Ansible PPA

Ansible can be installed directly from the Kali repositories or you can use the official Ansible PPA. Adding the official PPA ensures that you get the latest stable version.

A PPA (Personal Package Archive) is a software repository created and maintained by individual developers or teams, which allows users to easily install and update software on Ubuntu-based systems (including Kali Linux, which is based on Debian and can use Ubuntu PPAs).

ShellScript
sudo apt-add-repository --yes --update ppa:ansible/ansible

4. Install Ansible

After adding the repository, you can install Ansible using apt.

ShellScript
sudo apt install ansible

5. Verify the Installation

Once installed, you can check if Ansible is properly installed by checking its version.

ShellScript
ansible --version


If everything is set up correctly, you should see the version of Ansible installed on your system. You are now ready to use Ansible for automation tasks on your Kali Linux system! Lets have a look at a concrete example of how we can actually use ansible.

Example: Install and configure Apache web server on multiple servers

Step 1: Create an Inventory File

Ansible needs to know which servers it will manage. You do this by creating an inventory file that lists the IP addresses or domain names of the target servers.

Create a file called hosts in the same directory where you’ll place your playbook.

ShellScript
sudo nano hosts


Add the IP addresses or hostnames of the web servers under a group called [webservers]. For example:

[webservers]
192.168.1.10
192.168.1.11


Replace 192.168.1.10 and 192.168.1.11 with the actual IP addresses or hostnames of the servers you want to manage.

Step 2: Create the Ansible Playbook

An Ansible playbook is written in YAML format. You can create a new playbook file called apache-setup.yml and add the configuration code to it.

Create the playbook file:

ShellScript
nano apache-setup.yml


Paste the playbook content into it:

---
- hosts: webservers
  become: yes
  tasks:
    - name: Install Apache
      apt:
        name: apache2
        state: present

    - name: Ensure Apache is running
      service:
        name: apache2
        state: started
  • What this does:
    • hosts: webservers: The playbook will run on all hosts listed under the [webservers] group in the inventory file.
    • become: yes: Tells Ansible to run the tasks with elevated privileges (e.g., using sudo).
    • tasks: These are the steps Ansible will perform:
      • Install Apache.
      • Ensure that the Apache service is started and running.

Step 3: Run the Playbook

Now that you have the inventory file and the playbook ready, you can run the playbook using ansible-playbook.

Run the following command to execute the playbook:

ShellScript
ansible-playbook -i hosts apache-setup.yml


If it’s the first time you’re connecting to the servers, you may be prompted to accept the SSH key fingerprints. Type “yes” to continue.

Step 4: Verify the Apache Installation

Once the playbook finishes running, Ansible will output the results of each task. If everything worked correctly, you can verify that Apache is installed and running on your target servers by either:

SSH into a server: Log in to one of your servers and check if Apache is running.

ShellScript
ssh user@192.168.1.10
sudo systemctl status apache2

Test from a browser: Open your web browser and navigate to the IP address of one of the web servers (e.g., http://192.168.1.10). You should see the default Apache welcome page.

Summary of Steps:

  1. Create an inventory file (hosts) to define your target servers.
  2. Create an Ansible playbook (apache-setup.yml) with tasks to install and configure Apache.
  3. Run the playbook using the ansible-playbook command.
  4. Verify that Apache is installed and running on the target servers.

That’s it! You’ve now automated the installation and configuration of Apache on multiple servers using Ansible.

Similar Posts