Ansible playbook from the ground up.


This is based on the the Ansible best practices documented here. http://docs.ansible.com/ansible/playbooks_best_practices.html

#! /bin/bash

# make a directory for roles
mkdir roles

# make a directory for group variables
mkdir group_vars

# make a directory for all
mkdir group_vars/all

# add a vars file and add a variable to it
touch group_vars/all/vars.yml
echo 'my_var: {{ vault_my_var }}' >> group_vars/all/vars.yml

# add a vault file, a vaiable and encrypt it
touch group_vars/all/vault.yml
echo vault_my_var: 12345 >> group_vars/all/vault.yml
ansible-vault encrypt group_vars/all/vault.yml

# make a directory for host variables
mkdir host_vars

# if any custom modules, put them here       
mkdir library  

#if any custom filter plugins, put them here       
mkdir filter_plugins

#callback directory  
mkdir callback_plugins

#make a root file
touch site.yml

#add an ansible.cfg file    
touch ansible.cfg  

# add some basics to the ansible.cfg    
cat > ansible.cfg <<EOL
[defaults]
callback_plugins = ./callback_plugins
filter_plugins = ./filter_plugins
vault_password_file = vault_pass.py
log_path = ./log.txt
EOL

# add the vault decryption script to get the vault password from an environment variables
cat > vault_pass.py <<EOL
#!/usr/bin/env python
import os
print os.environ.get('ANSIBLE_VAULT_PASSWORD')
EOL

echo The value of environment variable ANSIBLE_VAULT_PASSWORD will be used to decrypt the vault.

# build a new role called default
ansible-galaxy init --init-path=roles --offline --verbose default

# review what's built
tree

The result should look like this:

New Vault password:
Confirm New Vault password:
Encryption successful
The value of environment variable ANSIBLE_VAULT_PASSWORD will be used to decrypt the vault.
Using /Users/bthornto/github/ansible_network_getting_started/eos_example/ansible.cfg as config file
- default was created successfully
.
├── ansible.cfg
├── callback_plugins
├── filter_plugins
├── go.sh
├── group_vars
│   └── all
│       ├── vars.yml
│       └── vault.yml
├── host_vars
├── library
├── log.txt
├── roles
│   └── default
│       ├── README.md
│       ├── defaults
│       │   └── main.yml
│       ├── files
│       ├── handlers
│       │   └── main.yml
│       ├── meta
│       │   └── main.yml
│       ├── tasks
│       │   └── main.yml
│       ├── templates
│       ├── tests
│       │   ├── inventory
│       │   └── test.yml
│       └── vars
│           └── main.yml
├── site.yml
└── vault_pass.py

16 directories, 15 files