wip: ansible pull
Some checks failed
/ Ansible Lint (push) Failing after 50s
/ Ansible Lint (pull_request) Failing after 48s

This commit is contained in:
June 2025-07-23 15:27:38 +02:00
commit a7ba739ec9
Signed by: june
SSH key fingerprint: SHA256:o9EAq4Y9N9K0pBQeBTqhSDrND5E7oB+60ZNx0U1yPe0
12 changed files with 330 additions and 156 deletions

View file

@ -0,0 +1,23 @@
# `ansible_pull` role
A role for setting up automatic `ansible_pull` runs.
## Supported Distributions
Should work on Debian-based distributions.
## Required Arguments
- `ansible_pull__age_private_key`: The age private key to use to decrypt SOPS secrets with.
- `ansible_pull__repo_url`: The URL of the repo to run the playbook from.
- `ansible_pull__inventory`: The inventory to use.
- `ansible_pull__playbook`: The playbook to run.
- `ansible_pull__timer_on_calendar`: When to run the playbook. This is the argument to a systemd timers OnCalendar. See the systemd.time man page for reference.
## Optional Arguments
- `ansible_pull__user`: The user to run `ansible_pull` as. Defaults to `ansible_user`.
- `ansible_pull__checkout`: The branch/tag/commit to check out to run the playbook from. Defaults to `main`.
- `ansible_pull__timer_randomized_delay_sec`: The timer will be randomly delayed by a value between 0 and this. Useful to not have all timers fire at the same time, even if `ansible_pull__timer_on_calendar` is the same. Time value in seconds. Defaults to 0.
## Links & Resources

View file

@ -0,0 +1,3 @@
ansible_pull__user: "{{ ansible_user }}"
ansible_pull__checkout: "main"
ansible_pull__timer_randomized_delay_sec: "0"

View file

@ -0,0 +1,4 @@
- name: systemd daemon reload
ansible.builtin.systemd_service:
daemon_reload: true
become: true

View file

@ -0,0 +1,27 @@
argument_specs:
main:
options:
ansible_pull__age_private_key:
type: str
required: true
ansible_pull__repo_url:
type: str
required: true
ansible_pull__inventory:
type: str
required: true
ansible_pull__playbook:
type: str
required: true
ansible_pull__timer_on_calendar:
type: str
required: true
ansible_pull__user:
type: str
required: false
ansible_pull__checkout:
type: str
required: false
ansible_pull__timer_randomized_delay_sec:
type: str
required: false

View file

@ -0,0 +1,63 @@
- name: ensure dependencies are installed
ansible.builtin.apt:
name: virtualenv
state: present
become: true
# https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#installing-and-upgrading-ansible-with-pip
# https://www.redhat.com/en/blog/python-venv-ansible
- name: ensure Ansible installation exists
ansible.builtin.pip:
name:
- ansible
- jmespath
state: present
virtualenv: /usr/local/lib/ansible_pull_venv
become: true
- name: ensure secrets directory exists
ansible.builtin.file:
path: /etc/ansible_pull_secrets
state: directory
mode: "0750"
owner: root
group: "{{ ansible_pull__user }}"
become: true
- name: ensure age private key is deployed
ansible.builtin.copy:
content: "{{ ansible_pull__age_private_key }}"
dest: /etc/ansible_pull_secrets/age_private_key
mode: "0640"
owner: root
group: "{{ ansible_pull__user }}"
become: true
- name: ensure systemd service exists
ansible.builtin.template:
src: ansible-pull.service.j2
dest: /etc/systemd/system/ansible-pull.service
owner: root
group: root
mode: "0644"
become: true
notify:
- systemd daemon reload
- name: ensure systemd timer exists
ansible.builtin.template:
src: ansible-pull.timer.j2
dest: /etc/systemd/system/ansible-pull.timer
owner: root
group: root
mode: "0644"
become: true
notify:
- systemd daemon reload
- name: ensure systemd timer is started and enabled
ansible.builtin.systemd_service:
name: ansible-pull.timer
state: started
enabled: true
become: true

View file

@ -0,0 +1,16 @@
[Unit]
Description=ansible-pull for configuration and maintenance
After=network-online.target
Wants=network-online.target
[Service]
Type=oneshot
Environment="SOPS_AGE_KEY_FILE=/etc/ansible_pull_secrets/age_private_key"
ExecStart=/usr/local/lib/ansible_pull_venv/bin/ansible-pull \
--directory /home/chaos/ansible_pull_checkout \
--clean \
--url "{{ ansible_pull__repo_url }}" \
--checkout "{{ ansible_pull__checkout }}" \
--inventory "{{ ansible_pull__inventory }}" \
"{{ ansible_pull__playbook }}"
User={{ ansible_pull__user }}

View file

@ -0,0 +1,9 @@
[Unit]
Description=ansible-pull for configuration and maintenance on a timer
[Timer]
OnCalendar={{ ansible_pull__timer_on_calendar }}
RandomizedDelaySec={{ ansible_pull__timer_randomized_delay_sec }}
[Install]
WantedBy=timers.target