Difference between revisions of "Ansible-inventory"

From wikieduonline
Jump to navigation Jump to search
 
(18 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
{{lowercase}}
 +
<code>[[ansible-inventory --list]]</code>
 +
 +
== Basic operations with your inventory ==
 +
* List managed hosts:
 +
:: <code>[[ansible all --list-hosts]]</code>
 +
:: <code> ansible YOUR_GROUP --list-hosts</code>
 +
:: <code> [[ansible-inventory --graph]]</code>
 +
:: <code> [[ansible-inventory --list]] </code>
 +
:: To filter just one group of host: <code>ansible-inventory --list | jq '.["YOUR_GROUP_NAME"]' </code>
 +
 +
* List defined groups
 +
::<code>ansible localhost -m debug -a 'var=groups.keys()'</code>
 +
::<code>ansible localhost -m debug -a 'var=groups'</code>
 +
 
== Inventory of managed nodes ==
 
== Inventory of managed nodes ==
 
https://www.digitalocean.com/community/tutorials/how-to-manage-multistage-environments-with-ansible
 
https://www.digitalocean.com/community/tutorials/how-to-manage-multistage-environments-with-ansible
Inventory is defined in <code>/etc/ansible/hosts</code> file. It allows you to define your managed hosts by hostname or IP address, and group them, such as "my_webservers_group" in our example in INI format.  
+
Inventory is defined in <code>[[/etc/ansible/hosts]]</code> file. It allows you to define your managed hosts by hostname or IP address, and group them, such as "my_webservers_group" in our example in INI format.  
  
 
Groups of groups, hierarchies, is also supported using (:children) keyword: <code>[YOUR_NEW_GROUP_OF_GROUPS:children]</code> <ref>https://www.digitalocean.com/community/tutorials/how-to-manage-multistage-environments-with-ansible</ref>
 
Groups of groups, hierarchies, is also supported using (:children) keyword: <code>[YOUR_NEW_GROUP_OF_GROUPS:children]</code> <ref>https://www.digitalocean.com/community/tutorials/how-to-manage-multistage-environments-with-ansible</ref>
  
<source lang="yaml">
+
<pre>
 
#This is a example of a host configuration file. You can use # to include your comments on hosts file
 
#This is a example of a host configuration file. You can use # to include your comments on hosts file
  
Line 27: Line 42:
 
my_jumper_server_alias ansible_host=192.0.2.50 ansible_port=5555  
 
my_jumper_server_alias ansible_host=192.0.2.50 ansible_port=5555  
  
</source>
+
</pre>
  
 
You can also read Ansible best practices<ref>http://docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html</ref>
 
You can also read Ansible best practices<ref>http://docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html</ref>
  
'''Basic operations with your inventory:'''
+
== Activities ==
* List managed hosts:
+
* Read about Ansible dynamic inventory https://docs.ansible.com/ansible/latest/user_guide/intro_dynamic_inventory.html
:: <code> ansible all --list-hosts</code>
 
:: <code> ansible YOUR_GROUP --list-hosts</code>
 
:: <code> ansible-inventory --graph </code>
 
:: <code> ansible-inventory --list </code>
 
:: To filter just one group of host: <code>ansible-inventory --list | jq '.["YOUR_GROUP_NAME"]' </code>
 
  
* List defined groups
+
== Help ==
::<code>ansible localhost -m debug -a 'var=groups.keys()'</code>
+
* <code>[[ansible-inventory --help]]</code>
::<code>ansible localhost -m debug -a 'var=groups'</code>
 
  
 +
== Related ==
 +
* <code>[[/etc/ansible/hosts]]</code>
 +
* <code>[[ansible-playbook -i]]</code>
  
 
== See also ==
 
== See also ==
 +
* {{ansible-inventory}}
 
* {{ansible}}
 
* {{ansible}}
  
  
 
[[Category:Ansible]]
 
[[Category:Ansible]]

Latest revision as of 08:58, 19 August 2022

ansible-inventory --list

Basic operations with your inventory[edit]

  • List managed hosts:
ansible all --list-hosts
ansible YOUR_GROUP --list-hosts
ansible-inventory --graph
ansible-inventory --list
To filter just one group of host: ansible-inventory --list | jq '.["YOUR_GROUP_NAME"]'
  • List defined groups
ansible localhost -m debug -a 'var=groups.keys()'
ansible localhost -m debug -a 'var=groups'

Inventory of managed nodes[edit]

https://www.digitalocean.com/community/tutorials/how-to-manage-multistage-environments-with-ansible Inventory is defined in /etc/ansible/hosts file. It allows you to define your managed hosts by hostname or IP address, and group them, such as "my_webservers_group" in our example in INI format.

Groups of groups, hierarchies, is also supported using (:children) keyword: [YOUR_NEW_GROUP_OF_GROUPS:children] [1]

#This is a example of a host configuration file. You can use # to include your comments on hosts file

foo_server.example.com
192.168.6.1
bar_server.example.com

[my_webservers_group]
foo5.example.com
bar6.example.com

[my_dbservers_group]
onedb1.example.com
twodb.example.com

#Example of a server alias on standard Ansible port
my_local_defined_hostname ansible_host=192.0.2.50

#Example of a server alias on a non standard Ansible port

my_jumper_server_alias ansible_host=192.0.2.50 ansible_port=5555 

You can also read Ansible best practices[2]

Activities[edit]

Help[edit]

Related[edit]

See also[edit]

  • https://www.digitalocean.com/community/tutorials/how-to-manage-multistage-environments-with-ansible
  • http://docs.ansible.com/ansible/latest/user_guide/playbooks_best_practices.html
  • Advertising: