Variables and Facts Flashcards
How do you define variables in playbook?
vars:
key_file: /usr/local/nginx.key
server_name: localohost
How do you define variables in playbook using a file?
vars_files:
- nginx.yml
How to log out the content of a variable?
- debug: var=variable name
Put the result of executing a command in a variable
- name: capture current user into a varianle
command: whoami
register: login
How do you prevent ansible from stoping execution of a task after encountering an error?
Add the flag ignore_errors: True to the task definition.
What is the type of value received when using register clause?
dictionary. Though the list of fields depends on each module.
How do you know what are the fields of the dictionary returned by the register clause?
Log the result and review the fields. Official documentation of modules often does not include the list of fields.
- name: log a registered variable
command: whoami
register: login
debug: var=login
Assume I use the register clause with the command module. The result populated by register is a dictionary. What field of this dictionary contains the true result of the command?
stdout
If a variable “user” contains a dictionary, how do you access a key password of this dictionary?
{{ user.password }}
{{ user[‘password’] }}
In what case can you use {{ user[‘name’] }} where you cannot use {{ user.name }}?
if the key of the dictionary contained in the variable ‘user’ has spaces. for instance {{ user[‘firstname and lastname’] }}
What are facts?
Information Ansible gathers on the remote machine before running the playbook.
Example of facts
CPU Architecture, Operating System, memory, disk info
What is the ad-hoc command for retrieving facts?
ansible server -m setup
What is the ad-hoc command for retrieving facts starting with ansible_eth?
ansible server -m setup -a “filter=ansible_eth*”
Explain this: {{ ansible_local.example.book.title }}
This returns a fact named title defined under the group named book in a file named example.fact located at /etc/ansible/facts.d on one of the managed hosts.