Skip to content

Commit

Permalink
Add support for creating roles and update documentation to match the …
Browse files Browse the repository at this point in the history
…update
  • Loading branch information
Kevin Kirsche committed Aug 23, 2017
1 parent 0c1fc2b commit 52b12e7
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 57 deletions.
62 changes: 39 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,28 @@ pip install -U dist/*.whl
### Help Text

```
usage: ansible-generate [-h] [-a] [-i INVENTORIES [INVENTORIES ...]] [-v]
[projects [projects ...]]
usage: ansible-generate [-h] [-a] [-i INVENTORIES [INVENTORIES ...]]
[-r ROLES [ROLES ...]] [-v]
[-p PROJECTS [PROJECTS ...]]
Generate an ansible playbook directory structure
positional arguments:
projects
optional arguments:
-h, --help show this help message and exit
-a, --alternate-layout
-i INVENTORIES [INVENTORIES ...], --inventories INVENTORIES [INVENTORIES ...]
-r ROLES [ROLES ...], --roles ROLES [ROLES ...]
-v, --verbose
-p PROJECTS [PROJECTS ...], --projects PROJECTS [PROJECTS ...]
```

#### Defaults

* `alternate-layout` --- False
* `verbose` --- False
* `inventories` `['production', 'staging']`
* `alternate-layout` --- `False`
* `verbose` --- `False`
* `inventories` --- `['production', 'staging']`
* `roles` --- `[]`
* `projects` --- `[]`

### Example

Expand All @@ -68,33 +70,47 @@ ansible-generate
#### New-project

```
ansible-generate playbook_name
ansible-generate -p playbook_name
```

#### Alternate Layout

```
ansible-generate -a playbook_name
ansible-generate -a
```

#### Custom Inventories

```
ansible-generate -i production staging lab -a test
ansible-generate -i production staging lab
```

#### Roles
This portion of the tool relies on Ansible's `ansible-galaxy` command line
application
```
ansible-generate -r role1 role2
```

#### Output

```
~/Downloads ❯❯❯ ansible-generate -i production staging lab -a test
creating directory /Users/example/Downloads/test/roles
creating directory /Users/example/Downloads/test/inventories/production/group_vars
creating directory /Users/example/Downloads/test/inventories/production/host_vars
creating directory /Users/example/Downloads/test/inventories/staging/group_vars
creating directory /Users/example/Downloads/test/inventories/staging/host_vars
creating directory /Users/example/Downloads/test/inventories/lab/group_vars
creating directory /Users/example/Downloads/test/inventories/lab/host_vars
creating file /Users/example/Downloads/test/inventories/production/hosts
creating file /Users/example/Downloads/test/inventories/staging/hosts
creating file /Users/example/Downloads/test/inventories/lab/hosts
creating file /Users/example/Downloads/test/site.yml
~/Downloads ❯❯❯ ansible-generate -i production staging lab -r common ubuntu centos -a -p network_security_baseline
creating directory /Users/example_user/Downloads/network_security_baseline/roles
creating directory /Users/example_user/Downloads/network_security_baseline/inventories/production/group_vars
creating directory /Users/example_user/Downloads/network_security_baseline/inventories/production/host_vars
creating directory /Users/example_user/Downloads/network_security_baseline/inventories/staging/group_vars
creating directory /Users/example_user/Downloads/network_security_baseline/inventories/staging/host_vars
creating directory /Users/example_user/Downloads/network_security_baseline/inventories/lab/group_vars
creating directory /Users/example_user/Downloads/network_security_baseline/inventories/lab/host_vars
creating file /Users/example_user/Downloads/network_security_baseline/inventories/production/hosts
creating file /Users/example_user/Downloads/network_security_baseline/inventories/staging/hosts
creating file /Users/example_user/Downloads/network_security_baseline/inventories/lab/hosts
creating file /Users/example_user/Downloads/network_security_baseline/site.yml
ansible galaxy output for role common:
- common was created successfully
ansible galaxy output for role ubuntu:
- ubuntu was created successfully
ansible galaxy output for role centos:
- centos was created successfully
```
64 changes: 41 additions & 23 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,28 @@ Help Text

::

usage: ansible-generate [-h] [-a] [-i INVENTORIES [INVENTORIES ...]] [-v]
[projects [projects ...]]
usage: ansible-generate [-h] [-a] [-i INVENTORIES [INVENTORIES ...]]
[-r ROLES [ROLES ...]] [-v]
[-p PROJECTS [PROJECTS ...]]

Generate an ansible playbook directory structure

positional arguments:
projects

optional arguments:
-h, --help show this help message and exit
-a, --alternate-layout
-i INVENTORIES [INVENTORIES ...], --inventories INVENTORIES [INVENTORIES ...]
-r ROLES [ROLES ...], --roles ROLES [ROLES ...]
-v, --verbose
-p PROJECTS [PROJECTS ...], --projects PROJECTS [PROJECTS ...]

Defaults
^^^^^^^^

- ``alternate-layout`` --- False
- ``verbose`` --- False
- ``inventories`` ``['production', 'staging']``
- ``alternate-layout`` --- ``False``
- ``verbose`` --- ``False``
- ``inventories`` --- ``['production', 'staging']``
- ``roles`` --- ``[]``
- ``projects`` --- ``[]``

Example
~~~~~~~
Expand All @@ -88,36 +90,52 @@ New-project

::

ansible-generate playbook_name
ansible-generate -p playbook_name

Alternate Layout
^^^^^^^^^^^^^^^^

::

ansible-generate -a playbook_name
ansible-generate -a

Custom Inventories
^^^^^^^^^^^^^^^^^^

::

ansible-generate -i production staging lab -a test
ansible-generate -i production staging lab

Roles
^^^^^

This portion of the tool relies on Ansible's ``ansible-galaxy`` command
line application

::

ansible-generate -r role1 role2

Output
^^^^^^

::

~/Downloads ❯❯❯ ansible-generate -i production staging lab -a test
creating directory /Users/example/Downloads/test/roles
creating directory /Users/example/Downloads/test/inventories/production/group_vars
creating directory /Users/example/Downloads/test/inventories/production/host_vars
creating directory /Users/example/Downloads/test/inventories/staging/group_vars
creating directory /Users/example/Downloads/test/inventories/staging/host_vars
creating directory /Users/example/Downloads/test/inventories/lab/group_vars
creating directory /Users/example/Downloads/test/inventories/lab/host_vars
creating file /Users/example/Downloads/test/inventories/production/hosts
creating file /Users/example/Downloads/test/inventories/staging/hosts
creating file /Users/example/Downloads/test/inventories/lab/hosts
creating file /Users/example/Downloads/test/site.yml
~/Downloads ❯❯❯ ansible-generate -i production staging lab -r common ubuntu centos -a -p network_security_baseline
creating directory /Users/example_user/Downloads/network_security_baseline/roles
creating directory /Users/example_user/Downloads/network_security_baseline/inventories/production/group_vars
creating directory /Users/example_user/Downloads/network_security_baseline/inventories/production/host_vars
creating directory /Users/example_user/Downloads/network_security_baseline/inventories/staging/group_vars
creating directory /Users/example_user/Downloads/network_security_baseline/inventories/staging/host_vars
creating directory /Users/example_user/Downloads/network_security_baseline/inventories/lab/group_vars
creating directory /Users/example_user/Downloads/network_security_baseline/inventories/lab/host_vars
creating file /Users/example_user/Downloads/network_security_baseline/inventories/production/hosts
creating file /Users/example_user/Downloads/network_security_baseline/inventories/staging/hosts
creating file /Users/example_user/Downloads/network_security_baseline/inventories/lab/hosts
creating file /Users/example_user/Downloads/network_security_baseline/site.yml
ansible galaxy output for role common:
- common was created successfully
ansible galaxy output for role ubuntu:
- ubuntu was created successfully
ansible galaxy output for role centos:
- centos was created successfully
16 changes: 11 additions & 5 deletions ansible_generator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,23 @@ def cli():
action=u'store_true',
dest=u'alternate_layout')
parser.add_argument(
'-i',
'--inventories',
u'-i',
u'--inventories',
nargs='+',
default=['production', 'staging'],
default=[u'production', u'staging'],
dest=u'inventories',
type=str)
parser.add_argument(
'-r', '--roles', nargs='+', default=[], dest=u'roles', type=str)
u'-r', u'--roles', nargs=u'+', default=[], dest=u'roles', type=str)
parser.add_argument(
'-v', '--verbose', action='store_true', dest='verbosity')
parser.add_argument(u'projects', nargs='*', default=None)
parser.add_argument(
u'-p',
u'--projects',
nargs=u'+',
default=[],
dest=u'projects',
type=str)

args = parser.parse_args()

Expand Down
14 changes: 9 additions & 5 deletions ansible_generator/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ def create_file_layout(projects=None,
if not success:
return False

for role in roles:
success = create_role(
rolename=role, directory='{project}/roles'.format(project=project))
if not success:
return False
for project in projects:
for role in roles:
success = create_role(
rolename=role,
directory='{project}/roles'.format(project=project))
if not success:
return False
return True


Expand Down Expand Up @@ -99,6 +101,8 @@ def create_role(rolename, directory):

stdout = stdoutf.read()
stderr = stderrf.read()

print(u'ansible galaxy output for role {r}:'.format(r=rolename))
if stdout:
print(stdout.strip())
if stderr:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
# your project is installed. For an analysis of "install_requires" vs pip's
# requirements files see:
# https://packaging.python.org/en/latest/requirements.html
install_requires=['raven'],
install_requires=['raven', 'ansible'],

# List additional groups of dependencies here (e.g. development
# dependencies). You can install these using the following syntax,
Expand Down

0 comments on commit 52b12e7

Please sign in to comment.