Ansible for Beginners

Gaurav Ashtikar

Ansible for Beginners

Brought you by Gaurav Ashtikar

Contents

  1. Why Ansible?
  2. Installation and Basic setup
  3. Basic Modules
  4. Basic of Playbooks

Why Ansible

  1. Simple to use
  2. Leverages Python
  3. Modular
  4. Open Source
  5. Support multi tier deployment

Ansible: fictional machine capable of instantaneous or superluminal communication.

Installation

  1. Using APT (On Ubuntu)
    • $sudo apt-add-repository ppa:ansible/ansible
    • $sudo apt-get update
    • $sudo apt-get install ansible
  2. Using YUM/DNF (On RHEL/CentOS/Fedora)
    • $sudo yum install ansible
  3. Using PIP (On Mac OS/Other Linux distros)
    • $sudo easy_install pip
    • $sudo pip install ansible

Inventory file

mail.example.com

[webservers]
foo.example.com
bar.example.com

SSH-Keys

  1. Generate SSH key using $ssh-keygen
  2. Make sure you have access to remote system
  3. Add your public key (~/.ssh/id_rsa.pub) into remote ~/.ssh/authorized_keys file

Test your setup

  1. $ansible all -m ping
  2. $ansible all -m ping -u root -k
  3. $ansible all -m ping -K
  4. $ansible all -m ping -f 10

Modules

  1. Libraries that can be executed directly on remote hosts or through Playbooks.
  2. User can also write their own modules
  3. Two types of modules:

Command Modules

System Modules:

File Modules:

Source Control Modules:

Packaging Modules:

Playbooks

Basic Playbook nginx.yml example:

---
- hosts: local
tasks:
  - name: Install Nginx
    apt: pkg=nginx state=installed update_cache=true

To run Playbook : $ansible-playbook -b nginx.yml

Handlers

---
- hosts: local
tasks:
  - name: Install Nginx
    apt: pkg=nginx state=installed update_cache=true
    notify:
      - Start Nginx
handlers:
  - name: Start Nginx
    service: name=nginx state=started

Demo Time

Do it yourself

References

  1. http://docs.ansible.com/ansible
  2. https://serversforhackers.com/an-ansible-tutorial
  3. http://wiki.glitchdata.com/index.php?title=Ansible_Training

Thanks

Fork me on Github