Wednesday, November 7, 2018

Creating our Inventory File

We can have below options in an Inventory File.
-Add Behavioral Parameters
-Create host-based variables
-Create a Group
-Create group-based variables


##Inventory file with per host parameters and a group.

[db]
db1.company.com ansible_ssh_user=aaron ansible_ssh_pass=1123
#ansible_user and ansible_password can also be used.
db2.company.com ansible_python_interpreter=/usr/bin/python

[datacenter-west:children]
db

#the :children means that the entries below should be treated as groups instead of machines.

#variables based on a group or system.
[datacenter-west:var]
ansible_ssh_user=ansible_user
ansible_ssh_pass=#45e!@Gh
ntp-server=5.6.7.8


 
 
If we want to provide private key in the inventory file instead of password:
 
ansible_ssh_private_key_file=uday.pem
 
Reference

Let's create a sample for our lab environment.

web1 i am just keeping alias for my system, not the machinename.
the parameters to the machine are specified beside the machine instead of passing from command line.So, we don't need to pass in the ansible command.


web1 ansible_ssh_host=192.168.33.20 ansible_ssh_user=vagrant ansible_ssh_pass=vagrant
 

The ansible command:
ansible web1 -i inventory -m ping

We can change the above inventory file to add it to a group.
ansible webservers -i inventory -m ping


web1 ansible_ssh_host=192.168.33.20 ansible_ssh_user=vagrant ansible_ssh_pass=vagrant
db1 ansible_ssh_host=192.168.33.30 ansible_ssh_user=vagrant ansible_ssh_pass=vagrant

[webservers]
web1
db1

We can create multiple groups to separate the data and functionality between machines.And also sub groups or parent groups.

ansible datacenter -i inventory -m ping


web1 ansible_ssh_host=192.168.33.20 ansible_ssh_user=vagrant ansible_ssh_pass=vagrant
db1 ansible_ssh_host=192.168.33.30 ansible_ssh_user=vagrant ansible_ssh_pass=vagrant

[webservers]
web1
 
[dbservers] 
db1
 
[datacenter:children]
webservers
dbservers

We can assign variables values to single group directly instead of defining for each machine.

We can pass parameters to groups instead of passing for each machine, if they are common.

ansible datacenter -i inventory -m ping


web1 ansible_ssh_host=192.168.33.20
db1 ansible_ssh_host=192.168.33.30

[webservers]
web1
 
[dbservers] 
db1
 
[datacenter:children]
webservers
dbservers
 
[datacenter:vars]
ansible_ssh_user=vagrant
ansible_ssh_pass=vagrant 
AWS Sample I executed with: ansible datacenter -i inventory -m ping


web1 ansible_ssh_host=10.10.4.7
db1 ansible_ssh_host=10.10.4.116

[webservers]
web1

[dbservers]
db1

[datacenter:children]
webservers
dbservers

[datacenter:vars]
ansible_ssh_user=ec2-user
ansible_ssh_private_key_file=uday.pem

No comments:

Post a Comment