Skip to main content

Ansible

Ansible is an open source, command-line IT automation software application written in Python. It can configure systems, deploy software, and orchestrate advanced workflows to support application deployment, system updates, and more.

I use ansible to help with any repetitive tasks, installs, and backups!

I've only begun to scratch the surface with Ansible and am certainly not an expert.

Considering this, I'm only going to show how I've used Ansible and give some examples. I'll leave the tutorials to those that understand it better and you can find on YouTube / Google.

My Use Cases

Below are the main use cases I have currently for Ansible.

  1. Updates
  2. Installations

Updates

For keeping my Raspberry Pi linux distribution up to date, I utilize a simple playbook to run updates and upgrades.

apt.yml
- hosts: "*"
become: yes
tasks:
- name: apt
apt:
update_cache: yes
upgrade: "yes"

Installations

I have setup Ansible playbooks for several of my services that I want installed on my Raspberry Pi or any home lab. Almost all of the installation instructions in the tutorial-basics have been setup as Ansible playbooks.

Docker Installation
docker.yml
- hosts: "*"
become: yes
tasks:
- name: install packages to allow apt to use a repo over HTTPS
apt:
pkg:
- ca-certificates
- curl
- gnupg
state: present
update_cache: true
become: true

- name: Set up GPG key
ansible.builtin.shell: |
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
chmod a+r /etc/apt/keyrings/docker.gpg
echo \
"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
tee /etc/apt/sources.list.d/docker.list > /dev/null
become: true

- name: Update apt
apt:
update_cache: yes
upgrade: "yes"

- name: install Docker Engine
apt:
pkg:
- docker-ce
- docker-ce-cli
- containerd.io
- docker-buildx-plugin
- docker-compose-plugin
state: present
update_cache: true
become: true

- name: Add pi user to Docker user group
ansible.builtin.shell: usermod -aG docker pi
become: true
Oh My Zsh
ohmyzsh.yml
- hosts: "*"
vars:
the_user: "{{ ansible_user_id }}"
tasks:
- name: install vim and zsh packages
apt:
pkg:
- zsh
- vim
state: present
update_cache: true
become: true

- name: Install Oh My ZSH and set ZSH as default
ansible.builtin.shell: yes 'y' | sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
args:
creates: ~/.oh-my-zsh

- name: Register shell var
ansible.builtin.shell: echo $SHELL
register: shell

- name: Register zsh var
ansible.builtin.shell: which zsh
register: which_zsh

- name: Set ZSH as default
ansible.builtin.shell: chsh -s $(which zsh) {{ the_user }}
become: true
when: shell.stdout != which_zsh.stdout

- name: Set personal custom repo for Oh My Zsh
ansible.builtin.shell: |
mv custom custom-old

git clone https://github.com/austinbspencer/custom-aliases.git custom

cd custom

git checkout $USER
args:
chdir: ~/.oh-my-zsh/
creates: custom-old

Conclusion