CyberKeeda In Social Media

How to add Hosts and Host groups into Ansible Server



Ansible Initial Host Configuration.

Ansible knows the hosts and hostgroups from a host file by default it is located as /etc/ansible/hosts.



[root@cyberkeeda ~]# vim /etc/ansible/hosts 


One can find many default examples of hosts and hostgroups, some part of it has been pasted below.

# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
#   - Comments begin with the '#' character
#   - Blank lines are ignored
#   - Groups of hosts are delimited by [header] elements
#   - You can enter hostnames or ip addresses
#   - A hostname/ip can be a member of multiple groups

# Ex 1: Ungrouped hosts, specify before any group headers.
## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10

# Ex 2: A collection of hosts belonging to the 'webservers' group

## [webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100



So our server inventory is something like below.

Hostname
IP
webserver01.cyberkeeda.com
192.168.0.101
webserver02.cyberkeeda.com
192.168.0.102
webserver03.cyberkeeda.com
192.168.0.103

Lets assume a scenario, we have to inatall 3 new Apache httpd servers with help of ansible.

For thios first requirement is to make an entry of hostgroup into ansible’s host file.

Hence we will create a hostgroup named as webservers and append all above three servers into it.



[root@cyberkeeda ~]# vim /etc/ansible/hosts


And align the below lines

[webserver]  : 

You can use any hostgroup name as per your requirement, choose wisely as going further you will call this hostgroup and all hosts will be considered within it.

Then append all server ip or hostname after hostgroup.
Final entry will look like below one.

[webservers]
webserver01.cyberkeeda.com
webserver02.cyberkeeda.com
webserver03.cyberkeeda.com


Check if it’s working or not with Some basic ansible commands, some are enlisted below
    

[root@cyberkeeda ~]#  ansible -m ping webservers


You can also specify an individual host:

    
[root@cyberkeeda ~]#  ansible -m ping webserver02.cyberkeeda.com

You can specify multiple hosts by separating them with colons:

   
 [root@cyberkeeda ~]# ansible -m ping host1:host2

The shell module lets us send a terminal command to the remote host and retrieve the results. For instance, to find out the memory usage on our host1 machine, we could use:


 [root@cyberkeeda ~]# ansible -m shell -a 'free -m' host1



Have a look on the video too.



No comments:

Post a Comment

Designed By Jackuna