Setting up Ansible

Why should you do this?

If you have more than 2 machines that you regularly use and login to either to update, it can become a bit tedious. Which is where Ansible comes in to help you save some time.
The idea with Ansible is to automate certain jobs, like running “apt update && apt upgrade -y” every week or however often you want.
Honestly when starting this project, it seemed daunting, and like a really big mouthful. Now having run this for a week or so, I regret not starting it earlier. Because its really not that bad.

The biggest hurdle is those pesky yaml files, and their indentations.

I recommend using a separate machine for this, as this makes things easier.

Note: you only need to install Ansible on 1 machine, because it “logs in” with credentials and running commands on the remote machine.

Required Knowledge
Advanced Command Line

Required Software
Ansible

How to guide

Starting Ansible

In your machine open a terminal
Run command
sudo apt install -y ansible 

This installs Ansible on your machine.

Setting up the hosts file

Run command
sudo nano /etc/ansible/hosts

In the hosts file we are going to add our remote machines.

Inside /etc/ansible/hosts

# You can input as many groups and computer names as you want.
[servernames]
computer-name ansible_host=192.168.1.10

[servernames2]
computer-name ansible_host=192.168.2.10

[all:vars]
ansible_python_interpreter=/usr/bin/python3

Once done inputting the information.
Press CTRL + S to save
Press CTRL +X to exit

[servernames] refers to a group of machines. You don’t need more than 1 group, unless you’re doing different things with them.

computer-name | you can input whatever name you have for your machine.

ansible_host=<ip> | gives Ansible an IP that it can connect to.

The [all:vars] is just to say we want to use “Python3 to do everything.

Let’s create a simple Ansible playbook
See previous post about setting up a GitHub Repository. Syncing GitHub to Ubuntu Server and Vice Versa

cd into your GitHub folder
Run command
sudo nano my_first_playbook.yml

This creates a file in your folder that you can later sync to GitHub.

Inside the my_first_playbook.yml

---
- hosts: all
  become: true

  tasks:
    - name: run apt update
      apt:
        update_cache: yes
        cache_valid_time: 3600

    - name: run apt upgrade
      apt:
        upgrade: dist
        force_apt_get: yes
        autoremove: yes

Press CTRL + S to save
Press CTRL + X to exit

Using this code will update all your machines that you input into the ‘hosts’ file earlier.
become: true | refers to becoming ‘sudo’ or root

tasks | refers to the list underneath, starting with -name

– name: | is a title for the job that its going to run

There is multiple options for various apps to run. Here I chose apt
I tell it to update its cache.
Basically “sudo apt update”
Time part is to say it can only be this old otherwise update it.

In the next we are doing the same, but with upgrade instead.
Basically “sudo apt upgrade -y”
And to remove old unused packages.

Running our first playbook

In the command line
Run command
ansible-playbook <path to your playbook> -u <the user you wish to login with on the remote system> -kK

This tells Ansible to run, and which playbook to use, it will default to ALL servers in your hosts file.
The -kK tells Ansible it needs to ask for your users password and sudo password.

The Result

Ansible will gather information and complain if you typed something wrong in the .yml file
Otherwise it will show an output of the titles you specified.

You have now run your first Ansible playbook 🙂

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *