Hey there! Managing IT infrastructure efficiently is crucial as your systems grow larger and more complex. But nobody wants to spend time repeatedly configuring servers by hand. That‘s where Ansible comes in – it‘s a super handy tool that can automate away all those tedious sysadmin tasks.
In this comprehensive guide, I‘ll walk you step-by-step through installing Ansible on an Ubuntu server from scratch. I‘ll also share some pro tips and best practices I‘ve learned from using Ansible professionally.
By the end, you‘ll be able to:
- Install and configure Ansible on Ubuntu
- Understand key Ansible concepts like inventories, modules, and playbooks
- Automate common administrative tasks with ad-hoc commands
- Launch complex deployments using Ansible playbooks
- Follow Ansible best practices for production environments
Let‘s get started!
Why Ansible Should Be Your Go-To Automation Tool
First off, why Ansible versus the many other options out there? Here‘s a quick rundown of what makes Ansible so popular:
Agentless – There‘s no need to install any agents or daemons on your servers. Ansible handles all communication via SSH. This makes setup a breeze.
Idempotent – Ansible playbooks and modules are designed to be idempotent. That means if a playbook runs twice, your systems will converge to the desired end state every time.
Intuitive – Ansible uses easy to understand YAML playbooks. In contrast, other tools use convoluted domain-specific languages.
Secure – Connections use SSH with host keys checked by default. No agents store credentials that could be compromised.
Powerful – An expansive library of modules can automate everything from cloud infrastructure to network gear.
According to Red Hat‘s 2021 survey, Ansible surpassed competitors like Puppet and Chef in popularity long ago. 45% of respondents use Ansible, while only 20% use Puppet and 12% use Chef.
And Ansible adoption is still growing rapidly. From 2018 to 2021, Ansible usage increased by 65%. DevOps teams love how easy it is to get started automating with Ansible!
Now that you know Ansible is the right tool for infrastructure automation tasks, let‘s go over how to install it on Ubuntu.
Step-by-Step Guide to Installing Ansible on Ubuntu
The first step is installing Ansible on the control node, which will be used to run playbooks and administer your servers. You‘ll want to use a fresh Ubuntu 20.04 server for this.
Make sure you have sudo privileges on the control node. Then log in and follow these steps:
1. Update Package Index
sudo apt update
Always a good idea before installing new packages!
2. Install Ansible
sudo apt install ansible
The latest Ansible version will now be installed from Ubuntu‘s default repositories. Simple!
3. Check the Installed Version
ansible --version
This confirms Ansible is installed and shows the version:
ansible 2.9.6
config file = /etc/ansible/ansible.cfg
configured module search path = [u‘/home/x/.ansible/plugins/modules‘, u‘/usr/share/ansible/plugins/modules‘]
ansible python module location = /usr/lib/python3/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 3.8.10 (default, Nov 14 2022, 12:59:47) [GCC 9.4.0]
Take note of the config file location, module path, and Python version – these can be useful later.
And that‘s it for installation! With just two quick commands you installed the latest Ansible release. Now let‘s move on to configuration.
Inventory File – List Your Managed Servers
The inventory file tells Ansible what servers you want to manage.
By default, this is located at /etc/ansible/hosts. Open it with sudo privileges in your text editor:
sudo vim /etc/ansible/hosts
Then add your servers, with aliases, like so:
[webservers]
10.10.10.5 ansible_host=web1
10.10.10.6 ansible_host=web2
[dbservers]
10.10.10.7 ansible_host=db1
The [webservers] and [dbservers] are group names you can use later in playbooks.
ansible_host sets the hostname Ansible uses. This is helpful if your server‘s inventory alias differs from its configured hostname.
You can also store variables on a per-group basis:
[webservers]
10.10.10.5
10.10.10.6
[dbservers]
10.10.10.7
[webservers:vars]
nginx_enabled=true
[dbservers:vars]
mysql_port=3306
These variables will be accessible later in playbooks.
Once your inventory is populated, you‘re ready to move on!
Configure SSH Access with Ansible User
By default Ansible connects to servers via SSH keys. You need to set up an Ansible user account on all your nodes.
First, on each managed node, create a ansible user:
sudo adduser ansible
Give this user sudo privileges without needing a password by editing /etc/sudoers:
ansible ALL=(ALL) NOPASSWD: ALL
This allows ansible to sudo to root to run playbook tasks.
Now on the control node, generate SSH keys for the ansible user:
ssh-keygen
Just press Enter to accept defaults. Do not set a passphrase.
This creates id_rsa and id_rsa.pub keys in /home/ansible/.ssh/.
Copy the public key to your nodes to enable passwordless SSH:
ssh-copy-id ansible@NODE_IP
Enter the ansible user‘s password when prompted. Repeat for every node.
Finally, test logging in to confirm it works:
ssh ansible@NODE_IP
If you connect without a password, SSH keys are configured correctly!
SSH key authentication is now set up so Ansible can run playbooks and commands on your nodes.
Run Ad-Hoc Commands on Managed Nodes
Let‘s see Ansible in action by running some ad-hoc commands.
Ad-hoc commands let you manage your servers without writing full playbooks. They‘re great for quick admin tasks across your fleet.
For example, get the uptime of your webservers:
ansible webservers -a "uptime"
This will run the uptime command and return the result for each node:
web1 | SUCCESS | rc=0 >>
13:10:26 up 1 day, 23:12, 4 users, load average: 0.00, 0.01, 0.05
web2 | SUCCESS | rc=0 >>
13:10:26 up 12 days, 20:54, 4 users, load average: 0.00, 0.01, 0.05
You can also manage packages, run commands with sudo, check service status, and much more.
Some other handy examples:
# Install nginx on webservers
ansible webservers -a "apt install nginx"
# Restart mysql on dbservers
ansible dbservers -b -a "systemctl restart mysql"
# Check memory usage
ansible all -a "free -m"
The -b flag executes the command with sudo. The all group refers to all servers in inventory.
Ad-hoc commands are great when you need to do some quick server administration. But what about something more complex like deploying an app? For that, it‘s time to learn playbooks.
Automate Complex Tasks with Ansible Playbooks
Ad-hoc commands have their uses, but playbooks are where Ansible really shines. Playbooks are YAML files that define automation procedures:
- Target servers
- Variables
- Tasks to execute
- Handlers that trigger based on task results
Playbooks let you script automated workflows ranging from simple config changes to elaborate deployments spanning multiple machines.
Here‘s a playbook that installs and configures Nginx:
---
- name: Install and configure Nginx
hosts: webservers
tasks:
- name: Install latest Nginx
apt:
name: nginx
state: latest
- name: Copy Nginx config file
copy:
src: /local/nginx.conf
dest: /etc/nginx/sites-enabled/default
- name: Start Nginx
service:
name: nginx
state: started
This playbook will:
- Install the latest Nginx version on the webservers
- Copy a custom
nginx.conffile from the control node - Start the Nginx service
To run it:
ansible-playbook nginx.yml
Playbooks open up an endless array of automation possibilities – deploying apps, changing configs, managing users, provisioning cloud resources, and way more.
I highly recommend browsing Ansible‘s playbook guides to learn in-depth playbook syntax and concepts.
Now that you know the Ansible basics, let‘s go over some best practices.
Ansible Best Practices for Smooth Operations
Here are some pro tips I‘ve picked up from using Ansible for server automation in large production environments:
Idempotency is Key – Structure playbooks using modules like apt, templates, copy, and service that safely manage state. Idempotency ensures consistency.
Use Templates Over Copy – Templates allow injecting variables. Dynamic configs are powerful.
Vault Encrypts Secrets – Store sensitive data like passwords and keys in encrypted Ansible Vault files. Decrypt only at runtime.
Tag Playbook Sections – Utilize tags to selectively run playbook portions. Useful for testing a single task.
Separate Environment Playbooks – Dedicate playbooks for prod, staging, development configurations. Keep them lightweight.
Leverage Ansible Galaxy – Galaxy provides community developed roles to simplify automating common tasks.
Test on Staging First – Set up a staging environment mirroring production to test playbooks safely.
Monitor Playbook Runs – Use tools like Rundeck or AWX to schedule playbooks and track failures.
Following these guidelines will help you avoid some pitfalls and keep your Ansible environment running smoothly even as your infrastructure grows.
Get Automating!
In this guide, you learned how to get Ansible installed on Ubuntu, connect it to your servers, run ad-hoc commands, and use playbooks to automate complex tasks.
With these Ansible basics, you‘re ready start managing your infrastructure efficiently and deploying apps consistently. Don‘t spend another day manually logging into servers – let Ansible take care of it for you!
The official Ansible docs have a ton more detailed information on all aspects of Ansible. As you expand your skills, look into advanced concepts like:
- Ansible roles for reusable playbook components
- Jinja2 templates for customized configuration files
- Integrating with CI/CD pipelines and cloud services
- Ansible Tower for an enterprise Ansible environment
The possibilities are endless when you harness the power of Ansible for automation. Implementing it may seriously be one of the best decisions you make as you scale up your IT systems and operations.
I hope this guide gives you a nice overview of everything involved in installing Ansible for the first time and setting up simple automation. Feel free to reach out if you have any other questions! I‘m always happy to chat more Ansible and DevOps.