Section 8 Flashcards
What is a template?
A configuration file that contains variables and is generated on managed hosts with the variables interpreted.
What are the four jinja 2 Template elements?
data = sample text
comment = {# comment #}
vairable = {{ ansible_facts[‘this’][‘that’] }
expression = {% for myhost in groups[‘web’]%}
{{ myhost }}
{% endfor % }
The above is a for loop
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
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
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
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 %}
In terms of Jinja2, how do you convert your variable to json, yaml, or check if it contains an ip address
{{ myvar | to_json }}
{{ myvar | to_yaml }}
{{ myvar | ipaddr }}
What are the different role directories and what do they do?
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
Where can you store roles?
./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
Create custom role directory structure
ansible-galaxy init rolename
How do you add roles to a playbook
roles:
- role1
- role2
What goes first, roles or tasks?
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.
Create a role directory use a role and run it
Take example from pg 212
If you have the same role dependencies on two different roles, how many times is the dependent role executed?
Only once
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
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)
What should you call the main playbook for your project directory?
site.yml
How do you get to the ansible galaxy site?
galaxy.ansible.com
Perform a command line ansible-galaxy search
os should be redhat
author
tags
get more info about the role
ansible-galaxy search –platforms redhat
–author me
–galaxy-tags httpd
ansible-galaxy info this.role
Where do ansible-galaxy roles usually install?
How do you change this to /home/ansible/roles?
~/.ansible/roles
ansible-galaxy install this.role -p /home/ansible/roles
Create a requirements file and install it
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