Initial commit

This commit is contained in:
Jack-Benny Persson 2022-03-17 02:59:54 +01:00
commit 4789552a6e
7 changed files with 185 additions and 0 deletions

69
README.md Normal file
View File

@ -0,0 +1,69 @@
# Ansible Collection - jackbenny.demo
This is a simple demonstration of what a collection looks like and works. The
collection is made as a part of my Swedish book about Ansible.
The `base` role installs some common package and sets the timezone according to
the `timezone`-variable. The default timezone is set to Europe/Stockholm. The
`base`-role uses the `update_cache`-role to update the package manager cache.
The `dummy` module demonstrates a simple module and doesn't do anything except
check if a value is greater than 50.
## Content
This collection contains the following:
* roles
* base
* update_cache
* modules
* dummy
## Example usage
```
- hosts: ankeborg
become: true
vars:
timezone: Europe/Stockholm
tasks:
- name: Test my dummy module
jackbenny.demo.dummy:
number: 51
register: the_num
- name: Print the return value
debug:
msg: "{{ the_num }}"
roles:
- jackbenny.demo.base
```
or, if your prefer to use the `collections` keyword:
```
- hosts: ankeborg
become: true
vars:
timezone: Europe/Stockholm
collections:
- jackbenny.demo
tasks:
- name: Test my dummy module
dummy:
number: 51
register: the_num
- name: Print the return value
debug:
msg: "{{ the_num }}"
roles:
- base
```

19
galaxy.yml Normal file
View File

@ -0,0 +1,19 @@
namespace: jackbenny
name: demo
version: 0.0.1
readme: README.md
authors:
- Jack-Benny Persson <jack-benny@cyberinfo.se>
description: Demonstration of a collection for my book about Ansible
license:
- GPL-2.0-or-later
tags:
- system
- demo
dependencies: {}
build_ignore: []
repository: https://github.com/jackbenny/ansible-collection-demo
documentation: https://github.com/jackbenny/ansible-collection-demo
homepage: https://github.com/jackbenny/ansible-collection-demo
issues: https://github.com/jackbenny/ansible-collection-demo/issues

73
plugins/modules/dummy.py Normal file
View File

@ -0,0 +1,73 @@
#!/usr/bin/python
DOCUMENTATION = r'''
---
module: dummy
short_description: This is a simply dummy module
version_added: "0.0.1"
description: This is a simply dummy module for demostrative puropses only.
The module doesn't do anything, except check if a value is greater than 50.
If the value is greater than 50, then the module reports a change. If the
value is below 50, no change is reported.
options:
number:
description: The value to check if it's greater than 50.
required: true
type: int
author:
- Jack-Benny Persson (jack-benny@cyberinfo.se)
'''
EXAMPLES = r'''
# Pass in a number
- name: Test a number
jackbenny.demo.dummy:
number: 55
'''
RETURN = r'''
number:
description: The original number passed to the module.
type: int
returned: always
sample: 55
'''
from ansible.module_utils.basic import AnsibleModule
def run_module():
# define arguments to the module
module_args = dict(
number=dict(type='int', required=True),
)
# create a dict for the result
result = dict(
changed=False,
number=0,
)
# settings for the module
module = AnsibleModule(
argument_spec=module_args,
supports_check_mode=False
)
# the logic for the module
result['number'] = module.params['number']
if result['number'] > 50:
result['changed']=True
# return the result as json
module.exit_json(**result)
def main():
run_module()
if __name__ == '__main__':
main()

View File

@ -0,0 +1 @@
timezone: Europe/Stockholm

2
roles/base/meta/main.yml Normal file
View File

@ -0,0 +1,2 @@
dependencies:
- role: update_cache

13
roles/base/tasks/main.yml Normal file
View File

@ -0,0 +1,13 @@
- name: Install common tools
package:
name: "{{ item }}"
with_items:
- curl
- gnupg
- vim
- ca-certificates
- s-nail
- name: Set the timezone
timezone:
name: "{{ timezone }}"

View File

@ -0,0 +1,8 @@
- name: Update cache on Debian/Ubuntu
apt:
update_cache: yes
when: ansible_os_family == 'Debian'
- name: Update cache on RedHat/CentOS/Fedora
dnf:
update_cache: yes
when: ansible_os_family == 'RedHat'