Ansible Playbooks yml Tutorials



Ansible playbook are just like normal file with ".yml" extension. Playbook (.yml) file understand set of define syntax. YAML is easier for humans to read and write than other common data formats like XML or JSON.


Ansible Flow
















e.g .yml:

---
# A list of activity
- Ping check
- Install
- Verify installation



Where as "---" This is part of the YAML format and indicates the start of a document

sample_test.yml

###################################################################
#                                       This yml file is used for                                                    #
#                                       Unix REHL 6 server                                                         #
#                                       Created by: Vinod Kumar                                                #
#                                       Created Date: 2014-09-01                                                #
##################################################################
---
- hosts: local
  sudo: yes
  vars:

##################################################################
# This is the task section for all activities                                                                #                    
##################################################################
  tasks:

     # #############################################################
     # This is for host checking alive or not                                                             #
     # #############################################################
     - name: 1. Check that the servers alive
       action: ping

     # #############################################################
     # Creating Apache group                                                                                  #
     # #############################################################
     - name: 2. create apache group
       action: group name=apache

     # #############################################################
     # Creating Apache user                                                                                     #
     # #############################################################
     - name: 3. create apache user
       action: user name=apache home=/var/www group=apache shell=/sbin/nologin

     # #############################################################
     # Installing the required RPM's if not there and restarting apache                   #
     # #############################################################
     - name: 4. installing the httpd zlib libxml2 libxslt
       yum: name={{ item }} state=present
       with_items:
       - httpd
       - zlib
       - libxml2
       - libxslt
       notify:
       - restart apache
     # ##############################################################
     # Ensuring Apache is reuunig                                                                             #
     # ##############################################################
     - name: 5. Ensure apache is running
       service: name=httpd state=started

  # ###############################################################
  # Handler for apache restart                                                                                  #
  # ###############################################################
  handlers:
    - name: restart apache
      service: name=httpd state=restarted


How to run yam file from Ansible:

[ansible@tutorialbyexample ansible]$  ansible-playbook -vvvv  sample_test.yml -u ansible -K
pass the password of you ansible user.

[ansible@tutorialbyexample ansible]$  ansible-playbook -vvvv sample_test.yml -u ansible -K
sudo password:

OutPut:

PLAY [local] ****************************************************************

GATHERING FACTS ***************************************************************
<127.0.0.1> ESTABLISH CONNECTION FOR USER: ansible on PORT 22 TO 127.0.0.1
<127.0.0.1> REMOTE_MODULE setup
<127.0.0.1> EXEC /bin/sh -c 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1426093853.44-274659780567368 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1426093853.44-274659780567368 && echo $HOME/.ansible/tmp/ansible-tmp-1426093853.44-274659780567368'
<127.0.0.1> PUT /tmp/tmpBTra2o TO /home/ansible/.ansible/tmp/ansible-tmp-1426093853.44-274659780567368/setup
<127.0.0.1> EXEC /bin/sh -c 'sudo -k && sudo -H -S -p "[sudo via ansible, key=fbmnnsxibviyayrcbosstdauprsqorqq] password: " -u root /bin/sh -c '"'"'echo SUDO-SUCCESS-fbmnnsxibviyayrcbosstdauprsqorqq; LANG=C LC_CTYPE=C /usr/bin/python /home/ansible/.ansible/tmp/ansible-tmp-1426093853.44-274659780567368/setup; rm -rf /home/ansible/.ansible/tmp/ansible-tmp-1426093853.44-274659780567368/ >/dev/null 2>&1'"'"''
ok: [127.0.0.1]

TASK: [1. Check that the servers alive] ***************************************
ok: [127.0.0.1] => {"changed": false, "ping": "pong"}

TASK: [2. create apache group] ************************************************
ok: [127.0.0.1] => {"changed": false, "gid": 48, "name": "apache", "state": "present", "system": false}

TASK: [3. create apache user] *************************************************
ok: [127.0.0.1] => {"append": false, "changed": false, "comment": "Apache", "group": 48, "home": "/var/www", "move_home": false, "name": "apache", "shell": "/sbin/nologin", "state": "present", "uid": 48}

TASK: [4. installing the httpd zlib libxml2 libxslt] **************************
ok: [127.0.0.1] => (item=httpd,zlib,libxml2,libxslt) => {"changed": false, "item": "httpd,zlib,libxml2,libxslt", "msg": "", "rc": 0, "results": ["Repository 'rhel-debuginfo' is missing name in configuration, using id providing httpd is already installed", "Repository 'rhel-debuginfo' is missing name in configuration, using id providing zlib is already installed", "Repository 'rhel-debuginfo' is missing name in configuration, using id providing libxml2 is already installed", "Repository 'rhel-debuginfo' is missing name in configuration, using id providing libxslt is already installed"]}

TASK: [5. Ensure apache is running] *******************************************
ok: [127.0.0.1] => {"changed": false, "name": "httpd", "state": "started"}

PLAY RECAP ********************************************************************
127.0.0.1             : ok=6    changed=0    unreachable=0    failed=0

This is all about for playbook YML. For more details see the http://docs.ansible.com/playbooks_intro.html

References

http://www.ansible.com/

1 comment: