Skip to content

Commit d324193

Browse files
Merge pull request #35 from MonolithProjects/develop
Role Idempotency
2 parents a5d0d67 + 6678230 commit d324193

File tree

6 files changed

+41
-13
lines changed

6 files changed

+41
-13
lines changed

.github/workflows/lifecycle.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
with:
1919
path: "${{ github.repository }}"
2020
- name: Molecule for Ansible - lint
21-
uses: MonolithProjects/action-molecule@v1.2.0
21+
uses: MonolithProjects/action-molecule@v1.3.0
2222
with:
2323
molecule_command: lint
2424

@@ -33,15 +33,15 @@ jobs:
3333
with:
3434
path: "${{ github.repository }}"
3535
- name: Molecule for Ansible - converge Default
36-
uses: MonolithProjects/action-molecule@v1.2.0
36+
uses: MonolithProjects/action-molecule@v1.3.0
3737
env:
3838
PERSONAL_ACCESS_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
3939
with:
4040
molecule_command: converge
4141
- name: Molecule for Ansible - converge tag uninstall
42-
uses: MonolithProjects/action-molecule@v1.2.0
42+
uses: MonolithProjects/action-molecule@v1.3.0
4343
env:
4444
PERSONAL_ACCESS_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
4545
with:
4646
molecule_command: converge
47-
converge_tags: uninstall
47+
converge_extra_args: '-e "uninstall_runner=yes" --tags uninstall'

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ jobs:
1717
with:
1818
path: "${{ github.repository }}"
1919
- name: Molecule for Ansible
20-
uses: MonolithProjects/action-molecule@v1.2.0
20+
uses: MonolithProjects/action-molecule@v1.3.0
2121
env:
2222
PERSONAL_ACCESS_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }}

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ This role will deploy/redeploy/uninstall and register/unregister local GitHub Ac
1313
* System must have access to the GitHub.
1414

1515
* The role require Personal Access Token to access the GitHub. The token has to be a value of `PERSONAL_ACCESS_TOKEN` variable.
16-
Export the token to the local host environment. The token has to have admin rights for the repo.
17-
Personal Access Token for GitHub account can be created [here](https://github.com/settings/tokens).
16+
Export the token to the local host environment. The token has to have admin rights for the repo.
17+
Personal Access Token for GitHub account can be created [here](https://github.com/settings/tokens).
1818
**Note:** Never store you personal access token in the GitHub repository. Use [GitHub Secrets](https://help.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets) or some different secrets service.
1919

20-
* Runner user has to be pre-created.
20+
* Runner user has to be pre-created.
2121
Recommended role: `monolithprojects.user_management`
2222

23-
* CentOS systems require EPEL repository.
23+
* CentOS systems require EPEL repository.
2424
Recommended role: `robertdebock.epel`
2525

2626
* Weekly tested on:
@@ -48,6 +48,9 @@ runner_version: "latest"
4848
# If found, replace already registered runner
4949
replace_runner: yes
5050

51+
# If found, delete already existed runner before install
52+
uninstall_runner: no
53+
5154
# Do not show Ansible logs which may contain sensitive data (registration token)
5255
hide_sensitive_logs: yes
5356

@@ -98,10 +101,10 @@ In this example the Ansible role will deploy (or redeploy) the GitHub Actions ru
98101
- role: monolithprojects.github_actions_runner
99102
```
100103

101-
By using tag `uninstall`, GitHub Actions runner will be removed from the host and unregistered from the GitHub repository.
104+
By using tag `uninstall` with combination of variable `uninstall_runner: yes`, GitHub Actions runner will be removed from the host and unregistered from the GitHub repository.
102105

103106
```bash
104-
ansible-playbook playbook.yml --tags uninstall
107+
ansible-playbook playbook.yml --tags uninstall -e "uninstall_runner=yes"
105108
```
106109

107110
## License

defaults/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ runner_version: "latest"
1111
# If found, replace already registered runner
1212
replace_runner: yes
1313

14+
# If found, delete already existed runner before install
15+
uninstall_runner: no
16+
1417
# Do not show Ansible logs which may contain sensitive data (registration token)
1518
hide_sensitive_logs: yes
1619

tasks/install_runner.yml

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,44 @@
3434
runner_version: "{{ api_response.json.tag_name | regex_replace('^v', '') }}"
3535
when: runner_version == "latest"
3636

37+
- name: Check if desired version already installed
38+
command: "grep -i {{ runner_version }} {{ runner_dir }}/bin/Runner.Listener.deps.json"
39+
register: runner_installed
40+
changed_when: False
41+
ignore_errors: yes
42+
tags:
43+
- install
44+
45+
- name: Create temporary directory for archive
46+
tempfile:
47+
state: directory
48+
register: tempdir
49+
run_once: yes
50+
delegate_to: localhost
51+
become: false
52+
when: runner_version not in runner_installed.stdout
53+
tags:
54+
- install
55+
3756
- name: Download runner package version - "{{ runner_version }}" (RUN ONCE)
3857
get_url:
3958
url: "https://github.com/actions/runner/releases/download/v{{ runner_version }}/actions-runner-linux-x64-{{ runner_version }}.tar.gz"
40-
dest: "./actions-runner-linux-{{ runner_version }}.tar.gz"
59+
dest: "{{ tempdir.path }}/actions-runner-linux-{{ runner_version }}.tar.gz"
4160
force: no
4261
run_once: yes
4362
become: false
4463
delegate_to: localhost
64+
when: runner_version not in runner_installed.stdout
4565
tags:
4666
- install
4767

4868
- name: Unarchive package
4969
unarchive:
50-
src: "./actions-runner-linux-{{ runner_version }}.tar.gz"
70+
src: "{{ tempdir.path }}/actions-runner-linux-{{ runner_version }}.tar.gz"
5171
dest: "{{ runner_dir }}/"
5272
owner: "{{ runner_user }}"
5373
mode: 0755
74+
when: runner_version not in runner_installed.stdout
5475
tags:
5576
- install
5677

tasks/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- install
1010

1111
- include_tasks: uninstall_runner.yml
12+
when: uninstall_runner
1213
tags:
1314
- uninstall
1415

0 commit comments

Comments
 (0)