Roles Flashcards

1
Q

What is the list of files and folders pertaining to a role called database?

A
database/defaults/main.yml
/database/vars/main.yml
/database/handlers/main.yml
/database/files
/database/templates
/database/tasks/main.yml
/database/meta/main.yml
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do I change the location of systemwide roles?

A

In ansible.cfg, change the defaulst.
[defaults]
roles_path=~/ansible/system_roles

This can also be done by setting env variable ANSIBLE_ROLES_APTH

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Do ansible have a notion of importing or including other playbooks?

A

Yes. Use include to include tasks and plays.

include: django.yml

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

A variable proxy_hostname is defined in role database. I sit visible in a different role web?

A

Yes. Ansible has no notion of namespace across roles. A variable defined in one role will be visible everywhere else.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the difference between tasks in a playbook and tasks in a role?

A

In a role, default location for files is /rolename/files or /rolename/templates for templates.

In a normal playbook the default location is inventory location.

This has an impact on copy and template modules.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is ansible-galaxy?

A

A command line tool that is used to find and download roles published by the community.

It can also be used to generate the skeleton for creating a role.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do use galaxy to create a role scaffolding?

A

ansible-galaxy init -p playbooks/roles web

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you declare role dependency?

A

dependencies:

  • { role: ntp, ntp_server=ntp.ubuntu.com}
  • { role: memcached}
  • { role: activemq}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is Ansible Galaxy?

A

An open source repo of community contributed ansible roles.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Install ntp role bennojoy from galaxy

A

ansible-galaxy install -p ./roles bennojoy.ntp

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Where does ansible galaxy install roles by default?

A

In system wide roles location

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

List installed roles with galaxy

A

ansible-galaxy list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Uninstall a role with galaxy

A

ansible-galaxy remove bennojoy.ntp

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you change the way ansible detect if a task has changed a state or failed?

A

use change_when and failed_when

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is ansible behavior when a task failed and how do you change this behavior?

A
  • Ansible stops the processing when a task failed

- To change this behavior use failed_when: False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Explain following task:

- copy: src=/src/main/java/Job.java /src/main/java/Job.java
  failed_when: False
  register: result
  debug: var=result
  fail:
A
  • Attempt to copy file Job1.java into Job.java
  • If the task fails, do not stop
  • register the result in the variable called result
  • log the result
  • stop the playbook execution
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Ansible ad-hoc command to delete a prostgres db called players

A
  • ansible –become –become-user postgres -m postgresql_db -a “name=players state=absent”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Where can I use filters in Ansible?

A

Inside {{}} and inside templates

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is the similitude between filters and pipes?

A

Using filters resembles using linux pipes. A variable is pipled through a filter.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Give an example of default filter and explain

A

“HOST”: “{{ database_host | default(‘localhost’) }}”

Expression evaluates to database_host if it is defined; otherwise it evaluates to ‘localhost’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Examples of variable filters

A

failed, changed, success, skipped

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Examples of file filters

A

basename, dirname, expanduser, realpath

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Example that shows the usage of basename filter

A

vars:
home: /usr/share/nginx/html/index.html
tasks:
- copy: src=files/index.html dest= {{ home }}

With basename filter:

vars:
home: /usr/share/nginx/html/index.html
tasks:
- copy: src=files/{{ home | basename }} dest= {{ home }}

24
Q

Explain the join filter

A

join a list of strings with a given delimiter

HOSTS: “{{ domains | join(‘,’)}}”

25
Q

Where does ansible look for custom filters?

A

in folder filter_plugins, relatively to the directory containing playbooks

or location can be defined using env variable ANSIBILE_FILTER_PLUGINS

26
Q

What are lookups?

A

An ansible mechanism to to read configuration data from various sources so that this data can be used in playbooks or templates.

27
Q

Give example of lookups

A

file, password, pipe, env, template, csvfile, redis_kv, etcd

28
Q

Provide lookup command to log SSH key from /Users/paul/.ssh/id_rsa.pub into a variable.

A

debug: var={{ lookup(‘file’,’/Users/paul/.ssh/id_rsa.pub’) }}

29
Q

What is goal of the pipe lookup?

A

Evaluate an external program on the control machine. Such as checking the version of git source base.

debug: msg=”{{lookup(‘pipe’, ‘git rev-parse HEAD’) }}”

30
Q

What is goal of the env lookup?

A

Retrieves the value of an environment variable on the control machine.

debug: msg=”{{lookup(‘env’, ‘SHELL’) }}”

31
Q

