Dear Readers,
In this article,we will see Launch 2 Hosts and Set-Up in Inventory File.
What is Inventory file?
- Ansible works against multiple managed hosts in your infrastructure at the same time, using a list or group of lists is known as the inventory.
- The Ansible inventory file defines the hosts and groups of hosts upon which commands, modules, and tasks in a playbook operate.
- The file can be in one of many formats depending on your Ansible environment and plugins.
- The default location for the inventory file is /etc/ansible/hosts.If necessary, you can also create project-specific inventory files in alternate locations.
- The inventory file can list individual hosts or user-defined groups of hosts.
Steps to Follow
- Launch 2 Hosts “Ansible-Node-1” and “Ansible-Node-2”.
- Create a group and mention Hosts Private IP under the group in the Inventory file “/etc/ansible/hosts”.
- Uncomment two files “inventory = /etc/ansible/hosts” and “sudo_user = root” in the ansible configuration file “/etc/ansible/ansible.cfg”.
1. Launch 2 Hosts “Ansible-Node-1” and “Ansible-Node-2”
Check Availability Zone of your Ansible Machine
we can see the Availability Zone “ap-south-1b” of your Ansible machine.
Note
Ansible machine is in ap-south-1b AZ.
we create 2 nodes at the same time by selecting 2 instances in the same Availability Zone “ap-south-1b”.
we need to open SSH and HTTP ports.
Create 2 Nodes “Ansible-Node-1” and “Ansible-Node-2”
Launch Linux EC2 Instance
Check below link to Launch Linux EC2 Instance.
We can see the 2 nodes “Ansible-Node-1” and “Ansible-Node-” which was created earlier.
Whenever we install ansible By default ,we will get two files “ansible.cfg” and “hosts” in the /etc/ansible” directory.
1 2 |
[root@ip-172-31-6-252 ec2-user]# ls /etc/ansible/ ansible.cfg hosts roles |
2. Create a group and mention Hosts Private IP under the group in the Inventory file “/etc/ansible/hosts”
Copy Private IP of 2 Hosts
Copy Host 1 “Ansible-Node-1” private IP
Copy Host 2 “Ansible-Node-2” private IP
Open the file “/etc/ansible/hosts” and create group “Ktexperts-Nodes-Group” and mention above 2 nodes IP addresses under group.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
[root@ip-172-31-6-252 ec2-user]# vi /etc/ansible/hosts 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. <span style="color: #ff6600;"><strong>[Ktexperts-Group] 172.31.5.201 172.31.1.221</strong></span> ## 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 ## 192.168.1.110 # If you have multiple hosts following a pattern you can specify # them like this: ## www[001:006].example.com # Ex 3: A collection of database servers in the 'dbservers' group ## [dbservers] |
Note
:wq! —- to quit.
Now on wards ansible server push code to 2 nodes but ansible main configuration file is not recognizing the 2 hosts.
So,we need to uncomment two files in the main configuration file “ansible.cfg”
3. Uncomment two files “inventory = /etc/ansible/hosts” and “sudo_user = root” in the ansible configuration file “/etc/ansible/ansible.cfg”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
[root@ip-172-31-6-252 ec2-user]# vi /etc/ansible/ansible.cfg # config file for ansible -- https://ansible.com/ # =============================================== # nearly all parameters can be overridden in ansible-playbook # or with command line flags. ansible will read ANSIBLE_CONFIG, # ansible.cfg in the current working directory, .ansible.cfg in # the home directory or /etc/ansible/ansible.cfg, whichever it # finds first [defaults] # some basic default values... <span style="color: #ff6600;"><strong>inventory = /etc/ansible/hosts</strong></span> #library = /usr/share/my_modules/ #module_utils = /usr/share/my_module_utils/ #remote_tmp = ~/.ansible/tmp #local_tmp = ~/.ansible/tmp #plugin_filters_cfg = /etc/ansible/plugin_filters.yml #forks = 5 #poll_interval = 15 <span style="color: #ff6600;"><strong>sudo_user = root</strong></span> #ask_sudo_pass = True #ask_pass = True #transport = smart #remote_port = 22 #module_lang = C #module_set_locale = False # plays will gather facts by default, which contain information about # the remote system. # # smart - gather by default, but don't regather if already gathered # implicit - gather by default, turn off with gather_facts: False # explicit - do not gather by default, must say gather_facts: True #gathering = implicit # This only affects the gathering done by a play's gather_facts directive, |
Note
:wq! —- to quit.
Now on wards ansible server push code to 2 hosts automatically by running playbooks.
Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates
KTEXPERTS is always active on below social media platforms.
Facebook : https://www.facebook.com/ktexperts/
LinkedIn : https://www.linkedin.com/company/ktexperts/
Twitter : https://twitter.com/ktexpertsadmin
YouTube : https://www.youtube.com/c/ktexperts
Instagram : https://www.instagram.com/knowledgesharingplatform