commit 4789552a6e4e5b5f99a6c30042e3848f62e10df5 Author: Jack-Benny Persson Date: Thu Mar 17 02:59:54 2022 +0100 Initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..60d7274 --- /dev/null +++ b/README.md @@ -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 + +``` diff --git a/galaxy.yml b/galaxy.yml new file mode 100644 index 0000000..2a4ca3d --- /dev/null +++ b/galaxy.yml @@ -0,0 +1,19 @@ +namespace: jackbenny +name: demo +version: 0.0.1 +readme: README.md +authors: +- Jack-Benny Persson +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 diff --git a/plugins/modules/dummy.py b/plugins/modules/dummy.py new file mode 100644 index 0000000..01decca --- /dev/null +++ b/plugins/modules/dummy.py @@ -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() diff --git a/roles/base/defaults/main.yml b/roles/base/defaults/main.yml new file mode 100644 index 0000000..c85b4be --- /dev/null +++ b/roles/base/defaults/main.yml @@ -0,0 +1 @@ +timezone: Europe/Stockholm diff --git a/roles/base/meta/main.yml b/roles/base/meta/main.yml new file mode 100644 index 0000000..79dda0f --- /dev/null +++ b/roles/base/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: + - role: update_cache diff --git a/roles/base/tasks/main.yml b/roles/base/tasks/main.yml new file mode 100644 index 0000000..5954088 --- /dev/null +++ b/roles/base/tasks/main.yml @@ -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 }}" diff --git a/roles/update_cache/tasks/main.yml b/roles/update_cache/tasks/main.yml new file mode 100644 index 0000000..fcdce12 --- /dev/null +++ b/roles/update_cache/tasks/main.yml @@ -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'