Introduction Flashcards
In what file do you specify information about servers that have to be managed by Ansible?
inventory file
What is the command for running playbooks?
ansible-playbook
What is ansible command for ad-hoc execution of commands?
ansible -i hosts -m ping
In what order does ansible look for config file?
1) ANSIBLE_CONFIG env variable
2) ./ansible.cfg (current directory)
3) ~/.ansible.cfg (home folder)
4) /etc/ansible/ansible.cfg (Installation directory)
(ECHI)
What is the prefered location for ansible config file?
current directory alongside playbooks
Why put ansible config files alonside playbooks?
So they can be version controlled
Example of variables that can be defined in ansible config file
1) Location of inventory file
2) User to SSH
3) SSH private keys
What is the default location of ansible config?
/etc/ansible/ansible.cfg
Command module for check uptime on testserver
ansible testserver -m command -a uptime
Explain: ansible testserver -a uptime
Execute the command module to check uptime of remote server: testserver. -m command is ommitted because the command module is very often used.
When running the command module what is the -b flag used for?
become root and run the command in priviledged mode
What is a playbook?
An ansible configuration management script
What is the very first line of a playbook?
three dash: —
Strings in YAML?
1) “This is a string”
2) This is also a string
Comments in YAML
This is a comment
Booleans in YAML
true, True, TRUE, yes, Yes, YES, on, On, ON, y, Y
false, False, FALSE, no, No, NO, off, Off, OFF, n, N
List in Ansible
- One
- Two
- Three
or
[One, Two, Three]
Dictrionary or Mapping in Ansible
address: 742 Evergreen Terrace
city: Vancouver
state: North Dakota
Define the structure of a playbook
A playbook is a list of plays, which themselves are dictionaries. So a playbook is a list of dictionaries.
Define the structure of a play
A play is a dictionary that must include:
1) hosts to confiigure
2) A list of tasks, which themselves are dictionaries
The play connects hosts to tasks.
Additional common settings of a play?
1) become
2) name
3) vars
Sytructure of a task
1) Name
2) module : arguments (e.g. apt: name=nginx update_cache=yes)
what are modules?
scripts that come with ansible and execute some sort of actions on the managed hosts
Example of modules
apt, copy, file, template
How do you get documentation on a module such as “copy”?
ansible-doc copy
Sum up the structure of a playbook?
1) A playbook is a list of plays
2) A plays ties an unordered set of hosts to an ordered list of tasks
3) Each task is associated with exactly one module
How do you reference a variable “var” in a playbook?
{{ braces }}
When do you have to quote variable reference?
When it starts the argument part of a module declaration. For instance: command: {{ braces }} - a foo
Should be replaced with:
command: “{{ braces }} - a foo”
What problems do you encounter if your command argument contain colon?
The colon makes ansible believe that your argument is a dictionary. This is solve by quoting the argument again.
However, in the case of debug module double quote is needed.
- name: show a debug message
debug: “msg=’This is a message: eh?’”
What is a handler?
A handler is a conditional structure similar to a task. It is triggered only if it has been notified by a task.
How does a task notify a handler?
It includes the notify instruction along with the name of the handler as argument.
- name: Copy TLS certs
copy: source dest
notify: restart nginx
How many times can a handler run in the execution of a playbook and when?
A handler runs only once after all the tasks are run at the end of the play. Even if the handler is notified multiple times, it runs only once.
Many handlers are defined in a play. In which order will they be executed?
In the order in which they are defined in the play NOT in the order in which they are notified.
What are typical use cases for handlers?
1) restarting services (more frequent)
2) reboot
Give two orchestration scenarios that are good for Ansible?
1) Upgrading web application behind a load balancer one after the other
2) Making sure DB is started and configured before web app starts
Write a task to install nginx on an Ubuntu node
- name: Install nginx
apt: name=nginx
Why is Ansible push model interesting?
You have full control on when the changes are applied and you do not need to wait for timers to expire.
What is the advantage of idempotency?
It’s safe to run an Ansible playbook multiple times against a server
Write a task to create a user
- name: Create user deploy
user: name=deploy group=web
Example of activity that goes into a pre-handler
Update the apt cache on Ubuntu
What happens when a host is defined in many groups with different set of variables?
The variable set are merged and the precedence rule applies
How do you validate connectivity to a host?
ansible host -m ping
Give example of two modules that are not idempotent
command and shell
Is command module idempotent?
No, command and shell modules are not idempotemt
Is shell module idempotemt?
No, command and shell modules are not idempotemt
What is a module?
encapsulated procedure that is responsible for managing a specific component on a specific platform.
What is battery-included approach?
The fact that Ansible includes tones of modules built-in.