Skip to content

Instantly share code, notes, and snippets.

@mrc0der
Last active April 6, 2021 21:46
Show Gist options
  • Save mrc0der/32a687851b25cf984bbcd0f7b921768c to your computer and use it in GitHub Desktop.
Save mrc0der/32a687851b25cf984bbcd0f7b921768c to your computer and use it in GitHub Desktop.
Ansible 101 and some more

Ansible

Overview

Ansible is a tool which uses no agents, so it's easy to deploy - it uses a very simple language (YAML, in the form of Ansible Playbooks) that allow you to describe your automation jobs.

Ansible is able to execute commands or playbooks across multiple instances simultaneously

0. Installing

Requirements:

  • ansible package locally
  • python3 preferred on target host, but python2 avail as a workaround.

Install with:

  • sudo yum|apt-get|dnf install ansible
  • usually xxx install ansible whatever your package manager of choice

1. Commands

You can run coomands / modules form the cli wtih no YAML needed.

  • Ping - ansible all -m ping
    • ping - Check all hosts connectivity
    • Assumes Inventory file configured (see below)
  • Using Modules - ansible web-1.example.com -m yum -a "name=httpd state=installed"

2. Inventory

The inventory file is a lookup table for Ansible to use when targeting individual hosts or groups or machines. This sits on your local machine that you are pushing out to - you can also store this in a repo alongside your playbook.

$> cat /etc/ansible/hosts
mail.example.com
mail.example.com

[webservers]
web-1.example.com
web-2.example.com

[dbservers]
db-1.example.com
db-2.example.com
db-3.example.com

3. Playbook (collection of tasks)

install-ubuntu-packages.yml

- name: Basic system-wide software installation
  hosts: all
  become: yes
  tasks:

    - name: Add official git PPA
      apt_repository:
        repo: ppa:git-core/ppa
        codename: "{{ dist_override | default(omit) }}"
        filename: "git"

    - name: add asciinema PPA
      apt_repository:
        repo: ppa:zanchey/asciinema
        codename: "{{ dist_override | default(omit) }}"
        filename: "asciinema"

    - name: Install latest versions of apt packages
      apt:
        name:
          - asciinema   # for recording teminal sessions
          - build-essential
          - flip
          - git
          - htop
          - ipython

Playbooks can import and chain other playbooks, a better way to go is using Roles

- import_playbook: general-system-config.yml
- import_playbook: dotfiles-and-cli-setup.yml
- import_playbook: install-ubuntu-packages.yml

4. Ansible Roles / Ansible Galaxy

You can have your own local roles in the same dir as your playbooks, or you can leverage the "Galaxy" / pacakge manager for Ansible Roles.

Example https://github.com/geerlingguy/ansible-role-mysql

See more https://www.ansible.com/overview/how-ansible-works

  • Install the role
    • ansible-galaxy install geerlingguy.mysql
  • Create Playbook - mysql-install.yml
    •   - hosts: database
          roles:
            - role: geerlingguy.mysql
              become: yes
      
  • Create vars file - default/vars.yml
    •   mysql_user_home: /root
        mysql_user_name: root
        mysql_user_password: root
      
  • More info on this example Role - https://galaxy.ansible.com/geerlingguy/mysql

5. Example Playbooks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment