Inventory Flashcards
Define inventory
The collection of hosts that Ansible manages
Simplest inventory content
ontartio.example.com
pei.example.com
nova-scotia.example.com
One host is always part of your inventory. Which one?
localhost
What is the difference between connecting to locahost and any other machine?
Ansible uses SSH for other hosts. For locahost it does not go through SSH. It uses local connection instead.
List Ansible behavioral inventory parameters
1) ansible_host
2) ansible_port
3) ansible_user
4) ansible_password
5) ansible_connection
6) ansible_private_key_file
7) ansible_shell_type
8) ansible_python_interpreter
9) ansible_*_interpreter
What are behavioral inventory paramaters?
behavioral invetory paramaters are variables that can be used to refine the definition of a host and how ansible should connect to it.
what is the name of the the default group?
all also named *
Ansible command to get dates on all managed nodes
ansible all - a “date”
What is the format of an inventory file?
ini file format
Define a group web with hosts toronto.example.com and vancouver.example.com
[web]
toronto. example.com
vancouver. example.com
I have 3 hosts running on the same 127.0.0.1 IP but different ports. My goal is to configure them. Explain why the following group configuration will not work?
And how to you resolve it?
[myservers]
- 0.0.1: 2000
- 0.0.1: 2001
- 0.0.1: 2002
Ansible inventory can associate only a single host with 127.0.0.1.
To resolve it use aliases
[myservers]
server1 ansible_host=127.0.0.1 ansible_port=2000
server2 ansible_host=127.0.0.1 ansible_port=2001
server3 ansible_host=127.0.0.1 ansible_port=2002
Define a group staging that contains two other groups: web and lb.
[staging:children]
lb
web
Define a group web that includes all servers web1.example.com to web20.example.com
[web]
web[1:20].example.com
Define two variables user and password for a group web
[web:vars]
user=macabo
password=eboubolo
What are possible types of a ansible variable?
Booleans, strings, dictionaries, lists
What are limitations of specifying variables in inventory files?
1) inventory files only support string and booleans as variable values
2) crowded inventory file
How do you define variables to prevent shortcommings of defining them in inventory file?
Define them in YAML files located under folders group_vars or host_vars.
what is the format of a file located under group_vars.
YAML
What are possible parameters of a dynamic inventory script?
- -host=hostname (for showing host details/variables of individual host)
- -list (for listing groups)
what is the aim of the –host parameter of an inventory script?
for showing host details/variables of individual host. Result is a json that shows variables and their values.
what is the aim of the –list parameter of an inventory script?
returns all groups along with their hosts. result is a json object of group names to host list.
How does Ansible retrieves variables for a speicific host, given –list returns list of groups?
–list includes an optimization that includes the meta section. meta is a json object of host to their variables.
What measures are needed to use static and dynamic inventory files with a single playbook?
1) Put all files in the same folder
2) make the dynamic inventory file executables
3) Specify the folder location in ansible configuration file
Write a fragment to configure inventory location in ansible config file
[defaults]
inventory = my_inventory
When are dynamic inventory scripts invoked during the execution of a playbook? What is the implication?
Right before the playbook itself is invoked. the implication is that if the playbook cannot configure hosts that it spins up itsself.
what is add_host used for?
add_hosts is used to add a host to an inventory dynamically during the execution of the playbook. It is different than dynamic inventory that generates all its hosts prior to the playbook being launched.
add_host can be used for instance to bring up a new vagrant machine and configure it within the same script.
Does add_host modify the inventory file?
No. It only add to the runtime inventory and the change is not persisted to a file.
explain group_by
Used to group nodes using some facts. For instance group by distribution. Can also be seen as a way to achieve conditional behaviour.
How do you tell Ansible to ignore files with extension txt when loading dynamic inventories from a folder
add txt to the list of extensions to skip in ansible.cfg
inventory_ignore_extension
What could be the use of empty groups in dynamic inventory file?
Empty group can be used to declare the group if the content is provided by a dynamic inventory script.
Create ansible.cfg that sets the inventory location as /home/paoblo/ansible/ansible.cfg
[defaults]
inventory= /home/paoblo/ansible/ansible.cfg