Categories
DevOps

Ansible

  1. What is Ansible?
  2. Connect to Remote Host
  3. Ansible Playbooks
  4. Handlers

What is Ansible?

An automation engine that allows for agentless system configuration and deployment.

Ansible is an agentless automation tool that you install on a single host (referred to as the control node). From the control node, Ansible can manage an entire fleet of machines and other devices (referred to as managed nodes) remotely with SSH, Powershell remoting, and numerous other transports, all from a simple command-line interface with no databases or daemons required.

Connect to Remote Host

  • SSH
    • Password with -k flag
    • Common practice: pre-shared key with a designated user for Ansible across all systems

Ansible Playbooks

Ansible playbook is a yaml file to provide instruction to Ansible. A single play is a series of steps to be performed on a host or group of hosts.

  • Check mode to do a quick sanity check
  • Retry file with –-limit flag to retry on only failed nodes
  • Add variables
  • Use Facts
  • Store result of task using register
--- # Bootstrap Webservers // Start of a yaml file
- hosts: webservers 
  become: yes              // sudo every cmd
  vars: 
    target_service: httpd
  gather_facts: yes        // Default yes
  tasks:
  - name: install httpd
    yum:                   // module in this task
      name: httpd          // Parameters of the task
      state: latest
  - name: create index.html file
    file:
      name: /var/www/html/index.html
      state: touch
  - name: add web content
    fileinfile:
      line: "{{ ansible_hostname}}" // using facts
      path: /var/www/html/index.html
    register: task_debug
  - debug:
      msg: "Output of lineinfile is : {{ task_debug}}"
  - name: start httpd
    service: 
      name: "{{ target_service }}"
      state: started

Handlers

Tasks that will be executed under conditions.

A handler may be called using the notify keyword, to be flagged for execution when a task performs a change.

The execution of the handler will be after the execution of the play.

Leave a comment