Wednesday, October 24, 2018

Ansible Quick Start

Ansible Quick Start

Ansible Quick Start Tasks and Solutions

Installation and Configuration

1
Task 1: Install Ansible
Pre-requisites:
1
2
$ sudo yum instal epel-release
$ sudo yum update
Solution:
Step 1: Install Ansible with required dependencies
1
 $ sudo yum install git python python-devel python-pip openssl ansible
Step 2: Check Whether Ansible installed or not
1
 $ ansible --version
============================================
1
Task 2: Configure ansible
Solution:
Step 1: Modify the ansible configuration file as below
1
 $ sudo vi /etc/ansible/ansible.cfg
→ Uncomment the below code in ansible.cfg file
inventory = /etc/ansible/hostssudo_user = root
Step 2: Take the backup of hosts file under ansible directiory
1
2
3
$ cd /etc/ansible         #Change to ansible directory
$ sudo mv hosts hosts.original       #Backup hosts file
$ sudo vi hosts
→ Add the hostnames under hosts file
[group-name1]
hostname1
hostname2 ..
[group-name2]
hostname1
hostname2 …
→ Save and Close
→ Here we are going to use ansible as non-previleged user that must have sudo rights.
→ To get the permissions we need to setup some settings
Step 3: Create a user for ansible
1
2
$ sudo adduser ansible
$ sudo passwd ansible
New password:
Re-Enter password:
1
 $ sudo visudo       #Add the ansible user
→ Find the below code
## Allows root to run any commands anywhere
root ALL=(ALL) ALL
→ Add the below lines at the above code
ansible ALL=(ALL) NOPASSWD: ALL
→ Save and Close
NOTE: Repeat the Step 3 on all the hosts that are added in /etc/ansible/hosts file.
=====================================
1
Task 3: Setup ssh-key exchange
Solution:
Step 1: Generate ssh-key under ansible directory
1
2
 $ su ansible    # Login to ansible user
$ ssh-keygen    # Generate ssh-key
Step 2: Copy the generated key on to the nodes
1
2
$ ssh-copy-id username@hostname or node IP
$ ssh-copy-id ansible@hostname or node IP     #Give the hostname or node IP as in hosts files
Step 3: Login remotely onto the node
→ When logged into the node it wouldn’t ask the password after copying the ssh-key.
1
2
$ ssh ansible@hostname or node IP
$ exit
NOTE: Repeat Step 2 and Step 3 for the nodes that are added in hosts file.
Step 4: copy the generated key onto the ansible server to login witout password
1
 $ ssh-copy-id server-ip or server-hostname
→ Check the conectivity
1
2
$ ssh server-hostname # It must be logged in without password
$ exit

Running Ansible Commands

1
Task-1: Run Ansible commands
Solution:
Step 1: Run a single command on all the hosts that are present in the hosts file
1
 $ ansible all -m ping     #Here all indicates all the hosts that are present in hosts file
Step 2: List everything under /home/ansible directory on all nodes
1
 $ ansible all -a “ls -al /home/ansible”
Step 3: View the content under previleged /var/log/messages file
1
 $ ansible all -a “cat /var/log/messages”    #We need to be root to view the previleged files
→ Modify the above command a little bit
1
 $ ansible all -s -a “cat /var/log/messages”      # Now we can view the content of the file without password.
Copy the files to the nodes using ansible
Step 4: Create a directory and create a text file under the directory
1
2
3
 $ cd
$ cd ansible # change to the directory ansible
$ sudo vi test.txt
→ Add some content into the text file
→ Save and Close
Step 5: Copy the text file to the nodes
1
 $ ansible hostname(or groupname) -m copy -a “src=test.txt dest=/tmp/test.txt”
Install and remove the packages on the nodes using ansible
Step 6: Install the package
1
 $ ansible hostname(or groupname) -s -m yum -a “name=name of the package to be installed state=latest”
Step 7: Remove the packages
1
 $ ansible hostname(or groupname) -s -m yum -a “name=name of the package to be Removed state=absent”
Adding and Removing user on the nodes using ansible
Step 8: Adding user to the nodes
1
 $ ansible hostname(or groupname) -s -m user -a “name=test”
Step 9: Removing user on thr nodes
1
 $ ansible hostname(or groupname) -s -m user -a “name=test state=absent”

Playbook Structure with YAML

1
Task: Write a playbook to install httpd package
Solution:
Step1: Create a .yaml file
1
2
3
4
 $ cd ansible
$ sudo mkdir playbook
$ cd playbook
$ sudo vi structure.yaml
---      #This is a structural YAML example to install HTTPD
- hosts: hostname(or group-name)    #give the hostname or group-name which has mentioned in the hosts file
  remote_user: ansible         #The user you setup
  become: yes      #Going to become troot user through become_method
  become_method: sudo          #It defined in the configuration file by default
  connection: ssh
  gather_facts: yes         #Its gets the information about the operating system
  vars:
    username: myuser
  tasks:
  - name: Install HTTPD server on <specified node>
    yum:          #Module how you are going to install the package
      name: httpd
      state: latest
      notify:
      - startservice
    handlers:
    - name: startservice
      service:
      name: httpd
      state: restarted
→ Save and Close
Step 2: calling a playbook
1
 $ ansible-playbook structure.yml
Step 3: check the status of httpd on the specified node
1
 $ sudo systemctl status httpd
Step 4: Delete the installed package
1
$ ansible hostname(or group-name) -s -m yum -a “name=httpd state=absent”

Gathering Facts