What is goal of the password lookup?

A

Generate a random password and ALSO write it to the specifed file.

-name: create deploy user
postgresql_user:
user: deploy
password: “{{ lookup(‘password’,’deploy-password.txt’)}}”

32
Q

What is goal of the template lookup?

A

Evaluate a jinja2 template and return the result.

denug: msg=”{{ lookup(‘template’,’message.j2’)}}”

33
Q

What is goal of the csvfile lookup?

A

Read an entry from csv file

debug msg=”{{ lookup(‘csffile’,’Paul file=file.csv delimiter=, col=1’) }}”

col=the column to retrieve
Paul=the row to retrieve; must appear exactly once in column 0.

34
Q

What is goal of the redis lookup?

A

Read an entry from redis

debug: msg=”{{ lookup(‘redix_kv’, ‘redis://host:port, my_key’) }}”

35
Q

What is goal of the etcd lookup?

A

Read entry from etcd”

debug: msg=”{{ lookup(‘etcd’,’my_key’)}}”

etcd is defined in ANSIBLE_ETCD_URL

36
Q

How do you loop in Ansible?

A

Use: with_items, with_lines, with_dict, with_fileglob, with_flattened, with_first_found, etc.

37
Q

Explain with_lines

A

Run a command on the control machine and loop over its result, line by line.

  • name: log the content of a file line by line
    debug: msg=”{{ lookup(‘file’,item)}}”
    with_lines:
    - cat /files/turing.txt
38
Q

Explain with_fileglob

A

Iterates over a set of files on the control machine

  • name: log the content of each file in directory
    debug: msg=”{{ lookup(‘file’,item)}}”
    with_lines:
    - /files/.txt
    - /var/logs/
    log
39
Q

Explain with_dict

A

iterate over a dictionary

-name: iterate over dict
debug: msg=”{{ item.key}}={{ item.value }}”
with_dict: {{ ansible_eth0.ip4 }}

40
Q

What is the relationship between lookups and loops?

A

Loops are lookup plugins with name starting with “with_” and that return a list.

41
Q

How do you change the default looping variable?

A

Use loop_var construct.

- debug: msg= {{user.name}}
  with_items:
        - {name: gil}
        - {name: sarina}
        - {name: leanne}
  loop_control:
        loop_var: user
42
Q

Explain the benefit of renaming the loop control variable?

A

Avoid conflict in cases where some playbook fragments are included that use the same loop control variables.

43
Q

Explain the label instruction in loop control?

A

Helps users control how data should be displayed during loop execution.

44
Q

Explain no_log

A

Allows hiding password from logs.

45
Q

Explain include

A

Allow you to include tasks or playbooks. Often used in roles.

46
Q

What is dynamic include?

A

Dynamic include is a mechanism that allows you to dynamically evaluate the name of the file to include.

  • include: {{ ansible_os_family }}.yml
47
Q

What can be a drawback of dynamic include?

A

ansible-playbook –list-tasks might not be able to determine all tasks if it cannot evaluate all variables.

Example: fact variables are not populated when using this command.

48
Q

What is the difference between include sand include_role?

A

include includes the complete content of a file

include_role sllows selectively choosing what parts to include and where in the play to include it. include_role makes the handle available as well.

49
Q

What is conditional include and what is the syntax?

A

include a file only when a condition is met:

  • include: Debian.yml
    when: “ansible_os_family==’Debian’”
50
Q

How can I use a block to make my code more compact?

A

Use a block to group tasks and provide a common conditions or arguments.

  • block:
    • package:
      name: nginx
  • service:
    name: nginx
    state: started
    enabled: yes
    become: yes
    when: “ansible_os_family == ‘Redhat’”
51
Q

What is ansible default error handling behavior?

A

take a host out of play if a task fails and continue as long as there are hosts remaining that haven’t encountered errors.

52
Q

What clauses does ansible provide to give you more control on error handling?

A

serial, max_fail_percentage, block-rescue-always

53
Q

what is ansible-vault

A

A command line tool that allows you to encrypt credentials so ansible can use them.
It creates encrypted file that ansible-playbook can recognize and decrypt automatically given the password.

54
Q

What are ansible valut commands?

A
create
encrypt
edit
decrypt
rekey
view
55
Q

How to tell ansible to prompt for password that it will use to decrypt the secret files?

A

ansible-playbook playbook.ym –ask-vault-pass

56
Q

Where can I specify the location of the encrypted password file?

A

Use vars_files to specify the file or provide the file when launching the playbook using –vault-password-file.