2 minute read

Topics that will be covered

  1. Introduction to Ansible.

  2. Overview of Ansible Architecture and Basic modules.

  3. Overview and demo of Ansible Tower.

  4. Ansible in Real Life.

  5. Pros and Cons of Ansible vs Other Configuration management tools.

  6. Workshop

Goal - Following are the tasks to be performed as part of Ansible workshop:

  1. Executing playbook task1.yml locally, which performs the operation

    Creating directory using Shell, File modules Demonstrate register and debug modules Install tree using Yum module

  2. Executing playbook task2.yml locally, which demonstrates use-case for

    Using vars module Using with_items module Using When module Using get_url Using Find

  3. Executing playbook task3.yml (on remote machine-Optional), which performs the operation of Installing Apache Using Handlers Modules

Tasks

Check ansible version.

ansible --version

Check if you’re able to ping localhost

ansible localhost –m ping

Task 1

Create a file named task1.yml and copy past content in blue into the same file.

---
- hosts: localhost
  become: yes
  tasks:
  - name: create a directory using shell
    shell: mkdir /var/log/test_dir1

  - name: Create a directory using file
    file: path=/var/log/test_dir2 state=directory mode=0755

  - name: list file or directory content
    shell: ls -l
    register: result
  - debug: var=result

  - name: Install tree
    apt: name=tree state=latest

Command to execute

ansible-playbook task1.yml

Verification: You can find both directories named “test_dir1” and “test_dir2”

ls –l /var/log

Task 2

Create a file named task2.yml and copy past content in blue into the same file.

---
- name: Task-02
  hosts: localhost
  gather_facts: yes
  become: yes
  vars:
       package: ['htop','unzip','traceroute']
  tasks:
  - name: install httpd
    apt: name={{ item }} state=present
    with_items: package
    when: ansible_os_family=="Debian"

  - name: Download Cirros Image from HTTP link
    get_url: url="http://download.cirros-cloud.net/0.3.5/cirros-0.3.5-x86_64-disk.img" dest="/tmp/" mode=0644

  - name: Create a file
    file:  path=/tmp/test.conf state=touch mode="u=rw,g=r,o=r"

  - name: Find file created as part of above task
    find: paths="/tmp/" patterns="test.*"
    register: result
  - debug: var=result

  - name: Create a cron job to monitor disk space
    cron: name="Monitor_Disk" minute=0 hour="1" user=cloud job="sudo df -H >> /tmp/output.txt"
    tags: disk, monitor, df

Command to execute

ansible-playbook task2.yml

Verification: Execute using Tags. You will find Cirros Image downloaded and file created

ls /tmp 

Execute using Tags

ansible-playbook task2.yml –t disk
ansible-playbook task2.yml –t monitor
ansible-playbook task2.yml –t df

Task 3

Create a file named task3.yml and copy past content in blue into the same file.

---
- name: Task-03
  hosts: localhost
  become: yes
  tasks:
  - name: ensure apache is at the latest version
    apt: pkg=apache2 state=installed update_cache=true
    notify:
     - Start Apache2

  - pause: seconds=5

  - name: Copy html file
    copy: src=/tmp/index.html dest=/var/www/html/index.html owner=ubuntu group=ubuntu mode=0644

  handlers:
    - name: Start Apache2
      service: name=apache2 state=restarted

Command to execute

ansible-playbook task3.yml 
comments powered by Disqus