Skip to main content

Command line tool to template the structure of a new ansible role.

Project description

ansible-roler

Build Status

ansible-roler is a simple command line tool to setup the recommendet folder structure for a new ansible role.

Table of Content


Setup

# From internal pip repo as user
pip install ansible-roler --user

# .. or as root
sudo pip install ansible-roler

Usage

$ ansible-roler --help
usage: ansible-roler [-h] [-c CONFIG_FILE] [-n NAME] [-p PATH] [-v]

Manage ansible role environments

optional arguments:
  -h, --help            show this help message and exit
  -c CONFIG_FILE        Location of configuration file:
                        [/Users/rkau2905/Library/Application Support/ansible-
                        role/config.ini]
  -n NAME, --name NAME  Name of the new role
  -p PATH, --path PATH  Path where the new role will be created
  -v, --verbose         Show more verbose output

Configuration

Defaults

ansible-roler will create the the minimal recommended folder structure:

common/
    tasks/
        main.yml
    handlers/
        main.yml
    templates/
    files/
    vars/
    defaults/
        main.yml
    meta/
        main.yml

The main.yml files will be created only if you enable the templating feature. Otherwise the folders will be left empty.

Base configuration

In addition to the command line options there are parameters you can adjust in a config file. The default location for your config file is ~/ansible-roler/config.ini but you can place it anywhere and specifiy the path with ansible-roler -c /path/to/config.ini.

[base]
# base path ansible-roler will use to create new roles
base_path=~/ansibleroles

[logging]
# error | warning | info | debug
# you can also control this with commandline attribute -vvv
log_level=warning

Templating

In special cases it can be helpfule to add templated files to each new role. The templating function can be used to place in customized meta/main.yml or a default config file for your CI in each new role. The templating is disabled by default and must be enabled in the config file befor you can use it.

[templating]
# enable template functionality
enable_templating=false
# path to your own subdir template file
# if not in config file default one will be used
# if added empty 'subdir_template=' subdir templating is disabled
subdir_template=/home/jdoe/ansible/custom/main.yaml.j2
# if you like you can exclude some subdirs from templating
# these folders will be left empty
exclude_subdirs=templates,vars,files
# path to your own ci template file
# if not in config file default one will be used
# if added empty 'ci_template=' ci templating is disabled
root_template=/home/jdoe/ansible/custom/.drone.yaml.j2

[template-vars]
# define some variables you want to use in your template
meta_author=John Doe
meta_license=MIT

ansible-roler comes with simple default template file but as you can see in the config you can customize and use your own. The default file looks as follows:

---
{% if subdir == 'tasks' %}
# Contains the main list of tasks to be executed by the role.
# Don't add tasks directly to the main.yml use includes instead
- include_tasks: setup.yml
{% endif %}
{% if subdir == 'handlers' %}
# Contains handlers, which may be used by this role or
# even anywhere outside this role.
{% endif %}
{% if subdir == 'defaults' %}
# Default variables for the role.
{% endif %}
{% if subdir == 'meta' %}
galaxy_info:
  author: {{ vars.meta.author | default('UNKNOWN') }}
  description: Deploy some application
  license: {{ vars.meta.author | default('MIT') }}
  min_ansible_version: 2.4
  platforms:
    - name: EL
      versions:
        - 7
  galaxy_tags:
    - myapp
dependencies: []
{% endif %}

Currently, you can set two template files:

  • subdir_template: template will be deployed to the folders, tasks, handlers, defaults and meta. This can be used to provide a pre-configured main.yml in each of these folders.
  • root_template: tempate will be deployed to the root of your role. This can be used to provide a default config for your ci system.

Templating in ansible-roler works only with two static jinja2 files but you can control the content of the destination file with variables. Following variables will be automatically passed to the template processor:

  • subdir_template
    • rolename: these variable holds the rolename you have passed to ansible-roler If you have prefixed your role with prexix.myrole only the second part will be used.
    • subdir: these variable holds the current subdir which is processed at this time. This is a good option to add differen content to your destination file in relation to the current directory. You look at the usage in the build-in example above.
  • root_template
    • rolename

There is also an option to set custom variables. These variables will be accessable throug vars. This variable is an empty dictionary as long as you don't set some variables. Therefore you have to define variables under the `template-vars' section in config file.

[template-vars]
# define some variables you want to use in your template
meta_author=John Doe
meta_license=MIT

Variable names will be split at the first underscore. The first part is used as the name of a sub-dict under vars the other part is used as the of your variable. The result of the these small example looks as follows:

{
  'meta': {
    'author': 'John Doe',
    'license': 'MIT'
  }
}

If you want to access your variables in a template, here is an example {{ vars.meta.author }}.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Maintainers and Contributors

Robert Kaussow

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ansible-roler-0.2.3.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ansible_roler-0.2.3-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file ansible-roler-0.2.3.tar.gz.

File metadata

  • Download URL: ansible-roler-0.2.3.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.4.3

File hashes

Hashes for ansible-roler-0.2.3.tar.gz
Algorithm Hash digest
SHA256 afa3a74ca331a00c8cf7c9eb07a4c6f9c29d901a6f578bab930cee158fcb11db
MD5 db6079a46adb12d599e4aaa16ac718af
BLAKE2b-256 4eac5c3560d1314ae51be03b24f8ea957149459b0af30f7f74b487501b42fa2c

See more details on using hashes here.

File details

Details for the file ansible_roler-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: ansible_roler-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.4.3

File hashes

Hashes for ansible_roler-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ade85e13e4c74bfe5157a7bc3ec56e1d99eef7de52187a0742fe7261b05f9a34
MD5 259d3da76359d7f6788e050a5c4dd0ef
BLAKE2b-256 4b21e4507b77177af6f8bc50798a475c733e0528c3643b1e202370aa0636ed3a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page