Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
x86-39 committed Jan 26, 2024
1 parent 112f4a3 commit 74f5ed1
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 84 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,6 @@ dmypy.json

# Pyre type checker
.pyre/

*.mp4
*.png
66 changes: 25 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,32 @@
Ansible Collection - diademiemi.template
Ansible Collection - diademiemi.bad_apple
========================================
Documentation for the collection template.

Contents
========

Roles
------
Role | Description | CI Status
--- | --- | ---
<!-- [diademiemi.template.EXAMPLE](./roles/template/) | Install EXAMPLE | [![Molecule test](https://github.com/diademiemi/ansible_collection_diademiemi.template/actions/workflows/ansible-role-EXAMPLE.yml/badge.svg)](https://github.com/diademiemi/ansible_collection_diademiemi.template/actions/workflows/ansible-role-EXAMPLE.yml) -->

Click on the role to see the README for that role.

Collection Structure
--------------

This collection makes use of my [Ansible Role Template repository](https://github.com/diademiemi/ansible_role_%74emplate.git). The `add-role.sh` script downloads this Template and generates a new role with the name specified. If a `molecule/default/molecule.yml` file is present, it will be ran with GitHub Actions.
<!-- I use %74 here to encode to a "t" so it doesnt get recursively replaced. The .git causes a redirect so you end up at the right URL :)-->

Usage:
Documentation for the collection bad_apple.

This is an Ansible Collection which features:
- A module `render_frame` which converts a frame of the Bad Apple video into an ASCII art representation.
- A playbook `bad-apple.yml` which:
- Installs necessary Pip packages.
- Downloads the Bad Apple video.
- Splits the video into PNG frames.
- Renders each frame into an ASCII art representation.
- Displays the ASCII art representation of each frame in the terminal.

To run the playbook, install the collection and run the playbook:
```bash
export NEW_ROLE_NAME="new_role"
./add-role.sh ${NEW_ROLE_NAME}
ansible-galaxy collection install diademiemi.bad_apple
```

Using Template
--------------
To use this template for a new role, run


OR locally
```bash
export NEW_ROLE_NAME="NEW_NAME"
export GITHUB_USER="diademiemi"
export GALAXY_API_KEY="YOUR_API_KEY"

find . -type f -exec sed -i "s/diademiemi/${GITHUB_USER}/g" {} + # Do not run this more than once
find . -type f -exec sed -i "s/template/${NEW_ROLE_NAME}/g" {} + # Do not run this more than once

# Assumes repo is named ansible_role_${NEW_ROLE_NAME}
gh secret set GALAXY_API_KEY -R ${GITHUB_USER}/ansible_collection_${GITHUB_USER}.${NEW_COLLECTION_NAME} -a actions -b ${GALAXY_API_KEY}
ansible-galaxy collection install .
```

# Remove this section from README.md
sed -i "/Using Template/Q" README.md
Then run the playbook:
```bash
ansible-playbook playbooks/bad-apple.yml -K
```
OR to skip installing Pip packages (`yt-dlp` and `Pillow`)
```bash
ansible-playbook playbooks/bad-apple.yml --skip-tags prereq
```

This is also provided as a script as `replace.sh`.
Wait 20 minutes and enjoy Bad Apple in Ansible!
4 changes: 2 additions & 2 deletions changelogs/config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
changelog_filename_template: ../CHANGELOG.rst
changelog_filename_bad_apple: ../CHANGELOG.rst
changelog_filename_version_depth: 0
changes_file: changelog.yaml
changes_format: combined
Expand Down Expand Up @@ -27,6 +27,6 @@ sections:
- Bugfixes
- - known_issues
- Known Issues
title: diademiemi.template
title: diademiemi.bad_apple
trivial_section_name: trivial
use_fqcn: true
5 changes: 5 additions & 0 deletions changelogs/fragments/major.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
major_changes:
- add render_frame module
- add bad-apple playbook

release_summary: Create Bad Apple playbook
12 changes: 6 additions & 6 deletions galaxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace: diademiemi

# The name of the collection. Has the same character restrictions as 'namespace'
name: template
name: bad_apple

# The version of the collection. Must be compatible with semantic versioning
version: 1.0.0
Expand All @@ -21,7 +21,7 @@ authors:

### OPTIONAL but strongly recommended
# A short summary description of the collection
description: your collection description
description: Bad Apple but it's Ansible

# 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'
Expand All @@ -44,16 +44,16 @@ tags:
dependencies: {}

# The URL of the originating SCM repository
repository: https://github.com/diademiemi/ansible_collection_diademiemi.template
repository: https://github.com/diademiemi/ansible_collection_diademiemi.bad_apple

# The URL to any online docs
documentation: https://github.com/diademiemi/ansible_collection_diademiemi.template
documentation: https://github.com/diademiemi/ansible_collection_diademiemi.bad_apple

# The URL to the homepage of the collection/project
homepage: https://github.com/diademiemi/ansible_collection_diademiemi.template
homepage: https://github.com/diademiemi/ansible_collection_diademiemi.bad_apple

# The URL to the collection issue tracker
issues: https://github.com/diademiemi/ansible_collection_diademiemi.template/issues
issues: https://github.com/diademiemi/ansible_collection_diademiemi.bad_apple/issues

# 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
Expand Down
61 changes: 61 additions & 0 deletions playbooks/bad-apple.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
- name: Bad Apple
hosts: localhost
tasks:
- name: Install python packages
tags: prereq
ansible.builtin.include_role:
name: diademiemi.python.pip
vars:
python_pip_user_packages:
- user: "{{ ansible_user_id }}"
packages:
- name: yt-dlp
- name: Pillow

- name: Download Video
tags: download
ansible.builtin.command: yt-dlp -f bestvideo[ext=mp4]+bestaudio[ext=m4a]/bestvideo+bestaudio https://www.youtube.com/watch?v=FtutLA63Cp8 -o bad_apple.mp4
args:
creates: bad_apple.mp4

- name: Ensure frame directory exists
tags: frames
ansible.builtin.file:
path: frames
state: directory
mode: '0755'

- name: Extract Frames
tags: frames
ansible.builtin.command: ffmpeg -i bad_apple.mp4 -vf fps=20 frames/bad_apple_%02d.png
args:
creates: frames/bad_apple_01.png

- name: Get all frames
tags: frames
ansible.builtin.find:
paths: frames
patterns: '*.png'
register: frames

- name: Render Frame
tags: render
diademiemi.bad_apple.render_frame:
input_file: "{{ item }}"
register: result
loop: "{{ frames.files | map(attribute='path') | list }}"

- name: Debug
tags: render
ansible.builtin.debug:
msg: "{{ result.results }}"

- name: Show frame
tags: render
ansible.builtin.debug:
msg: "{{ item }}"
loop: "{{ result.results | map(attribute='frame') | list }}"
loop_control:
pause: 0.0466
...
63 changes: 63 additions & 0 deletions plugins/modules/render_frame.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from ansible.module_utils.basic import AnsibleModule
from PIL import Image

def image_to_text_grid(input_image_path, grid_width=100, grid_height=30):
# Load the image
image = Image.open(input_image_path)

# Resize the image to match the grid size
image = image.resize((grid_width, grid_height))

# Convert the image to grayscale
image = image.convert('L')

# Define ASCII characters for different brightness levels
ascii_chars = " .:-=+*#%@"

# Calculate the range of each brightness level
step = 256 // len(ascii_chars)

# Create the text grid
text_grid = []
for y in range(grid_height):
line = ""
for x in range(grid_width):
brightness = image.getpixel((x, y))
char_index = min(brightness // step, len(ascii_chars) - 1)
line += ascii_chars[char_index]
text_grid.append(line)

print(text_grid)

return text_grid

def run_module():
module_args = dict(
input_file=dict(type='str', required=True),
)

result = dict(
changed=False,
message=''
)

module = AnsibleModule(
argument_spec=module_args,
supports_check_mode=True
)

input_file = module.params['input_file']

frame = image_to_text_grid(input_file)

result['msg'] = "Frame rendered to module output"
result['frame'] = frame
result['changed'] = True

module.exit_json(**result)

def main():
run_module()

if __name__ == '__main__':
main()
35 changes: 0 additions & 35 deletions replace.sh

This file was deleted.

0 comments on commit 74f5ed1

Please sign in to comment.