ansible meetup-kamen
TRANSCRIPT
![Page 1: Ansible meetup-kamen](https://reader036.vdocuments.us/reader036/viewer/2022062503/586e8ceb1a28aba0038b867f/html5/thumbnails/1.jpg)
The Power of Jinja2 Templates
Kamen Tarlov Middleware Engineer and co-founder
![Page 2: Ansible meetup-kamen](https://reader036.vdocuments.us/reader036/viewer/2022062503/586e8ceb1a28aba0038b867f/html5/thumbnails/2.jpg)
Agenda• About Me
• Introduction to Jinja2 Templates
• Jinja2 IF and Loop Statements
• Jinja2 Template Skeleton
• Ansible and Jinja2 in action
• Questions and Answers
![Page 3: Ansible meetup-kamen](https://reader036.vdocuments.us/reader036/viewer/2022062503/586e8ceb1a28aba0038b867f/html5/thumbnails/3.jpg)
• Middleware Engineer and co-founder
• Experience with MW Technologies and Configuration Management
• Ansible, Puppet ..
• Openstack
About Me
![Page 4: Ansible meetup-kamen](https://reader036.vdocuments.us/reader036/viewer/2022062503/586e8ceb1a28aba0038b867f/html5/thumbnails/4.jpg)
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
![Page 5: Ansible meetup-kamen](https://reader036.vdocuments.us/reader036/viewer/2022062503/586e8ceb1a28aba0038b867f/html5/thumbnails/5.jpg)
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
![Page 6: Ansible meetup-kamen](https://reader036.vdocuments.us/reader036/viewer/2022062503/586e8ceb1a28aba0038b867f/html5/thumbnails/6.jpg)
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]"}
![Page 7: Ansible meetup-kamen](https://reader036.vdocuments.us/reader036/viewer/2022062503/586e8ceb1a28aba0038b867f/html5/thumbnails/7.jpg)
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
![Page 8: Ansible meetup-kamen](https://reader036.vdocuments.us/reader036/viewer/2022062503/586e8ceb1a28aba0038b867f/html5/thumbnails/8.jpg)
Ansible and Jinja2 in action
![Page 9: Ansible meetup-kamen](https://reader036.vdocuments.us/reader036/viewer/2022062503/586e8ceb1a28aba0038b867f/html5/thumbnails/9.jpg)
THANK YOU!