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.
Explain local facts?
facts that are defined on a speicific machine by its owner or operator. This is different from standard facts such as memory and disk info that are collected by Ansible.
How do you define local facts?
Define a file in ini format and store it under /etc/ansible/facts.d
How do you display all tasks of a playbook?
ansible-playbook –list-tasks playbook.yml
Can an ansible variable reference another variable?
Yes, definitely. Example:
venv_path: “{{ venv_home }}/{{ proj_name }}”
What is the difference in the way the apt module and the pip modules handle list?
apt module will results in once single execution of apt with the list of packages passed as argument
pip will be called many times, once for each package in the list.
We want to execute a task as root. what instruction do we use to facilitate this in the playbook?
- name: example of task that escalate priviledges
apt : pkg=nginx update_cache=yes
become: True