DevOps Classroomnotes 07/Jan/2023


  • 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
  • This falls under category all
  • We can add specific groups


  • 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





  • Adding variables to hosts in inventory

[ubuntu] name=node1
localhost name=acn

[centos] name=node2

  • let print the variable values using playbook
- name: test inventory
  hosts: ubuntu
    - name: print my variable
        var: name
  • Refer Here for the changes and see below for execution report

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

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
  • 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
  • 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
  • Refer Here for usage of stats and skipping un-necessary extraction of tomcat
  • As we need to change permission of shell files only when tomcat is extracted, using the same condition Refer Here for the changeset

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 | 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
} | 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

Leave a Reply

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

About continuous learner

devops & cloud enthusiastic learner