DevOps Classroomnotes 07/Jan/2023

Inventory

  • Ansible allows groups in inventory.
  • Inventory is list of hosts where the playbook has to be executed.
  • Inventory can be written in two formats
    • ini file
    • yaml

Ansible inventory ini file format

  • ini file is a text file
  • The inventory can be written as follows
  • all entries directly
172.31.24.180
172.31.24.181
dev.internal.qt.com
  • This falls under category all
  • We can add specific groups
172.31.24.180
localhost

[ubuntu]
172.31.24.180
localhost

[centos]
20.121.55.193
  • In the above file we have two named groups ubuntu and centos and one group all
  • If we have sequence of entries we can mention them in inventory
172.31.24.180
localhost

[ubuntu]
172.31.24.180
localhost

[centos]
20.121.55.193

[webservers]
web[01:10].qt.com

Preview

  • Adding variables to hosts in inventory
172.31.24.180
localhost

[ubuntu]
172.31.24.180 name=node1
localhost name=acn

[centos]
20.121.55.193 name=node2

[webservers]
web[01:10].qt.com
  • let print the variable values using playbook
---
- name: test inventory
  hosts: ubuntu
  tasks:
    - name: print my variable
      ansible.builtin.debug:
        var: name
  • Refer Here for the changes and see below for execution report

Preview
* Refer Here for the changes done to accomadate the group variable
Preview

Lets try to make this playbook work with centos 7

  • Lets solve the different values for ubuntu and centos machines
  • Refer Here for the changes done to install java on centos and ubuntu
  • Refer Here for the fix to solve variable name collision
    Preview
  • Refer Here for the changes to add next few steps
  • Refer Here for the changes done to copy the service file according to distribution using conditionals and execute playbook
    Preview
  • Refer Here for the changes done to reload the tomcat service
  • When we re-execute ansible playbook some modules are executing un-necessarily lets stop that from happening
    Preview
  • Refer Here for usage of stats and skipping un-necessary extraction of tomcat
    Preview
  • As we need to change permission of shell files only when tomcat is extracted, using the same condition Refer Here for the changeset
    Preview

ansible facts

  • ansible can collect information about the node where the playbook is under execution using facts.
  • To collect facts ansible uses setup module Refer Here
  • Lets use adhoc command to filter facts ansible -i hosts -m setup -a "filter=*distribution*" all
172.31.24.180 | SUCCESS => {
    "ansible_facts": {
        "ansible_distribution": "Ubuntu",
        "ansible_distribution_file_parsed": true,
        "ansible_distribution_file_path": "/etc/os-release",
        "ansible_distribution_file_variety": "Debian",
        "ansible_distribution_major_version": "22",
        "ansible_distribution_release": "jammy",
        "ansible_distribution_version": "22.04",
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false
}
20.121.55.193 | SUCCESS => {
    "ansible_facts": {
        "ansible_distribution": "CentOS",
        "ansible_distribution_file_parsed": true,
        "ansible_distribution_file_path": "/etc/redhat-release",
        "ansible_distribution_file_variety": "RedHat",
        "ansible_distribution_major_version": "8",
        "ansible_distribution_release": "NA",
        "ansible_distribution_version": "8.5",
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
}

Adhoc command

  • Adhoc command is a way of running ansible module by constructing command line
  • We use adhoc commands for activities which donot require automation.

Conditionals in Ansible

  • Refer Here for the official ansible docs on conditionals

Published
Categorized as Uncategorized Tagged

By continuous learner

devops & cloud enthusiastic learner

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please turn AdBlock off
Animated Social Media Icons by Acurax Wordpress Development Company

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube