Skip to content

Commit

Permalink
Merge pull request #123 from linkorb/refactor-single-bulk-playbooks-0000
Browse files Browse the repository at this point in the history
Refactor single, and bulk, playbooks #0000
  • Loading branch information
mhitza authored Jan 25, 2025
2 parents bacbb42 + c8c1dc1 commit a385503
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ jobs:
# XXX ansible installed within GitHub Runner via pipx, which doesn't support direct installation from a file
# like pip does. See https://github.com/pypa/pipx/issues/934
- name: install repo-ansible dependencies
run: xargs pipx inject ansible-core jsonschema
run: pipx inject ansible-core jsonschema

- name: generate schema README table (markdown partial)
run: python3 library/schema_to_docs_partial.py

- name: run ansible playbook
run: ansible-playbook playbook-cwd.yaml
run: ansible-playbook apply.yaml
env:
ANSIBLE_DISPLAY_OK_HOSTS: 0
ANSIBLE_DISPLAY_SKIPPED_HOSTS: 0
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ ENV ANSIBLE_DISPLAY_OK_HOSTS=0
ENV ANSIBLE_DISPLAY_SKIPPED_HOSTS=0

# Set the default command to run when the container starts
CMD ansible-playbook -ilocalhost, /opt/repo-ansible/playbook-cwd.yaml
CMD ansible-playbook -ilocalhost, /opt/repo-ansible/apply.yaml
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ docker pull ghcr.io/linkorb/repo-ansible:latest
docker run --rm -v "$PWD":/app ghcr.io/linkorb/repo-ansible:latest
```

> This command will execute the repo-ansible playbook-cwd.yaml in your current directory and report on the tasks
> This command will execute the repo-ansible apply.yaml in your current directory and report on the tasks
> that reported changes throughout the execution.

Expand All @@ -58,7 +58,7 @@ $ pip3 install jsonschema


```shell
/your-repository $ ansible-playbook /tmp/repo-ansible/playbook-cwd.yaml
/your-repository $ ansible-playbook /tmp/repo-ansible/apply.yaml

PLAY [localhost] **************************************************************************

Expand Down
File renamed without changes.
4 changes: 3 additions & 1 deletion playbook-all.yaml → bulk-apply.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
- hosts: all
connection: local
strategy: free
vars:
base_path: "{{ lookup('env', 'REPO_ANSIBLE_BASE_PATH') or (playbook_dir + '/workspace') }}"
repo_path: "{{ base_path }}/{{ inventory_hostname }}"
Expand All @@ -12,6 +11,7 @@
repository: "{{ inventory_hostname }}"
destination: "{{ repo_path }}"
when: not skip_checkout
tags: checkout

- import_tasks: tasks/version-migrations.yaml
- import_tasks: tasks/retrieve-validate-repo-data.yaml
Expand Down Expand Up @@ -40,6 +40,8 @@
- import_tasks: tasks/pull-request.yaml
when: changes == 'pull-request'
tags: commit-changes

- import_tasks: tasks/push.yaml
when: changes == 'push'
tags: commit-changes
38 changes: 24 additions & 14 deletions docs/BulkOperations.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

The repository contains playbooks that can simultaneously operate across multiple repositories (e.g., `playbook-all.yaml`).
The repository contains the bulk-apply playbook that can simultaneously operate across multiple repositories.

These bulk operations require an inventory file that makes up the "hosts" upon which it should operate. In this
context, the hosts are the names of the repositories, which should be in an automatic fashion: checked out, modified per
their `repo.yaml` configuration file, and push these changes upstream.
context, the hosts are the repository names, which will be: checked out, modified per
their `repo.yaml` configuration file, and commited back upstream.

**Install the [GitHub CLI](https://cli.github.com/) and [jq](https://jqlang.github.io/jq/) dependencies.**

Expand All @@ -20,17 +20,25 @@ gh search repos --owner=linkorb --limit=1000 \
| tee generated-inventory.ini
```

> **Note** `generated-inventory.ini` is part of `.gitignore` to avoid accidental file commits.
> **Note** `generated-inventory.ini` file is part of `.gitignore` to avoid accidental file commits.
> An entry within the inventory is of the form `REPOSITORY_NAME ansible_host=localhost`
> An entry within the inventory follows the form `REPOSITORY_NAME ansible_host=localhost`
#### Run a playbook in bulk

You can selectively run the playbooks defined within this repository. For example you
could check them all out within the `./workspace/` directory (by default) using:
#### Bulk apply repo-ansible


```shell
ansible-playbook -i generated-inventory.ini bulk-apply.yaml
```

#### Running select sections of bulk-apply.yaml

For example you can check out all repositories defined in the inventory to the `./workspace/` directory using:

```shell
ansible-playbook -i generated-inventory.ini playbook-checkout.yaml
ansible-playbook -i generated-inventory.ini \
--tags checkout bulk-apply.yaml
```

> [!NOTE]
Expand All @@ -40,22 +48,24 @@ ansible-playbook -i generated-inventory.ini playbook-checkout.yaml
> `REPO_ANSIBLE_BASE_PATH` environment variable.
The changes can be committed back upstream using either pull requests or directly pushing a commit into the
default branch. Via specific playbooks:
default branch.

```shell
ansible-playbook -i generated-inventory.ini playbook-pull-request.yaml
ansible-playbook -i generated-inventory.ini \
--tags commit-changes -e changes=pull-request bulk-apply.yaml
```

```shell
ansible-playbook -i generated-inventory.ini playbook-push.yaml
ansible-playbook -i generated-inventory.ini \
--tags commit-changes -e changes=push bulk-apply.yaml
```

Alternatively, when `playbook-all.yaml` is executed, the `REPO_ANSIBLE_CHANGES` environment variable can be set to
Alternatively, when `bulk-apply.yaml` is executed, the `REPO_ANSIBLE_CHANGES` environment variable can be set to
either `pull-request`, or `push` to invoke the desired commit path to be taken (by default, this step is skipped).

#### Bulk apply across repositories and create pull requests with changes

```shell
REPO_ANSIBLE_CHANGES=pull-request \
ansible-playbook -i generated-inventory.ini playbook-all.yaml
ansible-playbook -i generated-inventory.ini bulk-apply.yaml
```
4 changes: 2 additions & 2 deletions docs/partials/readme.installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ docker pull ghcr.io/linkorb/repo-ansible:latest
docker run --rm -v "$PWD":/app ghcr.io/linkorb/repo-ansible:latest
```

> This command will execute the repo-ansible playbook-cwd.yaml in your current directory and report on the tasks
> This command will execute the repo-ansible apply.yaml in your current directory and report on the tasks
> that reported changes throughout the execution.

Expand All @@ -40,7 +40,7 @@ $ pip3 install jsonschema


```shell
/your-repository $ ansible-playbook /tmp/repo-ansible/playbook-cwd.yaml
/your-repository $ ansible-playbook /tmp/repo-ansible/apply.yaml

PLAY [localhost] **************************************************************************

Expand Down
12 changes: 0 additions & 12 deletions playbook-checkout.yaml

This file was deleted.

11 changes: 0 additions & 11 deletions playbook-pull-request.yaml

This file was deleted.

11 changes: 0 additions & 11 deletions playbook-push.yaml

This file was deleted.

2 changes: 1 addition & 1 deletion repo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ license: mit
license_year: 2024
name: repo-ansible
type: other
version: v0.11.0
version: v0.11.1
visibility: public

0 comments on commit a385503

Please sign in to comment.