| spring-pet-clinic | student courses  | Automate deployent  |

 

Ansible intro, Manual Deployment,
Ansible Installation by sunil, installation by intelliqit, Automate Deployment using playbook, example1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
Inventory/hosts file, custom inventory file, Ansible Variables, Handlers, Error Handling, when statement, tags, Loops,
Ansible Modules, Ansible command module, Playbook, YAML,  
playbook vs xml,  www.middlewareinventory.com/blog/ansible-playbook-example

 

 

Shell module example1: Installing docker on nodes.

  1. #ansible all -m shell -a 'curl -fsSL https://get.docker.com -o get-docker.sh'
  2. #ansible all -m shell -a 'sh get-docker.sh'
  3. #ansible all -m command -a 'docker --version' (To check docker is installed)
  4. #ansible all -a ' snap remove --purge docker' -b

Shell Module example2: Ansible execute command 'ls -la' and output is saved in a file (redirect output in a file)

  1. #ansible all -m shell -a 'ls -la > file1'

User Module Example1: Create a user in all nodes. Refer docs.ansible here

  1. #ansible all -m user -a 'name=aziz password=India123' (will get permission error as it is executed as ubuntu user, either run as root user or use sudo because passwd file is not having write permission for others)
  2. #ansible all -m user -a 'name=aziz password=India123' -b (-b=become, it is used for getting higher priviledges on the manageed nodes)
  3. #ansible all -m user -a 'name=Abdul password=India123 home=/home/Abdul comment="manager" uid=1234 shell=/bin/bash' -b

Group Module Example1: create a group in all nodes. Refer docs.ansible here

  1. #ansible all -m group -a 'name=group1 state=present' -b

APT Module Example1: install git on all nodes. refer docs.ansible here

  1. #ansible all -m apt -a 'update_cache=yes' -b
  2. #ansible all -m apt -a 'name=git state=present' -b (present = install, absent=uninstall, latest=upgradation)
  3. #ansible all -m command -a 'git --version'
  4. #ansible all -m apt -a 'name=git state=absent' -b
  5. #ansible all -m apt -a 'name=git state=present update_cache=yes' -b

APT Module Excample2: Install tomcat8 and update repository as well.

  1. #ansible all -m apt -a 'name=tomcat8 state=present update_cache=yes' -b
  2. To chedck tomcat8 is installed, in any browser enter publicIP:8080

 File Module Example1: create, delete file / folder, refer docs.ansible here

  1. #ansible all -m file -a 'name=/tmp/file1 state=touch' (create empty file)
  2. #ansible all -m file -a 'name=/tmp/folder1 state=directory'(directory created)
  3. #ansible all -m file -a 'name=/tmp/file1 state=absent' (it will delete)

File Module Example2: File permission

  1. #ansible all -m file -a 'name=file1 state=touch'
  2. #ansible all -a 'ls -l'
    1. -rw-rw-r-- 1 ubuntu ubuntu  (file permission= owner=rw- group=rw- others=r-- owner=ubuntu group=ubuntu)
  3. #ansible all -m file -a 'name=file5 state=touch owner=aziz1 group=group1 mode=700' -b

Copy Module Example1: It copies the file from controller to nodes.

  1. #ansible all -m copy -a 'src=/data1 dest=/tmp'

Copy Module Example2: copy the file but change the owner group and file permission in the destination. actual permission is -rw-r--r-- change to -rwxrwxrwx 1 aziz1 group1

  1. #ansible all -m copy -a 'src=/data1 dest=/ owner=aziz1 group=group1 mode=777' -b

Copy Module Example3: copy new content and overwrite into the destination file.  (data1 has got some text which will be overwrite with content=welcome to star distributors).

  1. #ansible worker1IP -m copy -a 'content="welcome to star distributors\n" dest=/data1' -b

Fetch Module Example1: It fetch or copes from nodes to controller.

  1. #ansible worker1IP -m fetch -a 'src=/data1 dest=/tmp/worker1' -b (it will create a folder worker1 and inside create a folder of privateIP of worker1 in /tmp folder and data1 file copied)if you use all and same file present in all nodes then there will be folder of all nodes IP and file will be copied.  #tree /tmp/worker1
  2. copy file beteen two nodes (worker1 and worker2), it is two step process, it fetch the file from node to controller and copy from controller to second node.
    1. fetch from node to controller: #
    2. copy controller to node: #

