Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgini committed Jun 12, 2022
1 parent d37b0df commit 0ed4cb8
Show file tree
Hide file tree
Showing 49 changed files with 304 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Chapter-15/ansible.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ roles_path = roles

deprecation_warnings = False
host_key_checking = false

library = ./library

[privilege_escalation]
become = false
Expand Down
6 changes: 6 additions & 0 deletions Chapter-15/collection/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# This is the documentation page for custom colletion demo

(Documentation and detaiils goes here)

This collection was created for Ansible Real Life book by Packt

Author: [Gineesh Mada Pparambath](https://www.linkedin.com/in/gineesh/)

Book Repository: [PacktPublishing/Ansible-for-Real-life-Automation](https://github.com/PacktPublishing/Ansible-for-Real-life-Automation/)
9 changes: 4 additions & 5 deletions Chapter-15/collection/galaxy.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@

---
namespace: ginigangadharan
name: custom_modules_demo
version: 1.0.2
version: 1.0.4
readme: README.md
authors:
- Gineesh Madapparambath <[email protected]>
description: Ansible Custom Module Demo
description: Ansible Custom Module Demo for Ansible Book
license:
- GPL-2.0-or-later
license_file: ''
Expand All @@ -16,6 +15,6 @@ tags:
- devops
dependencies: {}
repository: https://github.com/PacktPublishing/Ansible-for-Real-life-Automation/
documentation: https://github.com/PacktPublishing/Ansible-for-Real-life-Automation/blob/main/Chapter-15/README.md
homepage: https://github.com/PacktPublishing/Ansible-for-Real-life-Automation/blob/main/Chapter-15/README.md
documentation: https://github.com/PacktPublishing/Ansible-for-Real-life-Automation/tree/main/Chapter-15/collection
homepage: https://github.com/PacktPublishing/Ansible-for-Real-life-Automation/tree/main/Chapter-15/collection
issues: https://github.com/PacktPublishing/Ansible-for-Real-life-Automation/issues
18 changes: 18 additions & 0 deletions Chapter-15/hello-bash.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
- name: Testing Custom Module
hosts: node1
gather_facts: false

vars:
app_name: "bash"
app_version: "1.0"

tasks:
- name: Application Name and Version
customhello:
application_name: "{{ app_name }}"
application_version: "{{ app_version }}"
register: custom_value

- debug:
msg: "{{ custom_value }}"
16 changes: 16 additions & 0 deletions Chapter-15/hello-python.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
- name: Testing Custom Module
hosts: localhost
gather_facts: false
vars:
custom_message: "Hello"
custome_name: "John"
tasks:
- name: Calling custom module
hello_message:
message: "{{ custom_message }}"
name: "{{ custome_name }}"
register: custom_value

- debug:
msg: "{{ custom_value }}"
20 changes: 20 additions & 0 deletions Chapter-15/library/customhello.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
#
# This script accepts two inputs
# 1. application_name
# 2. application_version

changed="false"
display="This is a simple bash module"
OS="$(uname)"
HOSTNAME="$(uname -n)"

source $1
display="Application Name: $application_name (version: $application_version)"
if [ "$application_name" == "bash" ]; then
changed="true"
display="$display - This is a bash App"
fi

printf '{"changed": %s, "msg": "%s", "operating_system": "%s", "hostname": "%s"}' "$changed" "$display" "$OS" "$HOSTNAME"
exit 0
87 changes: 87 additions & 0 deletions Chapter-15/library/hello_message.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# NO Copyright: feel freee to use - Gineesh ([email protected])


DOCUMENTATION = '''
---
module: hello_message
short_description: A Hello Message Module
version_added: "2.10"
description:
- "A Hello Message Module"
options:
message:
description:
- The message to be printed.
required: true
type: string
name:
description:
- The name of the person.
required: false
type: string
author:
- Gineesh Madapparambath (@ginigangadharan)
'''

EXAMPLES = '''
# Simple Custom Hello App
- name: Calling hello_message module
hello_message:
message: "Hello"
name: "John"
'''

RETURN = '''
greeting:
description: Hello Response
returned: success
type: str
sample: Hello World
os_version:
description: Operating System Information
returned: success
type: str
sample: Linux 4.18.0-305.el8.x86_64 #1 SMP Thu Apr 29 08:54:30 EDT 2021
'''

from ansible.module_utils.basic import AnsibleModule, platform

def main():
module_args = dict(
message=dict(type='str', required=True),
name=dict(type='str', required=False),
)
result = dict(
changed=False,
greeting='Sample Message',
os_version=''
)
module = AnsibleModule(
argument_spec=module_args,
supports_check_mode=True
)

# if check mode
if module.check_mode:
module.exit_json(**result)

# default name
source_name = "World"

# if message is fail, then fail
if module.params['message'] == 'fail':
module.fail_json(msg='Failing this module', **result)

if module.params['name']:
source_name = module.params['name']

result['greeting'] = module.params['message'] + " " + source_name
result['os_version'] = platform.system()+ " " + platform.release() + " " + platform.version()
module.exit_json(**result)

if __name__ == "__main__":
main()
153 changes: 152 additions & 1 deletion Chapter-15/snippets.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
- [Figure 15.15. API call returned content](#figure-1515-api-call-returned-content)
- [Figure 15.17. ToDo items fetched using API call](#figure-1517-todo-items-fetched-using-api-call)
- [Figure 15.19. Output of new items add tasks](#figure-1519-output-of-new-items-add-tasks)
- [Figure 15.23. Ansible module path](#figure-1523-ansible-module-path)
- [Figure 15.24. Library path in ansible.cfg](#figure-1524-library-path-in-ansiblecfg)
- [Figure 15.27. Ansible playbook output for custom module](#figure-1527-ansible-playbook-output-for-custom-module)
- [Figure 15.32. Custom module details using ansible-doc command](#figure-1532-custom-module-details-using-ansible-doc-command)
- [Figure 15.33. Ansible custom module documentation details using ansible-doc command.png](#figure-1533-ansible-custom-module-documentation-details-using-ansible-doc-commandpng)
- [Figure 15.35. Verify playbook execution and hello_message module](#figure-1535-verify-playbook-execution-and-hello_message-module)
- [Figure 15.37. Build Ansible collection archive](#figure-1537-build-ansible-collection-archive)
- [Figure 15.38. Publish collection to Ansible Galaxy](#figure-1538-publish-collection-to-ansible-galaxy)

## 15.2. Output of Python Installation playbook

Expand Down Expand Up @@ -231,8 +239,151 @@ ok: [localhost] => {
<omitted>...
```

## Figure 15.20. Jinja2 template to prepare Akamai API call body
## Figure 15.23. Ansible module path

```yaml
[ansible@ansible Chapter-15]$ ansible-config dump |grep DEFAULT_MODULE_PATH
DEFAULT_MODULE_PATH(default) = ['/home/ansible/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
```

## Figure 15.24. Library path in ansible.cfg

```ini
[defaults]

library = ./library
```

## Figure 15.27. Ansible playbook output for custom module

```bash
<omitted>...
TASK [debug] *************************************************************************************************************
ok: [node1] => {
"msg": {
"changed": true,
"failed": false,
"hostname": "node-1",
"msg": "Application Name: bash (version: 1.0) - This is a bash App",
"operating_system": "Linux"
}
}
<omitted>...
```

## Figure 15.32. Custom module details using ansible-doc command

```shell
[ansible@ansible Chapter-15]$ ansible-doc hello_message
> HELLO_MESSAGE (/home/ansible/ansible-book-packt/Chapter-15/library/hello_message.py)

A Hello Message Module

OPTIONS (= is mandatory):

= message
The message to be printed.

type: string

- name
The name of the person.
[Default: (null)]
type: string


AUTHOR: Gineesh Madapparambath (@ginigangadharan)

EXAMPLES:
```

## Figure 15.33. Ansible custom module documentation details using ansible-doc command.png

```shell
<omitted>...
EXAMPLES:

# Simple Custom Hello App
- name: Calling hello_message module
hello_message:
message: "Hello"
name: "John"


RETURN VALUES:
- greeting
Hello Response

returned: success
sample: Hello World
type: str

- os_version
Operating System Information
<omitted>...
```

## Figure 15.35. Verify playbook execution and hello_message module

```shell
<omitted>...
TASK [debug] *************************************************************************************************************
ok: [localhost] => {
"msg": {
"changed": false,
"failed": false,
"greeting": "Hello John",
"os_version": "Linux 4.18.0-305.el8.x86_64 #1 SMP Thu Apr 29 08:54:30 EDT 2021"
}
}
<omitted>...
```

##


```shell
collection/
├── docs/
├── galaxy.yml
├── meta/
│ └── runtime.yml
├── plugins/
│ ├── modules/
│ │ └── module1.py
│ ├── inventory/
│ └── .../
├── README.md
├── roles/
│ ├── role1/
│ ├── role2/
│ └── .../
├── playbooks/
│ ├── files/
│ ├── vars/
│ ├── templates/
│ └── tasks/
└── tests/
...<omitted>...
```

## Figure 15.37. Build Ansible collection archive

```shell
[ansible@ansible Chapter-15]$ cd collection
[ansible@ansible collection]$ ansible-galaxy collection build
Created collection for ginigangadharan.custom_modules_demo at /home/ansible/ansible-book-packt/Chapter-15/collection/ginigangadharan-custom_modules_demo-1.0.0.tar.gz
[ansible@ansible Chapter-15]$
```

## Figure 15.38. Publish collection to Ansible Galaxy

```shell
[ansible@ansible collection]$ ansible-galaxy collection publish \
> --token $ANSIBLE_GALAXY_TOKEN \
> ./ginigangadharan-custom_modules_demo-1.0.0.tar.gz
Publishing collection artifact '/home/ansible/ansible-book-packt/Chapter-15/collection/ginigangadharan-custom_modules_demo-1.0.0.tar.gz' to default https://galaxy.ansible.com/api/
Collection has been published to the Galaxy server default https://galaxy.ansible.com/api/
Waiting until Galaxy import task https://galaxy.ansible.com/api/v2/collection-imports/20104/ has completed
Collection has been successfully published and imported to the Galaxy server default https://galaxy.ansible.com/api/
```
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Figure 15.11. Test ToDo API access .png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Figure 15.13. API health check task.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Figure 15.23. Ansible module path.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Figure 15.28. Module documentation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Figure 15.30. Module RETURN block.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0ed4cb8

Please sign in to comment.