DevOps Classroom Series – 07/Aug/2020

Inventory Parsing

  • Inventory is basic building block of Ansible architecture
  • when we execute ansible or ansible-playbook an inventory must be referenced.
  • Location of inventory can be referenced with -i or –inventory-file argument or ansible will pick up inventory from the path defined in ansible.cfg (/etc/ansible/ansible.cfg) Preview Preview Preview Preview
  • Inventories can be static or dynamic.
  • In enterprise we have different inventory files for different environments like
    • QA
    • Staging
    • Dev
    • Production
ansible-playbook -i staging/hosts deploy-app-service.yaml

ansible-playbook -i qa/hosts deploy-app-service.yaml

ansible-playbook -i dev/hosts deploy-app-service.yaml

ansible-playbook -i production/hosts deploy-app-service.yaml

Static Inventory

  • Static inventory has a static list of hosts (defined by hostnames or ip addresses) in an ini format in file.
  • The following are some of the inventory types which we have used
    • inventory with only host names
    localhost
    
    • inventory with group names and hosts under groups
[centos]
172.31.4.122 

[ubuntu]
172.31.3.192 
* inventory with group names and groups with children and
[centos]
172.31.4.122 

[ubuntu]
172.31.3.192 

[webserver:children]
centos
ubuntu

Preview Preview Preview

Inventory ordering

  • A Play level keyword, order was added in Ansible, which can have following values
    • inventory: This is default option & it means the hosts will be processed as written in inventory file
    • reverse_inventory: The hosts will be processed in the reverse order to what is specified in inventory
    • sorted: The hosts will be processed in alphabetically sorted order name
    • reverse_sorted: The hosts will be processed in reverse alphabetically sorted order name
    • shuffle: The hosts will be processed in a random order
  • Lets create an inventory
localhost
172.31.4.122 
172.31.3.192 
  • Lets create a playbook
---
- name: testing order
  hosts: all
  tasks:
    - name: ping the host
      ping:

  • Now lets change the order
---
- name: testing order
  hosts: all
  order: sorted
  tasks:
    - name: ping the host
      ping:

  • To test this try executing commands with this
ansible-playbook -f 1 -i trailinventory ping.yaml

Inventory variable data

  • Inventories provide more than just system names and groupings. Data necessary for groups and hosts also can be passed along. The data may include
    • Host specific data
    • Group Specific data
    • Behavioral parameters to tune how Ansible interacts
  • Lets create an inventory
localhost   package_name="test"
172.31.4.122 package_name="testing"
172.31.3.192 package_name="not testing"
  • Lets create a playbook with a module called as debug Refer Here
---
- name: testing variables
  hosts: all
  tasks:
    - name: print the variable
      debug:
        var: package_name
  • Now lets execute the playbook Preview
  • Now lets introduce groups and group variables
[local]
localhost

[centos]
172.31.4.122
localhost

[ubuntu]
172.31.3.192

[webserver]
172.31.3.192
172.31.4.122

[all:vars]
package_name="from all"

  • Now execute playbook Preview
  • Now lets change inventory as shown below & re-execute
[local]
localhost

[local:vars]
package_name="from local"

[centos]
172.31.4.122
localhost

[centos:vars]
package_name="from centos"

[ubuntu]
172.31.3.192

[ubuntu:vars]
package_name="from ubuntu"

[webserver]
172.31.3.192
172.31.4.122

[webserver:vars]
package_name="from webserver"

[all:vars]
package_name="from all"

Preview

  • Now lets change the inventory as shown below & execute
[local]
localhost package_name="localhost"

[local:vars]
package_name="from local"

[centos]
172.31.4.122 package_name="172.31.4.122"
localhost

[centos:vars]
package_name="from centos"

[ubuntu]
172.31.3.192 package_name="172.31.3.192"

[ubuntu:vars]
package_name="from ubuntu"

[webserver]
172.31.3.192
172.31.4.122

[webserver:vars]
package_name="from webserver"

[all:vars]
package_name="from all"

Preview

  • Now lets execute the following command
ansible-playbook -i inv4 -e "package_name=extravars" playbook1.yaml

Preview

  • In ansible when we run playbooks & if the same variable is defined at multiple, ansible has some kind of priority system which we will be learning "Ansible variable precedence"

Next steps

  • Understanding Behavioral parameters
  • Variable files
  • Dynamic inventories
  • Playbook parsing & ordering

Leave a Reply

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

Please turn AdBlock off
Social Media Icons Powered by Acurax Web Design 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