Section 8 Flashcards

1
Q

What is a template?

A

A configuration file that contains variables and is generated on managed hosts with the variables interpreted.

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

What are the four jinja 2 Template elements?

A

data = sample text

comment = {# comment #}

vairable = {{ ansible_facts[‘this’][‘that’] }

expression = {% for myhost in groups[‘web’]%}
{{ myhost }}
{% endfor % }

The above is a for loop

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

This file is managed by Ansible

Create a jinja2 file that says the below and then send it to the server you want with permissions you assign to it

You are part of a group with the following members:
member=ansible1:8080
member=ansible2:8080
member=ansible3:8080

A

Add the ansible_managed variable to you ansible.cfg

{{ ansible_managed }}
{% for host in groups[‘all’] %}
member={{ host }}:8080
{% endfor %}

vi test.yml
template:
src: jinja.j2
dest: /tmp/jinja
owner: root
group: root
mode: 0777

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

Create a jinja2 template that prints either one of two things on a web server:
if it’s apache2:
Welcome to Apache2
if its anything else
Welcome to httpd

Use a vairable in your playbook called apache_package that specifies where or not it is apache2 or httpd

A

vars:
- apache_package: httpd
tasks:
- template
src: jinja.j2
dest: /tmp/httpd.conf

jinja file
{% if kapache_package == ‘apache2’ %}
welcome to apache2
{% else %}
welcome to httpd
{% endif %}

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

In terms of Jinja2, how do you convert your variable to json, yaml, or check if it contains an ip address

A

{{ myvar | to_json }}
{{ myvar | to_yaml }}
{{ myvar | ipaddr }}

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

What are the different role directories and what do they do?

A

defaults - default variables that ma y be overwritten by other variables

files - static files that are needed by role tasks

handlers - handlers for use in this role

meta - metadata, like dependencies, plus license and maintainer info

tasks - Role task definitions

templates

tests - optional inventory and test.yml file to test role

vars - variables that are not meant to be overwritten

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

Where can you store roles?

A

./roles - store roles in current project directory. This location has the highest precedence

/.ansible/roles - exists in the current user home dir and makes the role available to the current user only. Second highest precedence

/etc/ansible/roles - location where roles stored to make them accessible to any user.

/usr/share/ansible/roles - Location where roles are stored after they are installed from rpm files. Lowest precedence

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

Create custom role directory structure

A

ansible-galaxy init rolename

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

How do you add roles to a playbook

A

roles:
- role1
- role2

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

What goes first, roles or tasks?

A

Roles, unless otherwise specified

pre_tasks:
post_tasks: - run tasks after roles but also after tasks specified in the playbook as well as the handlers they call.

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

Create a role directory use a role and run it

A

Take example from pg 212

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

If you have the same role dependencies on two different roles, how many times is the dependent role executed?

A

Only once

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

How do you add role dependencies?

Create one dependency that changes one of the default variables and another dependency that only runs when the server is in production

A

vi httin the meta/main.yml
dependencies:
- role: apache
vars:
apache_port: 80
- role: mariadb
when: environment == ‘production’

(This will look for the environment variable in the playbook vars, roles, etc. Basically anywhere that has anything to do with the play)

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

What should you call the main playbook for your project directory?

A

site.yml

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

How do you get to the ansible galaxy site?

A

galaxy.ansible.com

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

Perform a command line ansible-galaxy search
os should be redhat
author
tags

get more info about the role

A

ansible-galaxy search –platforms redhat
–author me
–galaxy-tags httpd

ansible-galaxy info this.role

17
Q

Where do ansible-galaxy roles usually install?

How do you change this to /home/ansible/roles?

A

~/.ansible/roles

ansible-galaxy install this.role -p /home/ansible/roles

18
Q

Create a requirements file and install it

A

vi roles/requirements.yml

  • name: geerlingguy.apache
    src: https://github.com/geerlingguy/ansible-role-apache
    scm: git
    version: v2.0.0
  • name: geerlingguy.nginx
    version: “2.7.0”
    ansible-galaxy install -r requirements.yml