commit 723ebd36917bf84c3f1d8a01c0099961a8ed9194 Author: Denis Savosin Date: Mon Aug 15 10:19:25 2022 +0700 initial: create collection with custom module and role diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..48c89c1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/.idea + +/*.tar.gz diff --git a/README.md b/README.md new file mode 100644 index 0000000..134f0aa --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Ansible Collection - my_own_namespace.yandex_cloud_cvl + +Documentation for the collection. diff --git a/galaxy.yml b/galaxy.yml new file mode 100644 index 0000000..cae36db --- /dev/null +++ b/galaxy.yml @@ -0,0 +1,61 @@ +### REQUIRED +# The namespace of the collection. This can be a company/brand/organization or product namespace under which all +# content lives. May only contain alphanumeric lowercase characters and underscores. Namespaces cannot start with +# underscores or numbers and cannot contain consecutive underscores +namespace: my_own_namespace + +# The name of the collection. Has the same character restrictions as 'namespace' +name: yandex_cloud_cvl + +# The version of the collection. Must be compatible with semantic versioning +version: 1.0.0 + +# The path to the Markdown (.md) readme file. This path is relative to the root of the collection +readme: README.md + +# A list of the collection's content authors. Can be just the name or in the format 'Full Name (url) +# @nicks:irc/im.site#channel' +authors: +- Denis Savosin + + +### OPTIONAL but strongly recommended +# A short summary description of the collection +description: test collection with custom module + +# Either a single license or a list of licenses for content inside of a collection. Ansible Galaxy currently only +# accepts L(SPDX,https://spdx.org/licenses/) licenses. This key is mutually exclusive with 'license_file' +license: +- GPL-2.0-or-later + +# The path to the license file for the collection. This path is relative to the root of the collection. This key is +# mutually exclusive with 'license' +license_file: '' + +# A list of tags you want to associate with the collection for indexing/searching. A tag name has the same character +# requirements as 'namespace' and 'name' +tags: [] + +# Collections that this collection requires to be installed for it to be usable. The key of the dict is the +# collection label 'namespace.name'. The value is a version range +# L(specifiers,https://python-semanticversion.readthedocs.io/en/latest/#requirement-specification). Multiple version +# range specifiers can be set and are separated by ',' +dependencies: {} + +# The URL of the originating SCM repository +repository: https://github.com/Dannecron/netology-devops-ansible-yandex-cloud-cvl + +# The URL to any online docs +documentation: https://github.com/Dannecron/netology-devops-ansible-yandex-cloud-cvl + +# The URL to the homepage of the collection/project +homepage: https://github.com/Dannecron/netology-devops-ansible-yandex-cloud-cvl + +# The URL to the collection issue tracker +#issues: http://example.com/issue/tracker + +# A list of file glob-like patterns used to filter any files or directories that should not be included in the build +# artifact. A pattern is matched from the relative path of the file or directory of the collection directory. This +# uses 'fnmatch' to match the files or directories. Some directories and files like 'galaxy.yml', '*.pyc', '*.retry', +# and '.git' are always filtered +build_ignore: [] diff --git a/plugins/README.md b/plugins/README.md new file mode 100644 index 0000000..29aa319 --- /dev/null +++ b/plugins/README.md @@ -0,0 +1,31 @@ +# Collections Plugins Directory + +This directory can be used to ship various plugins inside an Ansible collection. Each plugin is placed in a folder that +is named after the type of plugin it is in. It can also include the `module_utils` and `modules` directory that +would contain module utils and modules respectively. + +Here is an example directory of the majority of plugins currently supported by Ansible: + +``` +└── plugins + ├── action + ├── become + ├── cache + ├── callback + ├── cliconf + ├── connection + ├── filter + ├── httpapi + ├── inventory + ├── lookup + ├── module_utils + ├── modules + ├── netconf + ├── shell + ├── strategy + ├── terminal + ├── test + └── vars +``` + +A full list of plugin types can be found at [Working With Plugins](https://docs.ansible.com/ansible-core/2.13/plugins/plugins.html). diff --git a/plugins/modules/my_own_module.py b/plugins/modules/my_own_module.py new file mode 100644 index 0000000..f89ee88 --- /dev/null +++ b/plugins/modules/my_own_module.py @@ -0,0 +1,121 @@ +#!/usr/bin/python + +# Copyright: (c) 2018, Terry Jones +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +DOCUMENTATION = r''' +--- +module: my_own_module + +short_description: Netology devops test module + +# If this is part of a collection, you need to use semantic versioning, +# i.e. the version is of the form "2.5.0" and not "2.4". +version_added: "1.0.0" + +description: Создание тестового файла на удалённом хосте по определённому пути с определённым содержимым + +options: + path: + description: Путь до файла + required: true + type: str + new: + description: Содержимое файла + required: true + type: str +# Specify this value according to your collection +# in format of namespace.collection.doc_fragment_name +extends_documentation_fragment: + - my_namespace.my_collection.my_doc_fragment_name + +author: + - Denis Savosin (@Dannecron) +''' + +EXAMPLES = r''' +# Pass in a message +- name: Test with a message + my_namespace.my_collection.my_test: + name: hello world + +# pass in a message and have changed true +- name: Test with a message and changed output + my_namespace.my_collection.my_test: + name: hello world + new: true + +# fail the module +- name: Test failure of the module + my_namespace.my_collection.my_test: + name: fail me +''' + +RETURN = r''' +''' + +import os + +from ansible.module_utils.basic import AnsibleModule + +def run_module(): + # define available arguments/parameters a user can pass to the module + module_args = dict( + path=dict(type='str', required=True), + content=dict(type='str', required=True) + ) + + # seed the result dict in the object + # we primarily care about changed and state + # changed is if this module effectively modified the target + # state will include any data that you want your module to pass back + # for consumption, for example, in a subsequent task + result = dict(changed=False) + + # the AnsibleModule object will be our abstraction working with Ansible + # this includes instantiation, a couple of common attr would be the + # args/params passed to the execution, as well as if the module + # supports check mode + module = AnsibleModule( + argument_spec=module_args, + supports_check_mode=True + ) + + # if file does not exists then set changed to True + # else get content of file and compare it with content variable + # if contents are not equal, then changed=True + + file_exists = os.path.exists(module.params['path']) + file_content = '' + + if not file_exists: + result['changed'] = True + else: + file = open(module.params['path'], mode='r') + file_content = file.read() + file.close() + + if file_content != module.params['content']: + result['changed'] = True + + if module.check_mode: + module.exit_json(**result) + + if result['changed'] == False: + module.exit_json(**result) + + file = open(module.params['path'], mode='w') + file.write(module.params['content']) + file.close() + + module.exit_json(**result) + + +def main(): + run_module() + + +if __name__ == '__main__': + main() diff --git a/roles/my_own_role/defaults/main.yml b/roles/my_own_role/defaults/main.yml new file mode 100644 index 0000000..7d00468 --- /dev/null +++ b/roles/my_own_role/defaults/main.yml @@ -0,0 +1,3 @@ +--- +my_path: "/tmp/file" +my_content: "some_content" diff --git a/roles/my_own_role/tasks/main.yml b/roles/my_own_role/tasks/main.yml new file mode 100644 index 0000000..6d7eda9 --- /dev/null +++ b/roles/my_own_role/tasks/main.yml @@ -0,0 +1,5 @@ +--- +- name: create file + my_own_namespace.yandex_cloud_cvl.my_own_module: + path: "{{ my_path }}" + content: "{{ my_content }}"