How to create ansible inventory?

How to create ansible inventory file?

Ansible works on multiple “hosts” or machines in your infrastructure at the same time using a list or group of lists known as inventory.

default inventory file is located under path /etc/ansible/hosts.

we can also specify a different inventory file at the command line using the -i option to run any ansible playbook.

How to run ansible playbook using inventory file?

ansible-playbook test.yml -i testing

test.yml ansible playbook is executed based on testing inventory file.

Can we use multiple inverntory files in ansible?

ansible-playbook test.yml -i testing -i live_environment

Example inventory file /etc/ansible/hosts,

[dbservers]
db01.testing.example.com
db02.testing.example.com

[appservers]
app01.live.example.com
app02.live.example.com
app03.live.example.com

headings in brackets are group names.

group names are used to classify the hosts and helps to decide what hosts are controlling at what times and for what purpose.

How to add variables in inventory file?

Connection variables also work well as host variables:

[servers]
localhost      	ansible_connection=local
host1.test.com 	ansible_connection=ssh    	ansible_user=user1
host2.test.om  	ansible_connection=ssh    	ansible_user=user2

If connection variables are common for all the machines mentioned in group, we can assign connection variables as mentioned below,

[servers]
localhost     	 
host1.test.com      	 
host2.test.om 	 
[servers:vars]
ansible_connection=ssh
ansible_user=user1

Lets take simple inventory and see how to use in ansible to run command in all the machines,

[servers]
127.21.34.2
127.22.2.22

[test]
126.34.3.21
[DEPRECATION WARNING]: Distribution centos 8.4 on host 127.21.34.2should use /usr/libexec/platform-python, but is using /usr/bin/python for backward compatibility with
prior Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. This feature will be removed in version 2.12. Deprecation
warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
127.21.34.2 | SUCCESS => {
	"ansible_facts": {
    	"discovered_interpreter_python": "/usr/bin/python"
	},
	"changed": false,
	"ping": "pong"
}
[DEPRECATION WARNING]: Distribution centos 8.4 on host 127.22.2.22 should use /usr/libexec/platform-python, but is using /usr/bin/python for backward compatibility with
prior Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. This feature will be removed in version 2.12. Deprecation
warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
127.22.2.22 | SUCCESS => {
	"ansible_facts": {
    	"discovered_interpreter_python": "/usr/bin/python"
	},
	"changed": false,
	"ping": "pong"
}
[DEPRECATION WARNING]: Distribution centos 8.4 on host 126.34.3.21 should use /usr/libexec/platform-python, but is using /usr/bin/python for backward compatibility with
prior Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. This feature will be removed in version 2.12. Deprecation
warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
126.34.3.21 | SUCCESS => {
	"ansible_facts": {
    	"discovered_interpreter_python": "/usr/bin/python"
	},
	"changed": false,
	"ping": "pong"
}

Lets take simple inventory and see how to use in ansible to run command in servers group machines,

$ ansible -i /etc/ansible/hosts controller -m ping
[DEPRECATION WARNING]: Distribution centos 8.4 on host 127.21.34.2 should use /usr/libexec/platform-python, but is using /usr/bin/python for backward compatibility with
prior Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. This feature will be removed in version 2.12. Deprecation
warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
127.21.34.2 | SUCCESS => {
	"ansible_facts": {
    	"discovered_interpreter_python": "/usr/bin/python"
	},
	"changed": false,
	"ping": "pong"
}
[DEPRECATION WARNING]: Distribution centos 8.4 on host 127.22.2.22 should use /usr/libexec/platform-python, but is using /usr/bin/python for backward compatibility with
prior Ansible releases. A future Ansible release will default to using the discovered platform python for this host. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information. This feature will be removed in version 2.12. Deprecation
warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
127.22.2.22 | SUCCESS => {
	"ansible_facts": {
    	"discovered_interpreter_python": "/usr/bin/python"
	},
	"changed": false,
	"ping": "pong"
}




Python installation

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^