Synchronize Module Example1: copy files between two nodes or remote servers.  It ca be done with fetch modules as well. For Synchronize module to work hassle-free,SSH Key-based authentication must be enabled between remote nodes. otherwise, the synchronize task will get stuck and so does your Ansible play.  Ansible Synchronize is more like Ansible RSYNC. the typical rsync feature of Linux.

  1. Synchronize pull method: worker1 to worker2 (execute in worker2) pull the file from worker1.
    1. #

Ping Module Exampe1: Ping with nodes

  1. #ansible all -m ping

Git Module Example1: it is used to clone from git hub repository to all nodes.

  1. #ansible all -m git -a 'repo=https://github.com/aziz27uk/intelliqitdev.git dest=/tmp/mygit' -b (it will create mygit folder and clone all files)

 Service Module Example1: it is used to stop start restart services on nodes.

  1. #ansible all -m service -a 'name=tomcat8 state=restarted' -b
    1. state= restarted (restarting a service)
    2. state= stopped (stop a running service)
    3. state=started (starting a stopped service)

Replace Module Example1: in tomcat server.xml file in which port defined on which tomcat runs. default 8080 port will replace to 5050 in all nodes. (cd /etc/tomcat8/server.xml check connector where port defined)

  1. #ansible all -m replace -a 'regexp=8080 replace=5050 path=/etc/tomcat8/server.xml' -b   (regexp= regular expression the value which need to replace).
  2. #ansible all -m service -a 'name=tomcat8 state=restarted' -b
URI Module Example1:  It is used to check the particular URL is reachable from nodes.
  1. #ansible all -m uri -a 'url=http://facebook.com'  (Outcome will be in green colour as there is no change but only it is testing and status=200)

get_url command example1: download jenkins.war into all nodes

  1.  #ansible ll -m get_url -a 'url=https://get.jenkins.io/war-stable/2.289.3/jenkins.war dest=/tmp' 

 

 

Manual Deployment: Sprint-Pet-Clinic & Student Courses Application.

Spring-Pet-ClinicManual deployment of application (sprint pet clinic, java based), click for quality thought notes

Pre requisite:

  1. create a ubuntu instance.  open ports ssh 8080
  2. Java 8 should be installed.
    1. #sudo apt-get update
    2. #sudo apt-cache search jdk | less
    3. #sudo apt-get install openjdk-8-jdk
    4. #java -version
  3. Download spring pet clinic application
    1. wget https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
    2. #java -jar spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
  4. Access application with public IP of instance and it runs on port 8080
    1. publicIP:8080

Student Courses Application : manual deployment of application is written in python. clone source code from github.

Pre-requisite:

  1. create a ubuntu instance.  open ports ssh 8080
  2. Install python3, PIP-3 and git
    1. #sudo apt-get install python3 python3-pip git -y
  3. clone source code of student courses application from git hub.
    1. #git clone https://github.com/DevProjectsForDevOps/StudentCoursesRestAPI.git
    2. #cd StudentCoursesRestAPI.git #ls
    3. #pip3 install -r requirements
    4. #python3 app.py
  4. Access applcation with public IP and use port 8080

Automate Deployment of application through Ansible: The above two deployments has been done manually, now automate deployment of application using ansible, can be done by writing playbook.

 

 

 

 

Playbook_example1:check colours in the following example playbook and adhoc module.

---
-name: Creating users on managed nodes
  hosts: all
  become: yes
  tasks:
   -name: user creation
    user:
     name: aziz
     password: India123
     home: /home/aziz
     uid: 1010
     shell: /bin/bash
     comment: "manager user"
...

---

-name: titile of the playbook

hosts: all
become: yes
tasks:
  -name:
   -<module_name>:
     <module_arg1>:<module_value1>

 


 ...

Adhoc command:

#ansible all -m user -a 'name=aziz password=India123 home=/home/aziz uid=1010 shell=/bin/bash comment="manager user"' -b

 

Playbook_example2: Create a directory and copy files.

---
- name: create a directory and copy files
  hosts: all
  become: yes
  tasks:
   - name: create a directory
     file:
      name: /tmp/folder1
      state: directory
  - name: copy the /etc/passwd
    copy:
     src: /etc/passwd
     dest: /tmp/folder1
...

#ansible all -m file -a 'name=/tmp/folder1 state=directory

#ansible all -m copy -a 'src=/data1 dest=/tmp'

Playbook_example3: Install git and clone remote repository.

