Operations 12 min read

Master Ansible: From Installation to Advanced Playbooks on Ubuntu

This guide introduces Ansible, explains its core features and architecture, walks through installing it on Ubuntu 16.04, configuring SSH keys, setting password‑less sudo, managing inventories, using modules, writing playbooks, and handling common automation pitfalls.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Ansible: From Installation to Advanced Playbooks on Ubuntu

Ansible Overview

Ansible is an open‑source automation and configuration‑management tool built on OpenSSH. It can configure systems, deploy software, and orchestrate advanced IT tasks such as continuous deployment or zero‑downtime updates. Its goals are simplicity, ease of use, security, and reliability, making it suitable for developers, sysadmins, release engineers, and IT managers across environments of any size.

Because Ansible does not require agents on managed machines, there is no need to upgrade remote daemons or worry about losing management capability after uninstalling them.

Main Features

Administrators can execute commands (tasks) on hundreds or thousands of computers simultaneously. Common tasks include avoiding missed or unintended operations when manually maintaining complex servers and eliminating the time‑consuming manual initialization of new servers.

While shell scripts can become overly complex and hard to maintain, alternatives like Puppet and Chef require learning new toolchains. Ansible retains the familiar shell‑script workflow, reducing the learning curve.

Execute shell commands line by line.

No additional client tools needed (Linux already includes SSH).

Configuration is idempotent—running the same configuration multiple times causes no issues.

How Ansible Works

Ansible communicates over standard SSH, retrieving information, issuing commands, and copying files without installing agents on target machines. Any server with an open SSH port can be managed.

It uses a modular design; modules can be written in any language and communicate via JSON. Configuration files are written in YAML for simplicity, and playbooks allow interaction with clients.

Installation on Ubuntu 16.04

$ sudo apt-add-repository -y ppa:ansible/ansible
$ sudo apt-get update
$ sudo apt-get install -y ansible

Verify the installation:

$ ansible --version

Configuring SSH Keys for Clients

Copy the current user's public key to the managed hosts (e.g., 192.168.21.145 and 192.168.21.148):

$ ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
$ ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

Test password‑less SSH access:

$ ssh [email protected]
$ ssh [email protected]

Configuring Password‑less sudo

Allow user nick to run sudo commands without a password:

$ sudo visudo
nick    ALL=(ALL)    NOPASSWD: ALL

Now the following command runs without prompting for a password:

$ ansible testservers -b -u nick -a "apt update"

Inventory Management

The inventory file (default /etc/ansible/hosts) lists managed hosts. You can specify a custom inventory with the -i option:

$ ansible myservers -i /etc/ansible/myhosts -b -u nick -a "apt update"

Hosts can be defined individually by hostname or IP, or grouped together for batch operations.

Modules

Operations are encapsulated in modules, which accept key=value arguments. Common usage examples:

$ ansible testservers -m command -u nick -a "df -h"
$ ansible testservers -m service -a "name=httpd state=started"
$ ansible testservers -m copy -u nick -a "src=./app.js dest=./myapp/app.js"

Playbooks

Playbooks allow you to write a series of tasks in YAML and execute them with ansible-playbook. Example playbook to check disk usage on testservers:

---
- hosts: testservers
  become: true
  become_user: root
  tasks:
    - name: check disk
      command: df -h

Run the playbook:

$ ansible-playbook -u nick playbook.yml

Skipping Host Key Checking

To avoid interactive SSH host‑key verification on first connection, edit /etc/ansible/ansible.cfg and set host_key_checking = False (remove the comment character).

Conclusion

Ansible is a powerful automation tool that combines simplicity with flexibility, making it a valuable asset for developers and system administrators practicing DevOps. Mastering Ansible can significantly enhance your automation workflows.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Configuration ManagementDevOpsUbuntuPlaybooks
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.