Ansible Playbook Example-1

What’s with the Name “Ansible”?

 Ansible was used to control a large number of remote ships at once, over vast distances. Think of it as a metaphor for controlling remote servers.

Introduction:-

Ansible playbooks are a way to send commands to remote computers in a scripted way. Instead of using Ansible commands individually to remotely configure computers from the command line, you can configure entire complex environments by passing a script to one or more systems.

Ansible playbooks are written in the YAML data serialization format. If you don’t know what a data serialization format is, think of it as a way to translate a programmatic data structure (lists, arrays, dictionaries, etc) into a format that can be easily stored to disk. The file can then be used to recreate the structure at a later point. JSON is another popular data serialization format, but YAML is much easier to read.

Playbooks are written in YAML. Read more about YAML here: http://en.wikipedia.org/wiki/YAML

Before moving ahead go through previews blog here

Playbooks Overview

  • Playbooks are the main construct within Ansible.
  • Playbooks have a list of Plays.
  • Plays map hosts to tasks. Tasks are basically calls to Ansible modules.
  • Playbooks structure looks like:
    • Playbooks contain Plays.
    • Plays contain Tasks
    • Tasks call Modules.
  • The most basic formal of a playbook is as follows:
---
- hosts:
  vars:
  remote_user:
  tasks:
  - name:

The file starts with:

---

This is a requirement for YAML to interpret the file as a proper document. YAML allows multiple “documents” to exist in one file, each separated by ---, but Ansible only wants one per file, so this should only be present at the top of the file.

YAML is very sensitive to white-space, and uses that to group different pieces of information together. You should use only spaces and not tabs and you must use consistent spacing for your file to be read correctly. Items at the same level of indentation are considered sibling elements.

  • host defines which hosts/groups this playbooks should be applied to. This is a list so multiple can be entered separated by colons.
  • vars
  • remote_user defines the users name for accessing the hosts. Remote_user can be used under tasks as well.
  • tasks
  • name is the name of the task, could be anything, as long as it is descriptive.

==========================================================================

Lets start with playbook –

consider that you have setup the ssh-keygen and you can ssh with root access.

just make sure that you are able to ssh without password.

Screenshot from 2016-07-10 17-10-16

  • considering my remote host as 192.168.0.102.
  • created a hosts file as

[rupin]
dev ansible_host=192.168.0.102 ansible_user=root

Screenshot from 2016-07-10 17-22-07

Ansible Example1:-

Ansible playbook for Simple ping:-

#vim ping.yml

---
 - hosts: rupin
   gather_facts: False
   user: root
   tasks:
     - name: test connection   
       ping:

Save the file

NOTE:- Spacing plays an important role in playbook and don't use tab

# ansible-playbook -i hosts  ping.yml

Screenshot from 2016-07-10 17-28-50

Got successful Ping.

you can also wget the code  from GIT HUB

wget https://raw.githubusercontent.com/rupin147/ansible-playbook-example/master/ansible%20playbook%20to%20ping%20remote%20host%20ping.yml

—————————–x———————–x——————–

cowsay is a program which generates ASCII pictures of a cow with a message.you can install for ansible output in that type.

Screenshot from 2016-06-28 23-16-53.png

so the ansible look now similar like..

Screenshot from 2016-06-28 23-17-24

Note to Disable :-

If you don’t want to see the cows, you can disable cowsay by setting the
ANSIBLE_NOCOWS environment variable like this:
# export ANSIBLE_NOCOWS=1
You can also disable cowsay by adding the following to your ansible.cfg file.
[defaults]
nocows = 1

For random ansible cow

use  #export ANSIBLE_COW_SELECTION=random

Screenshot from 2016-06-28 23-21-16

 

Ansible Example2:-

Ansible playbook to copy file from ansible server to remote host:-

---
 - hosts: rupin
   gather_facts: False
   user: root
   tasks:
   - name: copy file from ansible server to remote host
     copy: src=/etc/ansible/hosts dest=/tmp/rupin

you can also wget the code

#wget https://raw.githubusercontent.com/rupin147/ansible-playbook-example/master/copy.yml
  • ways to represent the module in ansible  playbook  :-

(((((((((((((((((((OR))))))))))))))))))))))

---
 - hosts: rupin
 user: root
 tasks:
 - name: copy from my laptop to remote machine
 copy:
 src=source/file1
 dest=/tmp/file2

((((((((((((((((((((OR))))))))))))))))))))))
---
 - hosts: rupin
 user: root
 tasks:
 - name: copy from my laptop to remote machine
 copy:
 src: source/file1
 dest: /tmp/file2

 

Ansible Example3:-

Ansible playbook  to create a file on remote host with some content  :-

---
   - hosts: rupin
       gather_facts: False
       user: root
       vars:
           welcome: "intro to ansible"
       tasks:
          - name: ping
              ping:
          - name: sample motd
              copy:
                     dest: /tmp/motd
                     content: "hiiiiiii ansible ...................."

wget the code from GITHUB

#wget  https://raw.githubusercontent.com/rupin147/ansible-playbook-example/master/content.yml

Ansible Example4:-

Ansible playbook  to install,copy fiels,service restart:-

---
    - hosts: rupin
        gather_facts: False
        user: root
        tasks:
        - name: Install webfs
          apt: pkg=webfs state=installed update_cache=true
        - name: copy from my laptop to remote machine
          copy:
            src: install.yml
            dest: /tmp/install.yml
        - name: apache service restart
          service: name={{ item }} state=restarted
          with_items:
                    - apache2
                    - nginx
                    - ssh

Or

wget the code from GITHUB

#wget https://raw.githubusercontent.com/rupin147/ansible-playbook-example/master/installmultiple.yml

Ansible Example5:-

Ansible playbook  to create new user with login password:-

create a encrypted paassword with the below command

#mkpasswd --method=SHA-512

it will prompt for password enter the password

Screenshot from 2016-07-10 19-54-43.png

i my case my password is 123

---
  - hosts: rupin
    gather_facts: False
    user: root
    tasks:
      - name: create user
        user: name=test password=$6$I0Y70YelIjQq$PuW7P.u8XHROaBm0LyEMe2KrqcYRysGGlmgAlQ8uVeHZ5Mu6fLJ8BTBtoAfwA7k0zf3CUxNQs1OuSc1tIuVpg.

wget the code from GITHUB:-

#wget https://raw.githubusercontent.com/rupin147/ansible-playbook-example/master/user.yml

 

 

 

Advertisements

One thought on “Ansible Playbook Example-1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s