---
- name: Installing git and cloning remote repository
   hosts: all
   become: yes
   tasks:
    - name: installing git in all nodes
      apt:
       name: git
       state: present
       update_cache: yes
    - name: cloning git repository
      git:
       repo: https://github.com/aziz27uk/intelliqitdev.git
       dest: /home/ubuntu/playbookgit
...

#ansible all -m apt -a 'name=git state=present' -b

#ansible all -m git -a 'repo=https://github.com/aziz27uk/intelliqitdev.git dest=/tmp/mygit' -b

 Playbook_example4: Installing tomcat8, copy user.xml, replace 8080 port to 5050, restart comat8 service, check url working in nodes.

 ---
- name: Installing tomcat8, copy user.xml, replace 8080 port to 5050, restart comat8 service, check url working in nodes.
   hosts: all
   tasks:
    - name: installing tomcat8
       apt:
        name: tomcat8
        state: present
        update_cache: yes
    - name: copy tomcat-user.xml file
       copy:
        src: /home/ubuntu/tomcat-user.xml
        dest: /etc/tomcat8
    - name: change port number
       replace:
        regexp: 8080
        replace: 5050
        path: /etc/tomcat8/server.xml
    - name: restart tomcat8
       service:
        name: tomcat8
        state: restarted
    - name: url check of worker1
       uri:
        url: http://172.31.25.210:5050
        status_code: 200
    - name: url check for worker2
       uri:
        url: http://172.31.16.152:5050
        status_code: 200
    - name: url check for worker3
       uri:
        url: http://172.31.39.28:5050
        status_code: 200
 

 Playbook_Example5: Configuring Apache2, create index.html, restart apache2 and check url is reachable.

---

- name: install Apache2, create index.html, restart apache2 and check url
  hosts: all
  tasks:
   - name: Install apache2
     apt:
      name: apache2
      status: present
   - name: edit index.html file
     copy:
       content: "Welcome to star Distributors\n" 
       dest: /var/www/html/index.html
   - name: restart httpd service
     service:
      name: apache2
      state: restarted
   - name: check url is reachable in node worker1
     uri:
      url: http://privateIP:80
      state_code: 200
   - name: check url is reachable in node worker2
     uri:
      url: http://privateIP:80
      state_code: 200

...

 

 Variables: Ansible uses 3 types of variables:

  1. Global Scope variables: The values of varibales are  entered while executing playbook file --extra-vars and they have the highest priority.
  2. Host Scope variables
  3. Play Scope variables: the values of varibales are hardcode in playbook file as default values, you ue --extra-vars to enter variables values at execution of playbook file, if any value is not entered then it will take default hardcode value. It works with only one play.  Any variable declared within vars section within playbook is called as play scope variable.

 Playbook_example6: Playbook for installing/uninstalling software packages using gloabal scope variable.

---
- name: Installing/Uninstalling s/w package
hosts: all
tasks:
- name: Installing, uninstalling, update s/w packages
apt:
name: "{{a}}"
state: "{{b}}"
update_cache: "{{c}}"
...
 Global Scope Variable

Playbook_example7: creating users , assigning home directory and downloading files into users home directory using global scope variable.

---
- name: create user and downloading files into users home directory
  hosts: all
  tasks:
   - name: create user
     user:
      name: "{{a}}"
      password: "{{b}}"
      home: "{{c}}"
- name: downloading files into users home directory
  get_url:
   url: "{{d}}"
   dest: "{{e}}"
...
 Global Scope Variable

Playbook_example13: Play Scope Variable: install software using play scope varibale where variable is hardcode. 

---
- name: install software using play scope variable
  hosts: all
  vars:
   - a: tomcat8
   - b: present
   - c: no
  tasks:
   - name: install software
     apt:
      name: "{{a}}"
      state: "{{b}}"
      update_cache: "{{c}}"
...
Play Scope Variable

 Handlers: In ansible if any module is failed then it will not execute next module. If we want to execute a module only if someother module is executed successfully and it has made some changes(successfull and yellow coloured), handlers are executed only after all the tasks are executed.  Handlers are executed in the order that they are mentioned in the handler section and not in the order they are called in task section.  Even if a handler is called multiple times in the task section it will be executed only once.

Playbook_example12:installing apache2, edit index.html and restart apache2 with handler.

