Skip to main content

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

Metadata

Release files for ansible-roler 0.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ansible-roler 0.2.3
File Size Uploaded
ansible-roler-0.2.3.tar.gz 8.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ansible-roler 0.2.3
File Interpreter ABI Platform
ansible_roler-0.2.3-py3-none-any.whl Python 3 none any Details

Total release size: 18.0 kB

Release files / ansible-roler-0.2.3.tar.gz

Download URL ansible-roler-0.2.3.tar.gz
Size 8.2 kB
Tags Source
SHA-256 checksum
How to use checksums
afa3a74ca331a00c8cf7c9eb07a4c6f9c29d901a6f578bab930cee158fcb11db
BLAKE2b-256 checksum
How to use checksums
4eac5c3560d1314ae51be03b24f8ea957149459b0af30f7f74b487501b42fa2c
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / ansible_roler-0.2.3-py3-none-any.whl

Download URL ansible_roler-0.2.3-py3-none-any.whl
Size 9.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ade85e13e4c74bfe5157a7bc3ec56e1d99eef7de52187a0742fe7261b05f9a34
BLAKE2b-256 checksum
How to use checksums
4b21e4507b77177af6f8bc50798a475c733e0528c3643b1e202370aa0636ed3a
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.2.3 This release

2 release files

0.2.2

2 release files

0.2.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page