Simple Jinja2 templating script

Posted on Thu 14 January 2021 in devops • Tagged with python, jinja2


What I really love about Ansible is it's powerful templating capabilities.
It all boils down to the jinja2 templating engine, so I wrote a small script for just that part.

In this example I am using Jinja2 templating to generate a .ssh/config file from a yaml dict


# example/vars.yaml example/ssh_config.j2 [out.txt]

from jinja2 import Template
import yaml, sys
# load yaml vars file
yaml = yaml.safe_load(open(str(sys.argv[1]), 'r'))
# load jinja2 template file
template = Template(open(str(sys.argv[2])).read())
# if no out file was provided
if len(sys.argv) == 3:
    # print to stdout
# if outfile was provided
elif len(sys.argv) == 4:
    # write result to output file
    with open(str(sys.argv[3]), 'w') as f:
        print(template.render(yaml), file=f)


the sources, documentation and examples are located here

Running Ansible inside Docker

Posted on Tue 22 December 2020 in devops • Tagged with ansible, docker, bash


Sometimes it is important to be independent from your local dev machines setup.
I wrote a small wrapper script to run ansible tasks inside a docker container.


# check if argument was supplied
if [ $# -eq 0 ]
    echo "No arguments supplied; usage: $0 'ansible-playbook playbook.yaml' # The QUOTES are important here! "
    exit 1

if [ -f "$vault" ]; then
    docker container run -it --rm \
    -v $(pwd)/../:/data \
    -v $vault:/root/.ansible-vault-pass \
    -e ANSIBLE_VAULT_PASSWORD_FILE=/root/.ansible-vault-pass \
    cytopia/ansible:latest $1
    echo "$vault file does not exist."