Wednesday, December 30, 2020

Ansible - Dynamic Inventory

 Ansible - Dynamic Inventory - 12/30/2020



CM -> Controller Node -> PB - code ----> Managed Nodes

IP add into inventory

We have been manually updating the inventory with new IP address.

This process is called static inventory.

There are cases, that you don't know the ip of the target node. You have to login to target node and check the IP.
or upon reboot, system's IP get changed.

We are on dynamic world. We may bring a server for testing purpose and after that we shutdown.
After a while we may bring the server and IP get changes.

Say your env has thousands of servers.
or you have server is build on could - can be aws, google, azure or any other source or locally..
You have new IPs or your instance is on docker/container.

OS can be from any source and IP keep changing..
 - VM
 - Cloud -> AWS, GCP, Azure
 - Containers

We want such a mechanism, where we will configure playbook on certain context.

We can make our inventory little intellegent or call it dynamic.
What that mean?
- we will not write IP manually on inventory file.
- Since we don't know, and can't add it to inventory.

run playbook or ad-hock commands and
scan -> new IPs

what info do you provide?
- need ssh/IP for linux hosts..


we will have a playbook which goes out to aws
1. instal os (EC2) - provision a server

2. Configure webserver

in playbook, you have to define as,
- hosts: ip
  tasks
    - configure web server

You can only run this only if you know the IP.

if you know ip, you have to add this ip to inventory on control node.


# ansible all --list-hosts

never use IP in the playbook.
rather use group name.


os1 - 1.2.3.4
os2 - 1.2.3.5
os3 - 1.2.3.6

horizontal scaling -> adding more hosts


----------------------------
inventory
# cat /etc/ansible/ansible.cfg
# more /root/myhosts
create a single file, and update the config file.

You may be using multiple inventory files with different app, subnet or any other purpose.



# anlsible all --list-hosts

extension can be .py,yaml or no extention.

[root@master mydb]# cat >a
1.1.1.1
[root@master mydb]# cat >b
2.2.2.2
[root@master mydb]# cat >c
3.3.3.3

[root@master mydb]# ls
a  b  c


Update the inventory file to point the directory.

# vi /etc/ansible/ansible.cfg


Since ansible accept .py extention, we can write python code as well..

you can use scanning tool 'nmap' to scan


# ansible all --list-hosts

[root@master mydb]# cat my.py
#!/usr/bin/python3
print("5.5.5.5")


the display is not proper. IP comes but with print
but if you ask manually it displays properly.


You have to follow certain format.


check Bimal Daga's github
github.com/vimallinuxworld12/ansible_dynamic_inventory/master/hosts.py


download it:
# weget <download URL>


hosts.py get it from bimal's page
# cp hosts.py mydb
# chmod +x hosts.py
# python3 hosts.py --list


in exam, they give you pre-created file and need to copy it and run from there.

now, you can run ansible all --list-hosts
# ansible all --list-hosts

ansible gives you on good format that ansible understands it.


get another URl from ansible github link
http://github.con/ansible/treee/stable-2.9/controlb/inventory

There is a script just download and use it.

download the ec2.py file
# chmod +x ec2.py
Run manually
# python2 ec2.py

You need to install library called boto if you don't have it

# pip3 list
# pip3 install boto
# python2 3c2.py -> it failed again...

# python3 ec2.py --list

it mght be because of lower version of python
# ./ec2.py --list

stil a problem.

# vi ec2.py and change the path pytohon path.
#!/usr/bin/python3



Go to aws dashboard


you have to specify
1. region info
2. API
3. Login and pw info


# vi ec2.py

update the code with region / pw

you can create a variable and you done...


On dashboard
- IAM -> create user with power : poweruseraccount

click, click and click ....

record your access key and
you can use export variable

export AWS_ACCESS_KEY_ID='AJDHSJHDSHDDSDD'
export AWS_SECRET_ACCESS_Key='dsfsdfsdfsdfsdfsdfsd'
export AWS_REGION='US-EAST-1a'

# ansible all --list-hosts



-------------------------
launch an aws instance manualy
Tag:
name: mywebos
country: Nepal
DadaCenter:    Virginia


There is another file ec2.ini

download it as well.


giving error again.. error on 172 line.
go ahead and commentout the line and run it again

# ansible all --list-hosts


always tag the os on the cloud

Key        value
Country        US
DC        dc2
Tech        web

# ./ec2.py --list

keep ec2.ini file on the same location as ec2.py
Tag is really importand to work with ansible
# ansible tag_Country_US--list-hosts

# ansible tag_Country_IN--list-hosts

Now, in summary,
1. Launch an os using ansible playbook
  tag
2. Configure dynamic inventory

3. Write a playbook to configure a web server on the instance on the cloud.
   hint: use -hosts: tag_country_US

grab all IP and install web server...

ansible-docs -l

No comments:

Post a Comment

Git branch show detached HEAD

  Git branch show detached HEAD 1. List your branch $ git branch * (HEAD detached at f219e03)   00 2. Run re-set hard $ git reset --hard 3. ...