-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Bas Meijer <[email protected]>
- Loading branch information
1 parent
604107f
commit 52b21a2
Showing
24 changed files
with
164 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,65 @@ | ||
# ansible_skeleton | ||
# Ansible role skeleton | ||
Skeleton for `ansible-galaxy role init` | ||
|
||
# Using the template | ||
This template is designed for desired_state convergence with molecule. | ||
|
||
`desired_state` is a variable that drives the role. | ||
`present` is the default, and absent is the alterative for `desired_state`. | ||
|
||
The template is very generic for a systemd service installed from a corresponding package. | ||
Note that the tasks are split by state, for convergence, and that verify always runs. | ||
|
||
These 2 files control most of the play: | ||
- `tasks/main.yml` | ||
- `handlers/main.yml` | ||
|
||
|
||
To try it: | ||
|
||
```sh | ||
ansible-galaxy role init httpd | ||
cd httpd | ||
molecule converge | ||
molecule converge -- -e desired_state=absent | ||
``` | ||
|
||
The same thing works for Nginx: | ||
```sh | ||
ansible-galaxy role init nginx | ||
molecule test | ||
``` | ||
|
||
# Configure first! | ||
|
||
```sh | ||
pip install --user pre-commit tox | ||
mkdir -p $HOME/git ~/.ansible/roles | ||
cd $HOME/git | ||
git clone https://github.com/playingfield/ansible_skeleton.git | ||
cd ansible_skeleton | ||
mv ansible.cfg $HOME/.ansible.cfg | ||
cd ~/.ansible/roles | ||
ansible-galaxy role init my_role | ||
git clone https://github.com/playingfield/role_skeleton.git | ||
``` | ||
Add these lines to your ~/.ansible.cfg | ||
```ini | ||
[galaxy] | ||
role_skeleton = ~/git/role_skeleton | ||
role_skeleton_ignore = ^.git$,^.*/.git_keep$,README.md | ||
``` | ||
|
||
## matrix testing in a generated role | ||
|
||
```sh | ||
pip install --user tox | ||
# edit tox.ini for a relevant setup | ||
tox | ||
cd my_role && git init && git add . | ||
``` | ||
|
||
## pre-commit hooks in a generated role | ||
|
||
```sh | ||
pip install --user pre-commit tox | ||
# edit .pre-commit-config.yaml for a relevant setup | ||
git init | ||
pre-commit install | ||
git add . | ||
commit -m "initial commit" | ||
pre-commit run --all-files | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
; https://docs.ansible.com/ansible/latest/installation_guide/intro_configuration.html | ||
[defaults] | ||
host_key_checking = False | ||
inventory = ~/.ansible_hosts.yml | ||
|
||
[galaxy] | ||
role_skeleton = ~/git/ansible_skeleton/role | ||
role_skeleton_ignore = ^.git$,^.*/.git_keep$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
# | ||
# https://ansible-lint.readthedocs.io/en/latest/default_rules/ | ||
exclude_paths: | ||
- ./.tox | ||
skip_list: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
--- | ||
# https://yamllint.readthedocs.io/en/stable/rules.html | ||
extends: default | ||
ignore: | | ||
.tox/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
--- | ||
# defaults file for {{ role_name }} | ||
--- | ||
desired_state: present | ||
package: {{ role_name }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
# https://docs.ansible.com/ansible/latest/user_guide/playbooks_handlers.html | ||
- name: Control service | ||
ansible.builtin.systemd: | ||
name: "{{ service }}" | ||
state: "{{ systemd_state }}" | ||
enabled: "{{ systemd_enabled }}" | ||
daemon_reload: true |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Tasks to converge to state absent | ||
--- | ||
- name: flush handlers | ||
ansible.builtin.meta: flush_handlers | ||
|
||
- name: Remove package | ||
ansible.builtin.package: | ||
name: "{{ package }}" | ||
state: absent |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
--- | ||
# main tasks file for role | ||
# Main tasks entry point for the role | ||
- name: "State variables - {{ desired_state }}" | ||
ansible.builtin.include_vars: "{{ desired_state }}.yml" | ||
|
||
- name: "Converge state - {{ desired_state }}" | ||
include_tasks: "{{ desired_state }}.yml" | ||
ansible.builtin.include_tasks: "{{ desired_state }}.yml" | ||
|
||
- name: Flush handlers | ||
ansible.builtin.meta: flush_handlers | ||
|
||
- name: "Verify state - {{ desired_state }}" | ||
include_tasks: verify.yml | ||
ansible.builtin.include_tasks: verify.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
--- | ||
# tasks file for {{ role_name }} | ||
# prepare tasks file for {{ role_name }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Tasks to reach desired state present | ||
--- | ||
- name: Manage package | ||
ansible.builtin.package: | ||
name: "{{ package }}" | ||
state: "{{ desired_state }}" | ||
update_cache: true | ||
notify: Control service | ||
|
||
- name: Enable service | ||
ansible.builtin.systemd: | ||
name: "{{ service }}" | ||
enabled: true | ||
daemon_reload: true | ||
notify: Control service |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Verify desired state, both absent and present can be verified. | ||
--- | ||
- name: Gather package facts | ||
ansible.builtin.package_facts: | ||
|
||
- name: Assert package is installed | ||
ansible.builtin.assert: | ||
quiet: true | ||
that: | ||
- "'{{ package }}' {{ package_clause }} ansible_facts.packages" | ||
|
||
- name: Gather service facts | ||
ansible.builtin.service_facts: | ||
|
||
- name: Assert service absence | ||
when: desired_state == 'absent' | ||
ansible.builtin.assert: | ||
quiet: true | ||
that: | ||
- ansible_facts.services['{{ service }}'] is not defined | ||
|
||
- name: Test the service | ||
when: desired_state == 'present' | ||
block: | ||
- name: Assert that service is OK | ||
ansible.builtin.assert: | ||
quiet: true | ||
that: | ||
- ansible_facts.services['{{ service~'.service' }}'].status == 'enabled' | ||
- ansible_facts.services['{{ service~'.service' }}'].state == 'running' | ||
|
||
- name: Verify service_port is listening | ||
ansible.builtin.wait_for: | ||
port: "{{ service_port }}" | ||
state: started | ||
timeout: 10 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Variable values for desired_state: absent | ||
--- | ||
package_clause: "not in" | ||
service_state: stopped | ||
systemd_state: stopped | ||
systemd_enabled: false | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# vars file for | ||
--- | ||
service: "{{ package }}" | ||
service_port: 80 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Variable values for desired_state: present | ||
--- | ||
package_clause: "in" | ||
service_state: running | ||
systemd_state: started | ||
systemd_enabled: true | ||
... |