Ansible Setup Guide¶
A guide for installing Ansible and getting started with infrastructure automation.
Installation¶
Ubuntu¶
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install ansible
RHEL / AlmaLinux / CentOS¶
Verify Installation¶
Inventory Configuration¶
The inventory file defines the hosts Ansible manages.
Default Location¶
Example Inventory¶
[webservers]
server1 ansible_host=10.0.0.11
server2 ansible_host=10.0.0.12
server3 ansible_host=10.0.0.13
[dbservers]
db1 ansible_host=10.0.0.21
[all:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_user=your_username
ansible_ssh_private_key_file=~/.ssh/my-server-key
Verify Inventory¶
Ad-Hoc Commands¶
Quick commands without writing a playbook.
Ping All Hosts¶
Run a Shell Command¶
Install a Package¶
# On Debian/Ubuntu hosts
ansible webservers -m apt -a "name=vim state=latest" --become
# On RHEL/CentOS hosts
ansible dbservers -m dnf -a "name=vim state=latest" --become
Target Specific Hosts or Groups¶
Playbook Basics¶
Playbooks are YAML files defining automation tasks.
Example: Install Docker¶
# install-docker.yml
---
- name: Install Docker on Ubuntu servers
hosts: webservers
become: true
tasks:
- name: Install prerequisites
apt:
name:
- apt-transport-https
- ca-certificates
- curl
- software-properties-common
state: present
update_cache: true
- name: Add Docker GPG key
apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present
- name: Add Docker repository
apt_repository:
repo: "deb https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
state: present
- name: Install Docker
apt:
name: docker-ce
state: present
update_cache: true
- name: Start and enable Docker
systemd:
name: docker
state: started
enabled: true
- name: Add user to docker group
user:
name: "{{ ansible_user }}"
groups: docker
append: true
Run a Playbook¶
Dry Run (Check Mode)¶
Verbose Output¶
Ansible + Docker Integration¶
Run Containers via Ansible¶
- name: Run Nginx container
community.docker.docker_container:
name: nginx
image: nginx:latest
state: started
ports:
- "80:80"
restart_policy: unless-stopped
Note: Requires the
community.dockercollection: