Ansible Automation

Infrastructure-as-code with agentless configuration management.

4 min read

Why Ansible?

Ansible is a configuration management and automation tool that requires no agents on target machines. It connects over SSH (Linux/Unix) or WinRM (Windows), executes modules, and reports results. This agentless architecture means you can automate a new server by adding it to an inventory file -- no software installation, no daemon running, no certificate management.

For an IT professional managing a mixed Windows/Linux environment with Active Directory, Hyper-V, and cloud instances, Ansible provides a single automation layer across everything. Write a playbook once, run it against any number of targets.

Installation and Setup

Ansible runs from a control node -- any machine with Python 3.8+. Install it with pip or your package manager:

pip install ansible

After installation, create an inventory file (hosts) listing your managed nodes:

[webservers]
web1.example.com
web2.example.com

[dbservers]
db1.example.com

[all:vars]
ansible_user=admin

Test connectivity with ansible all -m ping. If your targets use SSH keys, configure ~/.ssh/config for user and key paths. For Windows targets, configure WinRM with SSL on port 5986 and use the ansible.windows collection.

Playbook Fundamentals

Playbooks are YAML files that define the desired state of your systems. Each playbook contains one or more plays, and each play targets a group of hosts with a set of tasks.

A basic playbook structure:

---
- name: Configure web servers
hosts: webservers
become: true
tasks:
- name: Install nginx
apt:
name: nginx
state: present

- name: Start and enable nginx
service:
name: nginx
state: started
enabled: true

The become: true directive elevates to root for tasks that need it. Each task is idempotent -- running it multiple times produces the same result without side effects. If nginx is already installed and running, Ansible reports "ok" rather than reinstalling.

Key Ansible Concepts

Modules are the building blocks of Ansible. Each module performs a specific action: installing packages, managing services, creating files, configuring network devices, etc. Over 2,700 modules are available covering Linux, Windows, cloud platforms, and networking equipment. Use ansible-doc module_name to read the documentation for any module.

Variables allow you to parameterize playbooks. Define them in inventory files, group_vars, host_vars, or pass them on the command line with -e "var=value". Variable precedence determines which value wins when the same variable is defined in multiple places.

Handlers are tasks that run only when notified by another task. The most common use case is restarting a service after its configuration file changes:

tasks:
- name: Update nginx config
template:
src: nginx.conf.j2
dest: /etc/nginx/nginx.conf
notify: restart nginx

handlers:
- name: restart nginx
service:
name: nginx
state: restarted

Roles organize playbooks into reusable components. A role has a standard directory structure: tasks/, templates/, files/, handlers/, vars/, and defaults/. Ansible Galaxy hosts thousands of community roles that you can use directly or customize.

Windows Automation with Ansible

Ansible manages Windows targets just as effectively as Linux. The requirements are straightforward:

Configure WinRM with a simple PowerShell script:

Enable-PSRemoting -Force
Set-Item wsman:\localhost\client\trustedhosts *
New-SelfSignedCertificate -DnsName "$(hostname)" -CertStoreLocation cert:\LocalMachine\My
# Configure WinRM to use the certificate

Once configured, use Windows-specific modules like win_feature, win_service, win_reg_stat, win_domain, and win_domain_computer to manage Active Directory, roles, features, and registry settings.

Practical Use Cases

Here are real scenarios where Ansible saves significant time and eliminates manual errors:

Best Practices

  1. Version control all playbooks, roles, and inventories in Git
  2. Use --check mode to preview changes before applying them
  3. Use --diff to see exactly what will change in configuration files
  4. Keep playbooks idempotent -- they should produce the same result when run repeatedly
  5. Use roles for anything you plan to reuse across multiple projects
  6. Store secrets in Ansible Vault, never in plain text
  7. Test playbooks against a staging environment before running in production
  8. Document your inventory structure and variable definitions for your team