Skip to content

Using Ansible

Automates application deployment and infrastructure configuration.

Install Ansible from this link for your OS.

Three key areas:

  • IT automation
  • Configuration management
  • Automatic deployment

Ansible Hosts File

The host file, located at /etc/ansible/hosts, is where you configure your "inventory". That is, the clients and servers, their addresses, usernames, passwords, etc.

Example If you're looking to deploy an Ansible playbook locally, this is how you can configure a localhost.

/etc/ansible/hosts

ini
[braden_local]
localhost ansible_connection=local ansible_user=braden
[braden_local]
localhost ansible_connection=local ansible_user=braden

Playbooks

A playbook is a core component of the Ansible ecosystem; it is a human readable set of instructions specifying what tasks should be carried out, for which hosts, and how everything should be configured. While the syntax is clear once everything has been written, the author of a playbook typically needs to conduct some research (or have some prior experience) in order to develop the playbook they envision.

Example: Simple Playbook with Tasks

yaml
---
- name: sample playbook
  hosts: braden_local # Set of hosts/nodes in /etc/ansible/hosts
  become: true # Requires root elevated permissions
  tasks:
    - name: create directories
      file:
        path: /home/{{ ansible_user }}/dev
        state: directory
    - name: install rsync
      apt:
        update_cache: true # Runs apt update
        state: latest
        pkg:
          - rsync
---
- name: sample playbook
  hosts: braden_local # Set of hosts/nodes in /etc/ansible/hosts
  become: true # Requires root elevated permissions
  tasks:
    - name: create directories
      file:
        path: /home/{{ ansible_user }}/dev
        state: directory
    - name: install rsync
      apt:
        update_cache: true # Runs apt update
        state: latest
        pkg:
          - rsync

**Example: Playbook with Roles

yaml
---
- hosts: braden_local # Set of hosts/nodes in /etc/ansible/hosts
  vars: "{{ global_variable }}"
  become: true # Requires root elevated permissions
  roles:
    - task_a
    - task_b
    - task_c
---
- hosts: braden_local # Set of hosts/nodes in /etc/ansible/hosts
  vars: "{{ global_variable }}"
  become: true # Requires root elevated permissions
  roles:
    - task_a
    - task_b
    - task_c

Example: Playbook with variable prompt, tasks, and roles

yaml
---
- hosts: "{{ target_in_hosts_file }}"
  vars:
    - foo_dir: /home/{{ ansible_user_id }}/oo

  vars_prompt:
    - name: a_version
      prompt: Install which version? [e.g. 0.2.7]
      private: false

  pre_tasks:
    - name: Validate something
      import_tasks: 'tasks/validate_input_variables.yml'
    - name: Setup base files
      become: true
      ansible.builtin.lineinfile:
        create: true
        path: "/etc/important_root_file"
        mode: "0444"
        line: "Host: {{ target_in_hosts_file }} with {{ a_version }}"

  tasks:
    - name: Task A
	  # ...
	- name: Task B
	  # ...

  roles:
    - role_a
    - role_b

  post_tasks:
    - name: Finalization task
      # ...
---
- hosts: "{{ target_in_hosts_file }}"
  vars:
    - foo_dir: /home/{{ ansible_user_id }}/oo

  vars_prompt:
    - name: a_version
      prompt: Install which version? [e.g. 0.2.7]
      private: false

  pre_tasks:
    - name: Validate something
      import_tasks: 'tasks/validate_input_variables.yml'
    - name: Setup base files
      become: true
      ansible.builtin.lineinfile:
        create: true
        path: "/etc/important_root_file"
        mode: "0444"
        line: "Host: {{ target_in_hosts_file }} with {{ a_version }}"

  tasks:
    - name: Task A
	  # ...
	- name: Task B
	  # ...

  roles:
    - role_a
    - role_b

  post_tasks:
    - name: Finalization task
      # ...

Push Configuration

Ansible is a push configuration management tool

![[ansible-architecture.png]]

Mitogen

Speed up Ansible immenesly with mitogen https://mitogen.networkgenomics.com/ansible_detailed.html#noteworthy-differences

Using Variables

Fill out...

Ansible Vault

Fill out...

Essential Ansible Modules

[! Note] It is advisable to use the fully qualified collection name (FQCN) modules, e.g., ansible.builtin.<module>. For brevity, builtin modules will only have the <module> names below, otherwise the FQCN will be given.

apt

Install one or many packages with the APT package manager.

yaml
ansible.builtin.apt:
  become: true
  state: present
  update_cache: true
  cache_valid_time: 3600
  name:
    - build-essential
    - cmake
    - git
ansible.builtin.apt:
  become: true
  state: present
  update_cache: true
  cache_valid_time: 3600
  name:
    - build-essential
    - cmake
    - git

stat

Testing Ansible

Two great tools for testing Ansible setups are

  1. Vagrant. Lets you use a VM to test the deployment of your ansible playbooks via a Vagrantfile, e.g.,
Vagrant.configure("2") do |config|
  config.vm.box = "generic/ubuntu2004"

  config.vm.define "testbed"
  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "ansible/playbooks/bootstrap-khadas.yml"
    ansible.extra_vars = {
      target: "testbed",
      package_type: "dev",
      start_services: true
    }
    ansible.groups = {
      "testbeds" => ["testbed"]
    }
  end
end
Vagrant.configure("2") do |config|
  config.vm.box = "generic/ubuntu2004"

  config.vm.define "testbed"
  config.vm.provision "ansible" do |ansible|
    ansible.playbook = "ansible/playbooks/bootstrap-khadas.yml"
    ansible.extra_vars = {
      target: "testbed",
      package_type: "dev",
      start_services: true
    }
    ansible.groups = {
      "testbeds" => ["testbed"]
    }
  end
end
  1. Molecule. Lets you test and develop Ansible roles in a Docker environment.

Tips and Tricks

In Vim use :set ft=yaml.ansible to set the filetype to be ansible and enable the language server.

ADDITIONAL RESOUCRES