k8s/roles/gitlab_agent/tasks/config.yml
2026-07-15 11:14:21 +03:00

88 lines
2.6 KiB
YAML

---
- name: Agent config | Verify GITLAB_FLUX_TOKEN is set
ansible.builtin.assert:
that:
- gitlab_agent_fleet_token | length > 0
fail_msg: "GITLAB_FLUX_TOKEN environment variable is not set — cannot push agent config to fleet repo"
- name: Agent config | Create temp directory for fleet repo clone
ansible.builtin.tempfile:
state: directory
suffix: fleet
register: _fleet_tmpdir
- name: Agent config | Clone fleet repo
ansible.builtin.git:
repo: "https://oauth2:{{ gitlab_agent_fleet_token }}@{{ gitlab_agent_hostname }}/k8s/k8s-fleet.git"
dest: "{{ _fleet_tmpdir.path }}/k8s-fleet"
version: "{{ gitlab_agent_fleet_branch }}"
depth: 1
no_log: true
changed_when: true
- name: Agent config | Create agent config directory
ansible.builtin.file:
path: "{{ _fleet_tmpdir.path }}/k8s-fleet/.gitlab/agents/{{ gitlab_agent_name }}"
state: directory
mode: "0755"
- name: Agent config | Write agent config.yaml
ansible.builtin.copy:
dest: "{{ _fleet_tmpdir.path }}/k8s-fleet/.gitlab/agents/{{ gitlab_agent_name }}/config.yaml"
mode: "0644"
content: |
gitops:
reconcile_timeout: 3600s
observability:
logging:
level: info
ci_access:
groups:
- id: {{ gitlab_agent_ci_access_group }}
{% if gitlab_agent_ci_access_projects | length > 0 %}
projects:
{% for project in gitlab_agent_ci_access_projects %}
- id: {{ project }}
{% endfor %}
{% endif %}
register: _agent_config
- name: Agent config | Commit and push if changed
when: _agent_config.changed
block:
- name: Agent config | Configure git user
ansible.builtin.git:
repo: "{{ _fleet_tmpdir.path }}/k8s-fleet"
user_name: "Ansible"
user_email: "ansible@{{ gitlab_agent_hostname }}"
changed_when: false
- name: Agent config | Stage agent config
ansible.builtin.git:
repo: "{{ _fleet_tmpdir.path }}/k8s-fleet"
add:
- ".gitlab/agents/{{ gitlab_agent_name }}/config.yaml"
changed_when: false
- name: Agent config | Commit
ansible.builtin.git:
repo: "{{ _fleet_tmpdir.path }}/k8s-fleet"
commit:
msg: "feat: add GitLab agent config for {{ gitlab_agent_name }}"
changed_when: true
- name: Agent config | Push
ansible.builtin.git:
repo: "{{ _fleet_tmpdir.path }}/k8s-fleet"
push: true
branch: "{{ gitlab_agent_fleet_branch }}"
no_log: true
changed_when: true
- name: Agent config | Remove temp directory
ansible.builtin.file:
path: "{{ _fleet_tmpdir.path }}"
state: absent