Ansible is a free and open-source automation tool. It is used for configuration management and application deployment. Ansible is available for almost all UNIX like operating systems.
System on which Ansible is installed is known as ‘Control Node’ and systems which are being managed by ansible are known as ‘managed host’. We don’t need to install any agent on managed hosts as Ansible works on ssh protocol (default port 22).
In this guide, we will learn how to install and use ansible on Debian 11 system. For the demonstration purpose, I am using following Lab.
- Ansible Control Node – 192.168.1.151 (control.example.net) – Debian 11
- Managed Host 1 – 192.168.1.170 (host1.example.net) – Rocky Linux 8
- Managed Host 2 – 192.168.1.180 (host2.example.net) – Rocky Linux 8
Minimum System Requirement for Ansible
- Minimal Debian 11 System
- Sudo User with root privileges
Let’s deep dive into Ansible Installation steps on Debian 11
1) Install Ansible with apt command
Ansible Debian package and its dependencies are available in the default Debian 11 package repositories. So, to install ansible, run following commands
$ sudo apt update $ sudo apt install -y ansible
Once ansible is installed, verify its version by running,
$ ansible --version
Above output confirms that ansible version 2.10.8 is installed.
2) Generate SSH keys and share it between managed hosts
To generate ssh keys, run ‘ssh-keygen’ command
Now exchange ssh keys using ssh-copy-id command,
$ ssh-copy-id 192.168.1.170 $ ssh-copy-id 192.168.1.180
On each managed host, configure following for sysops user so that it can run sudo commands without prompting password.
$ echo "sysops ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/sysops
3) Create ansible config file and inventory file
Let’s create project folder with name ‘ansible-demo’,
$ mkdir ansible-demo $ cd ansible-demo/
Create ansible.cfg file with following content
$ vi ansible.cfg [defaults] inventory = ./inventory host_key_checking = false remote_user = sysops ask_pass = False [privilege_escalation] become=true become_method=sudo become_user=root become_ask_pass=False
Save and exit the file.
Create the inventory file,
$ vi inventory [prod] 192.168.1.170 [dev] 192.168.1.180
Save & close the file
Now in ‘ansible-demo’ folder we have two files, ansible.cfg and inventory
$ pwd /home/sysops/ansible-demo $ ls -l total 8 -rw-r--r-- 1 sysops sysops 194 Apr 8 17:25 ansible.cfg -rw-r--r-- 1 sysops sysops 42 Apr 8 17:28 inventory $
4) Test and Verify Ansible Installation
Let’s first verify the managed host connectivity from ansible control node, run
$ ansible all -m ping
Perfect, above output confirms ansible is able to perform ping pong test to its managed hosts.
Let’s create a sample playbook to install Apache server on dev node
$ vi demo-apache.yml --- - name: Install Apache Web Server hosts: dev tasks: - name: install httpd package yum: name: httpd state: installed - name: Start httpd service service: name: httpd state: started
save & exit the file
To run the playbook, execute following command
$ ansible-playbook demo-apache.yml
Verify whether Apache is installed and started on Dev host, run following ansibe ad-hoc commands
$ ansible dev -m shell -a 'rpm -qa | grep -i httpd' $ ansible dev -m shell -a 'systemctl status httpd'
Great, above confirms that playbook has been executed successfully on dev host. That’s all from this guide, kindly post your queries and feedback in below comments sections.
Read Also: How to Install Microsoft Teams on Ubuntu/Debian Linux