Creating files with dynamic content
- Scenario: Write an Ansible Playbook to create a file called as Readme.txt with the following content
Hello,
This file is created using Ansible Automation.
The operating system family = <>
Operating system = <>
- For this we would be using Jinja2 templates in Ansible Refer Here and also over here
- Create a file called as readme.txt.j2 with following content
Hello,
This file is created using Ansible Automation.
The operating system family = {{ ansible_os_family }}
Operating system = {{ ansible_distribution }} {{ ansible_distribution_version }}
Thanks from {{ test_user }}
- Now create a playbook with the following content
---
- hosts: all
vars:
test_user: devops team
tasks:
- name: copy the template with dynamic
template:
src: readme.txt.j2
dest: /home/devops/readme.txt
- Now run this playbook on centos machine, to see the file in /home/devops/readme.txt with following content
Hello,
This file is created using Ansible Automation.
The operating system family = RedHat
Operating system = CentOS 7.6
Thanks from devops team
- On ubuntu machine content looks like
Hello,
This file is created using Ansible Automation.
The operating system family = Debian
Operating system = Ubuntu 18.04
Thanks from devops team
-
In Ansible we use Jinja templating for updating configuration files, properties files etc during Deployments
-
Jinja2 comes with lot of filter support Refer Here
-
Experiment 2: Install tomcat 8 on ubuntu Refer Here
-
Manual Steps:
sudo apt-get update
sudo apt install tomcat8 -y
# change configuration in sudo nano /etc/default/tomcat8
restart tomcat
# change configuration in sudo nano /etc/tomcat8/tomcat-users.xml
<tomcat-users>
<user username="admin" password="password" roles="manager-gui,admin-gui"/>
</tomcat-users>
- Create an template file tomcat8.j2 with following content
# Run Tomcat as this user ID. Not setting this or leaving it blank will use the
# default of tomcat8.
TOMCAT8_USER=tomcat8
# Run Tomcat as this group ID. Not setting this or leaving it blank will use
# the default of tomcat8.
TOMCAT8_GROUP=tomcat8
# The home directory of the Java development kit (JDK). You need at least
# JDK version 8. If JAVA_HOME is not set, some common directories for
# OpenJDK and the Oracle JDK are tried.
#JAVA_HOME=/usr/lib/jvm/java-8-openjdk
# You may pass JVM startup parameters to Java here. If unset, the default
# options will be: -Djava.awt.headless=true -XX:+UseConcMarkSweepGC
#
# Use "-XX:+UseConcMarkSweepGC" to enable the CMS garbage collector (improved
# response time). If you use that option and you run Tomcat on a machine with
# exactly one CPU chip that contains one or two cores, you should also add
# the "-XX:+CMSIncrementalMode" option.
JAVA_OPTS="-Djava.awt.headless=true -Xmx{{ xmx_size }}m -XX:MaxPermSize={{ perm_size }}m -XX:+UseConcMarkSweepGC"
# To enable remote debugging uncomment the following line.
# You will then be able to use a java debugger on port 8000.
#JAVA_OPTS="${JAVA_OPTS} -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"
# Java compiler to use for translating JavaServer Pages (JSPs). You can use all
# compilers that are accepted by Ant's build.compiler property.
#JSP_COMPILER=javac
# Use the Java security manager? (yes/no, default: no)
#TOMCAT8_SECURITY=no
# Number of days to keep logfiles in /var/log/tomcat8. Default is 14 days.
#LOGFILE_DAYS=14
# Whether to compress logfiles older than today's
#LOGFILE_COMPRESS=1
# Location of the JVM temporary directory
# WARNING: This directory will be destroyed and recreated at every startup !
#JVM_TMP=/tmp/tomcat8-temp
# If you run Tomcat on port numbers that are all higher than 1023, then you
# do not need authbind. It is used for binding Tomcat to lower port numbers.
# (yes/no, default: no)
#AUTHBIND=no
- Create a file called tomcat-users.xml.j2 with following content
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<tomcat-users xmlns="http://tomcat.apache.org/xml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
version="1.0">
<!--
NOTE: By default, no user is included in the "manager-gui" role required
to operate the "/manager/html" web application. If you wish to use this app,
you must define such a user - the username and password are arbitrary. It is
strongly recommended that you do NOT use one of the users in the commented out
section below since they are intended for use with the examples web
application.
-->
<!--
NOTE: The sample user and role entries below are intended for use with the
examples web application. They are wrapped in a comment and thus are ignored
when reading this file. If you wish to configure these users for use with the
examples web application, do not forget to remove the <!.. ..> that surrounds
them. You will also need to set the passwords to something appropriate.
-->
<!--
<role rolename="tomcat"/>
<role rolename="role1"/>
<user username="tomcat" password="<must-be-changed>" roles="tomcat"/>
<user username="both" password="<must-be-changed>" roles="tomcat,role1"/>
<user username="role1" password="<must-be-changed>" roles="role1"/>
-->
<user username="{{ tomcat_user }}" password=" {{ tomcat_password }}" roles="manager-gui,admin-gui"/>
</tomcat-users>
- Now write a playbook with the following content
---
- hosts: appservers
become: yes
vars:
xmx_size: 512
perm_size: 256
tomcat_user: devops
tomcat_password: devops
tomcat_packages:
- tomcat8-docs
- tomcat8-admin
- tomcat8-examples
tasks:
- name: Update ubuntu packages and install tomcat8
apt:
name: tomcat8
update_cache: yes
state: present
- name: copy tomcat8
template:
src: tomcat8.j2
dest: /etc/default/tomcat8
notify:
- restart and enable tomcat
- name: install tomcat packages
apt:
name: "{{ item }}"
state: present
loop: "{{ tomcat_packages }}"
- name: copy tomcat users
template:
src: tomcat-users.xml.j2
dest: /etc/tomcat8/tomcat-users.xml
notify:
- restart and enable tomcat
handlers:
- name: restart and enable tomcat
service:
name: tomcat8
enabled: yes
state: restarted
- Create a inventory with group appservers and execute ansible playbook