---
- name: installing apache2, edit index.html and restart apache2 with handler
  hosts: all
  tasks:
   - name: Installing apache2
     apt:
      name: apache2
      state: present
   - name: Edit index.html
     copy:
      content: "Welcome to Star Distributors\n "
      dest= /var/www/html/index.html
     notify: restart apache2
  handlers:
   - name: restart apache2
      service:
       name: apache2
       state: restarted
...

 

Handlers: There are two tasks define in the playbook, where task one install apache2 and edit index.html, when these two tasks successful and changed in yellow coloured then it notify to handlers to restart apache2 service, if either one task is not sucessfull or not in yellow coloured (changed) then it will not nofify to handlers.

Playbook_example14:  installing apache2, edit index.html and In handler restart apache2 and check url response in nodes.

---
- name: installing apache2, edit index.html and restart apache2 with handler and check url response
  hosts: all
  tasks:
   - name: Installing apache2
     apt:
      name: apache2
      state: present
     notify: url response on all nodes
   - name: Edit index.html
     copy:
      content: "Welcome to Star Distributors\n"
      dest: /var/www/html/index.html
     notify: restart apache2
   handlers:
    - name: restart apache2
      service:
       name: apache2
       state: restarted
    - name: url response on all nodes
      uri:
       url: "{{item}}"
       status_code: 200
      with_items:
       - http://172.31.32.32
       - http://172.31.42.147
       - http://172.31.31.164
       - http://172.31.31.64
...
Handlers: installing apache2, edit index.html and restart apache2 with handler and check url response

Error Handling: Whenever a module in ansible playbook fails, the execution of playbook stops there.  If we want to continue the playbook executioneven after a module fails we can use error handling, this is implemented by using three keywords of ansible: Block, Rescue, Always. If a code present block section fails control will come into the rescue section, if the block section is successful control does not come to rescue section.  Always section executed everytime irrespective of block section seccess or fails.

Playbook_example15: Error Handling, installing tomcat7 if not tomcat8 and check url response in nodes.

---
- name: Error Handling, installing tomcat7 if not tomcat8 and check url response
  hosts: all
  tasks:
   - block:
       - name: installing tomcat7
         apt:
          name: tomcat7
          state: present
     rescue:
      - name: installing tomcat8
        apt:
         name: tomcat8
         state: present
     always:
      - name: check url is reachable
        uri:
         url: "{{item}}"
         status_code: 200
        with_items:
         - http://172.31.32.32
         - http://172.31.42.147
         - http://172.31.31.164
         - http://172.31.31.64
...
Error Handling, installing tomcat7 if not tomcat8 and check url response in always section.

When Statement: This is similar to if conditions, execute a module only if condition is true/met.

 Playbook_example16: When statement: Installing firewalld when the variable value is 10. 

---
- name: Implementing when statement
  hosts: all
  vars:
   - a: 10
  tasks:
   - name: installing firewalld
      apt:
       name: firewalld
       state: present
      when: a == 10
...

Implementing when statement

Installing firewalld when the variable value is 10.

Debug Module: This is used for printing some output, we can capture the output of any module using register variable and display that using debug module.

Playbook_example17: Create a user and user details are stored in a vaiable and display variable values using debug module.

---
- name: create a user and store values in a variable and display details using debug module
  hosts: all
  tasks:
   - name: create a user
     user:
      name: abdulaziz
      password: India123
      home: /home/abdulaziz
      uid: 1234
     register: userdetails
      - name: display output of user module
         debug:
          var: userdetails
...
create a user and store values in a variable and display details using debug module, when you run playbook it will display details.

Tags: Tags are used for getting for more modular control on the playbook execution, using tag we can excecute only certain section of the playbook.  register is a variable to store capture data.

---
- name: check directory is present, capture info and display
  hosts: all
  tasks:
   - name: check directory present
     stat:
      path: /home/ubuntu
      register: info
  - name: display info
    debug:
     var: info
...

check directory is present, capture info and display.

It will check directory present , if present capture information and display when you run playbook.It will present data info in parent and child format.  info variable is a parent for directory and all information is present in child format.

