Skip to content

Ansible

Ansible is a IT automation tool, enabling infrastructure as code.

Quickstart

inventory.yaml
virtualmachines:
  hosts:
    multipass:
      ansible_host: <ip address>
      ansible_user: ubuntu
ansible virtualmachines -m ping -i inventory.yaml
playook.yaml
- name: My first play
  hosts: virtualmachines
  tasks:
    - name: Ping my hosts
      ansible.builtin.ping:
    - name: Print message
      ansible.builtin.debug:
        msg: Hello world
ansible-playbook -i inventory.yaml playbook.yaml

Usage

CLI

ansible-playbook -i /path/to/my_inventory_file -u my_connection_user -k -f 3 -T 30 -t my_tag -m /path/to/my_modules -b -K my_playbook.yml

Loads my_playbook.yml from the current working directory and:

  • -i - uses my_inventory_file in the path provided for inventory to match the pattern.
  • -u - connects over SSH as my_connection_user.
  • -k - asks for password which is then provided to SSH authentication.
  • -f - allocates 3 forks.
  • -T - sets a 30-second timeout.
  • -t - runs only tasks marked with the tag my_tag.
  • -m - loads local modules from /path/to/my/modules.
  • -b - executes with elevated privileges (uses become).
  • -K - prompts the user for the become password.

Useful Tasks

Add Ubuntu repository

- name: Add HashiCorp repo
  block:
    - name: Download GPG key
      ansible.builtin.get_url:
        url: https://apt.releases.hashicorp.com/gpg
        dest: /etc/apt/trusted.gpg.d/hashicorp.asc

    - name: Add repo
      ansible.builtin.apt_repository:
        repo: "deb [signed-by=/etc/apt/trusted.gpg.d/hashicorp.asc] https://apt.releases.hashicorp.com {{ ansible_distribution_release }} main"
        state: present

Add user to group

- name: Ensure "docker" group exists
  ansible.builtin.group:
    name: docker
    state: present

- name: Add "ubuntu" user to "docker" group
  ansible.builtin.user:
    name: ubuntu
    groups: docker
    append: true

Enable and start services

- name: Enable and start Nomad
  ansible.builtin.systemd:
    name: nomad
    state: started
    enabled: true

Install

Fedora

dnf install ansible
pip3 install --user molecule molecule[docker]

Resources


Last update: August 11, 2023
Created: June 3, 2023