Ansible meetup-kamen
-
Upload
kamen-tarlov -
Category
Internet
-
view
227 -
download
0
Transcript of Ansible meetup-kamen
The Power of Jinja2 Templates
Kamen Tarlov Middleware Engineer and co-founder
Agenda• About Me
• Introduction to Jinja2 Templates
• Jinja2 IF and Loop Statements
• Jinja2 Template Skeleton
• Ansible and Jinja2 in action
• Questions and Answers
• Middleware Engineer and co-founder
• Experience with MW Technologies and Configuration Management
• Ansible, Puppet ..
• Openstack
About Me
Introduction to Jinja2 templates• Why do we need templates?
• Library for Python and Django
• Jinja2 works with Python 2.6.x, 2.7.x and >= 3.3
• easy_install Jinja2
• pip install Jinja2
• Out of the box with Ansible
• File Extension – j2: sample.j2
• Variables in {{ var }}
• Statements in {% if/for %}
• Optimised performance
Jinja2 Template Skeleton{% for vhost in apache_vhosts %}<VirtualHost *:80> ServerName {{ vhost.servername }} DocumentRoot {{ vhost.documentroot }}{% if vhost.serveradmin is defined %} ServerAdmin {{ vhost.serveradmin }}{% endif %} <Directory "{{ vhost.documentroot }}"> AllowOverride All Order allow,deny Allow from all </Directory></VirtualHost>{% endfor %}
IF STATEMENT
LOOP
VARIABLE
Variables in Ansible
• Variables defined in configuration file
• Yaml Format
• Flexible
• Supports Arrays
vars: apache_vhosts: - {servername: “demojinja2.itgix.com", documentroot: "/www/domain"} - {servername: " demojinja3.itgix.com ", documentroot: "/www/otherdomain", serveradmin: "[email protected]"}
ANSIBLE PLAYBOOK ---- - hosts: all user: centos sudo: true tasks: - include_vars: './apache_vhosts.yaml' - name: ensure apache is installed yum: name=httpd state=present - name: make sure apache is running action: service name=httpd state=started enabled=true - name: add template to configuration directory template: 'src=./01-vhost.conf.j2 dest=/etc/httpd/conf.d/01-vhost.conf owner=apache group=apache mode=0755' - name: create document root directories file: path={{ item.documentroot }} state=directory owner=apache group=apache mode=0755 with_items: '{{ apache_vhosts }}' - name: upload index files template: 'src=./{{ item.servername }}.j2 dest=/var/www/html/{{ item.servername }}/index.html owner=apache group=apache mode=0755' with_items: '{{ apache_vhosts }}' - name: restart ansible service service: name=httpd state=restarted
Ansible and Jinja2 in action
THANK YOU!