Home Automate EVERYTHING with Ansible!
Post
Cancel

Automate EVERYTHING with Ansible!

Ansible. Need I say more? Well, maybe, if you’ve never heard of it. Ansible is a simple IT / DevOps automation that anyone can use. You can Automate anything with an SSH connection and WITHOUT installing any agents or clients. Join me as we set up, configure and start automating with Ansible!

📺 Watch Video

install

1
2
3
sudo apt update
sudo apt install ansible
sudo apt install sshpass

Note: Most distributions include an “older” version of Ansible. If you want to install the latest version of Ansible, see installing the latest version of ansible

hosts

1
2
3
4
5
[ubuntu]
server-01
server-02
192.168.0.100
192.168.0.1002

commands

check ansible version

1
ansible --version

command with module

1
ansible -i ./inventory/hosts ubuntu -m ping --user someuser --ask-pass

command with playbook

1
ansible-playbook ./playbooks/apt.yml --user someuser --ask-pass --ask-become-pass -i ./inventory/hosts

playbooks

apt.yml

1
2
3
4
5
6
7
- hosts: "*"
  become: yes
  tasks:
    - name: apt
      apt:
        update_cache: yes
        upgrade: 'yes'

qemu-guest-agent.yml

1
2
3
4
5
6
7
8
9
- name: install latest qemu-guest-agent
  hosts: "*"
  tasks:
    - name: install qemu-guest-agent
      apt:
        name: qemu-guest-agent
        state: present
        update_cache: true
      become: true

zsh.yml

1
2
3
4
5
6
7
8
9
- name: install latest zsh on all hosts
  hosts: "*"
  tasks:
    - name: install zsh
      apt:
        name: zsh
        state: present
        update_cache: true
      become: true

timezone.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- name: Set timezone and configure timesyncd
  hosts: "*"
  become: yes
  tasks:
  - name: set timezone
    shell: timedatectl set-timezone America/Chicago

  - name: Make sure timesyncd is stopped
    systemd:
      name: systemd-timesyncd.service
      state: stopped

  - name: Copy over the timesyncd config
    template: src=../templates/timesyncd.conf dest=/etc/systemd/timesyncd.conf

  - name: Make sure timesyncd is started
    systemd:
      name: systemd-timesyncd.service
      state: started

timesyncd.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See timesyncd.conf(5) for details.

[Time]
NTP=192.168.0.4
FallbackNTP=time.cloudflare.com
#RootDistanceMaxSec=5
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048

Installing the latest version of Ansible

Most distributions have an older version of Ansible installed. This is usually fine except sometimes you may need to use features from the latest Ansible. Use the following commands to update Ansible to the latest version.

Check version

1
ansible --version

If it’s not the version you are looking for, check to see where it is installed

1
which ansible

If it lives somewhere like

1
/usr/bin/ansible

this is most likely due to your distribution installing it there.

Remove previous version

1
sudo apt remove ansible

Check to be sure it is removed

1
which ansible

You should see

1
ansible not found

check to see that you have python3 and pip

1
python3 -m pip -V

You should see something like

1
pip 22.3.1 from /home/user/.local/lib/python3.8/site-packages/pip (python 3.8)

Install pip if the previous couldn’t find the pip module

1
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

Install ansible

1
python3 -m pip install --user ansible

Confirm your version with

1
ansible --version

⚙️ See all the hardware I recommend at https://l.technotim.live/gear

🚀 Don’t forget to check out the 🚀Launchpad repo with all of the quick start source files

This post is licensed under CC BY 4.0 by the author.

HIGH AVAILABILITY k3s (Kubernetes) in minutes!

Cloud Native Distributed Storage in Kubernetes with Longhorn