ex. "info" : {

            "changed": false, (no change in directory), "exists": true, (directory present), "mode": 0755 (permission)

Playbook_example18: Execute playbook particular module using tag.

---
- name: Implementing tags, Install tree, create a user, create a file
  hosts: all
  tasks:
   - name: install tree
     apt:
      name: tree
      state: present
     tags: tree_installation
    - name: create a user
      user:
       name: yezdani
       password: India123
       home: /home/yezdani
       uid: 1235
      tags: user_creation
   - name: create a file
     file:
      name: abc1
      state: touch
...

Implementing tags, Install tree, create a user, create a file

if you run playbook then it will execute all modules, install tree, create user and create a file.

use tag to run specific module.

files will not be created as there is no tag defined.  it only created if run playbook without defining tags.

 

Condition Statement:

Playbook_example19: Check a directory is present in remote nodes.  module stat is used to get information about a file or directory from remote nodes.

 

---
- name: check directory is present, capture info and display
  hosts: all
  tasks:
   - name: check directory present
     stat:
      path: /home/ubuntu
      register: info
  - name: display info
    debug:
     var: info
...

check directory is present, capture info and display.

It will check directory present , if present capture information and display when you run playbook.It will present data info in parent and child format.  info variable is a parent for directory and all information is present in child format.

ex. "info" : {

            "changed": false, (no change in directory), "exists": true, (directory present), "mode": 0755 (permission)

Playbook_example20: Check apache2 is installed by checking /var/www/html/index.html file exist.  The output of any module is captured in jason format, this output can be captured in variable which belongs to register type and it can be displayed using debug module.

---
- name: check directory is present, capture info and display
  hosts: all
  tasks:
   - name: check file present
     stat:
      path: /var/www/html/index.html
     register: info
   - name: display info
     debug:
      var: info
...
it will check file index.html is present or not, if present display information, where you can check exists: true.

Playbook_example21: check the directory is present, if not present then create a directory.  The output of any module is captured in jason format, this output can be captured in variable which belongs to register type and it can be displayed using debug module.

---
- name: check directory is present, capture info and display
  hosts: all
  tasks:
   - name: check folder present
     stat:
      path: /home/ubuntu/folder100
      register: info
   - name: display info
     debug:
      var: info
   - name: create a folder folder100
     file:
      name: /home/ubuntu/folder100
      state: touch
     when: info.stat.exists == false
...

it will check the directory folder100 is present and capture details and display.

we have define parent and child condition as follows:

info.stat.exists == false (info is parent, child is stat, subchild is exists, if it is false (folder100 is not present) then it will create folder.

Include Module: A child playbook can be called in parent playbook and execute.

Playbook_example22:

 

 

 

Playbook_example8: Check nodes uptime

---
- name: Check the remote host uptime
  hosts: all
  tasks:
   - name: Execute the Uptime command over Command module
      register: uptimeoutput
      command: "uptime"

   - debug:
       var: uptimeoutput.stdout_lines
...

 

 

Playbook_example9:Check the node hostname, version, distribution with uname.

---
- name: Check the remote host Hostname, Version, Distribution with UNAME
  hosts: all
  tasks:
   - name: Execute the UNAME command
      register: unameout
      command: "uname -a"

   - debug:
      var: unameout.stdout_lines

...

 

Playbook_example10: Install Apache 2 and make sure it is latest and running.

---
- name: Install apache2, make sure it is lates and running
  hosts: all
  become: yes
  become_user: root
  tasks:
   - name: Install apache2 latest version
     apt:
      name: apache2
      state: latest
   - name: ensure apache is running
     service:
      name: apache2
      state: started
...

 

Playbook_example11: Apache2 restart

---
- name: restart apache web server
  hosts: all
  become: yes
  become_user: root
  tasks:
   - name: restartapache
     service:
      name: apache2
      state: restarted
...
 

 

 

 

 

hosts: all

become: yes

tasks:

  - name: install git

    package:

        name: git

        state: present

  -name: uninstall git

   package:

       name: git

       state: absent

 Install tree package in ubuntu machine using playbook automation: 

manual steps:

  1. #sudo apt-get update
  2. #sudo apt-get install tree -y

The above manual execution perform in ansible automation:

  1. inventory file
  2. playbook

playbook:The above manual commands done with automation in playbook

hosts: all

become: yes

tasks:

  -name: update ubuntu packages

  -name: install tree

   3. module: need module to automate, select pre-defined modules and can write custom modules (required python code).

  1. Search the modules in google for the above apt-get update (apt-get update in ansible)

   4. based on search result from google, fill the module in playbook

hosts: all

become: yes

tasks:

  -name: update ubuntu packages and install tree

   apt:

       name: tree

       update_cache: yes

       state: present

  5. log into ansible controller.

  6. inventory file

  7. create yaml file

  8. run yaml file