Task: Gathering Facts
1
2
3
4
5
6
7
8
9
10
11
$ ansible all --list-hosts        #To see the list of hosts under hosts file
$ cd /ansible/playbooks
$ ansible hostname(or group-name) -m setup      # Setting up the gathering facts
$ ansible hostname(or group-name) -m setup | grep ipv4
$ ansible hostname(or group-name) -m setup -a ‘filter=*ipv4*’      #gathering facts of ipv4
$ ansible hostname(or group-name) -m setup -a ‘filter=*default*’     # gathering facts for default
$ ansible hostname(or group-name) -m setup --tree facts    # Gathering the facts to fats directory
$ cd facts
$ vi hostname #you can find all the facts in this file
$ ansible hostname(or group-name) -m setup -a ‘filter=fact-name’
$ ansible all -m setup --tree facts #Gathering all the hosts facts into a single directory

Variable Substitution

1
Task 1: How to assign variables in playbook
Solution:
Step 1: create a playbook that has variables to install package
1
2
 $ cd ansible/playbook
$ vi vartest.yaml
--- # Testing how variables work in the vars section of a playbook
- hosts: ‘{{ myhosts}}’
  remote_user: ansible
  become: yes
  become_method: sudo
  connection: ssh
  gather_facts: ‘{{ gather }}’
  vars:
    myhosts: hostname(or group-name)
    gather: yes
    pkg: telnet
  tasks: 
  - name: Install the indicated software
    yum:
      name: ‘{{ pkg }}’
      state: latest
Step 2: Run playbook
1
 $ ansible-playbook vartest.yaml
Step 3: Remove the package
1
$ ansible hostname(or group-name) -s -m yum -a “name=telnet state=absent”
Step 4: Install the package through command line
1
 $ ansible hostname(or group-name) -s -m yum -a “name=telnet state=latest”         #Installing package through command line
Step 5: Write another playbook for variable substitution
1
 $ sudo cp vartest.yaml varsubst.yaml
1
$ sudo vi varsubst.yaml
→ Modify the code as below
--- # Testing how variables work in the vars section of a playbook 
- hosts: ‘{{ myhosts}}’
  remote_user: ansible
  become: yes
  become_method: sudo
  connection: ssh
  gather_facts: ‘{{ gather }}’
  tasks: 
  - name: Install the indicated software
     yum:
       name: ‘{{ pkg }}’
       state: latest
→ Save and Close.
Step 6: Install package through variable substitution
1
 $ ansible-playbook varsubst.yaml --extra-vars ‘myhosts=hostname(group-name) gather=yes pkg=telnet’
Step 7: Delete the installed package on node 
1
$ ansible hostname(or group-name) -s -m yum -a “name=telnet state=absent”

Debug Statement

1
Task: How to use debug method in playbook
Solution:
Step 1: Write a playbook to install a package with debug method 
1
 $ sudo vi debug.yaml
→ Write the following code
--- # Playbook demo of debug and register statement 
- hosts: hostname(or group-name)
  remote_user: ansible
  become: yes
  become_method: sudo
  connection: ssh
  gather_facts: no
  tasks: 
  - name: Installing telnet package 
    yum:
      name: telnet
      state: latest
→ Save and Close.
Step 2: Run Playbook
1
 $ ansible-playbook debug.yaml
Step 3: Go to the respective node to check whether the package is installed or not
1
2
$ telnet
telnet&gt; quit
Step 4: Delete the package
1
 $ ansible hostname(or group-name) -s -m yum -a “name=telnet state=absent”
Step 5: Modify the playbook as below
1
 $ sudo vi debug.yaml
--- # Playbook demo of debug and register statement 
- hosts: hostname(or group-name)
  remote_user: ansible
  become: yes
  become_method: sudo
  connection: ssh
  gather_facts: no
  tasks: 
  - name: Installing telnet package 
    yum:
      name: telnet
      state: latest
      register: result
   - debug: var=result
→ Save and Close
Step 6: Run Playbook
1
$ ansible-playbook debug.yaml
Step 7: Delete the installed package on node 
1
$ ansible hostname(or group-name) -s -m yum -a “name=telnet state=absent”

Notifications and Handlers

1
Task: How to write Notifications and Handling events in a playbook
Solution:
Step 1: create a playbook
1
 $ sudo vi notify.yaml
--- # Notificaton and Handling events
- hosts: hostname(or group-name)
  remote_user: ansible
  become: yes
  become_method: sudo
  connection: ssh
  gather_facts: yes
  tasks: 
  - name: install http server 
     yum:
       name: httpd
       state: latest
     - name: enable and start the http service
        service: 
          name: httpd
          enabled: yes
          state: restarted
→ Save and Close
Step 2: Run Playbook
1
 $ ansible-playbook notify.yaml
Step 3: Goto respective node to check the package is installed or not
1
$ sudo systemctl status httpd
Step 4: Delete the installed package on node 
1
$ ansible hostname(or group-name) -s -m yum -a “name=telnet state=absent”
Step 5: Add the notifier in the playbook
1
 $ sudo vi notify.yaml
--- # Notificaton and Handling events
- hosts: hostname(or group-name)
  remote_user: ansible
  become: yes
  become_method: sudo
  connection: ssh
  gather_facts: yes
  tasks: 
  - name: install nginx web server 
    yum:
      name: nginx
      state: latest
      notify:
      - enable and start the nginx service
  handlers: 
  - name: enable and start the nginx service
    service: 
      name: nginx
      enabled: yes
      state: restarted
→ Save and Close
Step 6: Run the modified playbook.
1
 $ ansible-playbook notify.yaml
→ Observe the difference while executing the command
Step 7: Remove the installed package
1
 $ ansible hostname(or group-name) -s -m yum -a “name=httpd state=absent”
Step 8: Goto respective node to check the package is installed or not
1
$ sudo systemmctl status httpd
→ It shows not running

No comments:

Post a Comment