Ansible tricks

ansible Jul 31, 2021

I want to show you some tricks and solutions that I designed along my journey automating stuff with Ansible. To use these tricks doesn't matter if you are new in the Ansible world or already are a power user.

How to learn Ansible?

If you want to learn a lot of stuff from a really competent person, Jeff Geerling has an awesome playlist on his YouTube channel. The geerlingguy (his GitHub/Twitter name account) was part of the Ansible team and still maintaining many Ansible roles and collections. This is the best source that I can recommend for any beginner and even more advanced users today (2021).

Ansible 101
Ansible 101 introduces Ansible for Linux server administration, based on the best-selling book, Ansible for DevOps.

Free Ansible Workshops available on GitHub:

GitHub - ansible/workshops: Training Course for Ansible Automation Platform
Training Course for Ansible Automation Platform. Contribute to ansible/workshops development by creating an account on GitHub.

Free Red Hat introduction to Ansible and Automation Controller (AWX):

Ansible Basics: Automation Technical Overview | DO007
Learn the basics of Ansible automation and configuration management with Ansible Basics: Automation Technical Overview (DO007).

Basics

# Ansible requires python3 on the machine in order to work,
# but does exist one trick to install python3 with ansible:
- name: Bootstrap a host without python3 installed
  raw: dnf install -y python3
ansible: install python3
# Assuming a inventory file at ./inventory
ansible-inventory -i ./inventory --list
Convert an inventory file to a JSON format

Common structures

# Simple loops
- name: Ensure some OS packages are installed.
  apt: name="{{ item }}" state=latest
  loop:
    - git
    - golang
    - python3-pip
    - jq
  become: true
ansible: install multiple packages using apt

Install binary from remote

A common problem that I face is installing the latest version of an application from a GitHub release, so here are two examples of how to do that.

Install binary from GitHub release:

# sops
- name: Get latest 'sops' release metadata
  uri:
    url: https://api.github.com/repos/mozilla/sops/releases
    return_content: yes
  register: _sops_github_releases
  failed_when: "'.linux' not in _sops_github_releases.content"

- name: Ensure latest 'sops' release is installed
  get_url:
    url: "{{ item.browser_download_url }}"
    dest: /usr/local/bin/sops
    mode: 0755
  loop: "{{ _sops_github_releases.json[0].assets}}"
  when: "'.linux' in item.name"
  become: true
ansible: install from GitHub release and extract all files

Extract specific files from remote:

# age
- name: Get latest 'age' release metadata
  uri:
    url: https://api.github.com/repos/FiloSottile/age/releases
    return_content: yes
  register: _age_github_releases
  failed_when: "'linux-amd64.tar.gz' not in _age_github_releases.content"

- name: Ensure latest 'age' release is installed
  ansible.builtin.unarchive:
    src: "{{ item.browser_download_url }}"
    dest: /usr/local/bin
    extra_opts:
      - --strip=1
      - --wildcards
      - '*/age*'
    mode: 0755
    remote_src: yes
  loop: "{{ _age_github_releases.json[0].assets}}"
  when: "'linux-amd64' in item.name"
  become: true
ansible: install from GitHub release and extract only specific files

Tags

Luiz Felipe F M Costa

I am a quality engineer at Red Hat / Ansible. I love automation tools, games, and coffee. I am also an active contributor to open-source projects on GitHub.

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.