Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions ansible/roles/gocd-agent/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
# GoCD server URL (must be set in your playbook or inventory)
# gocd_server_url: "https://gocd.example.com:8154/go"

# GoCD agent auto-registration key (optional, for auto-registration)
# gocd_agent_auto_register_key: ""

# GoCD agent auto-registration resources (optional)
# gocd_agent_auto_register_resources: ""

# GoCD agent auto-registration environments (optional)
# gocd_agent_auto_register_environments: ""

# GoCD agent auto-registration hostname (optional)
# gocd_agent_auto_register_hostname: "{{ ansible_hostname }}"

# Additional environment variables for the agent
gocd_agent_environment: {}

# Installation method - set to true to install from .deb file instead of apt repository
gocd_install_from_deb: false

# URL to the .deb package (used when gocd_install_from_deb is true)
gocd_deb_version: 23.1.0-16079
gocd_deb_url: "https://download.gocd.org/binaries/{{ gocd_deb_version }}/deb/go-agent_{{ gocd_deb_version }}_all.deb"

# Number of GoCD agents to run on this host (default: 1)
# Set to a number > 1 to run multiple agents on the same host
gocd_agent_count: 1
14 changes: 14 additions & 0 deletions ansible/roles/gocd-agent/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
- name: Restart go-agent
ansible.builtin.systemd:
name: go-agent
state: restarted
daemon_reload: true

- name: Restart additional go-agents
ansible.builtin.systemd:
name: go-agent-{{ item }}
state: restarted
daemon_reload: true
loop: "{{ range(2, gocd_agent_count + 1) | list }}"
when: gocd_agent_count > 1
93 changes: 93 additions & 0 deletions ansible/roles/gocd-agent/tasks/configure-multiple-agents.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
---
# Configure additional GoCD agent instances (agent-2, agent-3, etc.)
- name: Create directories for additional GoCD agents
ansible.builtin.file:
path: "{{ item[0] }}-{{ item[1] }}"
state: directory
owner: go
group: go
mode: 0755
loop: "{{ ['/usr/share/go-agent', '/var/lib/go-agent', '/var/log/go-agent'] | product(range(2, gocd_agent_count + 1)) }}"
when: gocd_agent_count > 1

- name: Copy go-agent binaries and configs to additional agent directories
ansible.builtin.shell: |
cp -r /usr/share/go-agent/* /usr/share/go-agent-{{ item }}/
chown -R go:go /usr/share/go-agent-{{ item }}
args:
creates: /usr/share/go-agent-{{ item }}/bin
loop: "{{ range(2, gocd_agent_count + 1) | list }}"
when: gocd_agent_count > 1

- name: Update wrapper-properties.conf for additional agents
ansible.builtin.replace:
path: /usr/share/go-agent-{{ item }}/wrapper-config/wrapper-properties.conf
regexp: '/var/lib/go-agent'
replace: '/var/lib/go-agent-{{ item }}'
loop: "{{ range(2, gocd_agent_count + 1) | list }}"
when: gocd_agent_count > 1

- name: Update wrapper-properties.conf log directory for additional agents
ansible.builtin.replace:
path: /usr/share/go-agent-{{ item }}/wrapper-config/wrapper-properties.conf
regexp: '/var/log/go-agent'
replace: '/var/log/go-agent-{{ item }}'
loop: "{{ range(2, gocd_agent_count + 1) | list }}"
when: gocd_agent_count > 1

- name: Configure GoCD server URL in wrapper-properties.conf for additional agents
ansible.builtin.lineinfile:
path: /usr/share/go-agent-{{ item }}/wrapper-config/wrapper-properties.conf
regexp: '^wrapper\.app\.parameter\.101='
line: 'wrapper.app.parameter.101={{ gocd_server_url }}'
backrefs: false
loop: "{{ range(2, gocd_agent_count + 1) | list }}"
when: gocd_agent_count > 1 and gocd_server_url is defined
notify: Restart additional go-agents

- name: Update wrapper.conf for additional agents
ansible.builtin.replace:
path: /usr/share/go-agent-{{ item }}/wrapper-config/wrapper.conf
regexp: '/var/lib/go-agent'
replace: '/var/lib/go-agent-{{ item }}'
loop: "{{ range(2, gocd_agent_count + 1) | list }}"
when: gocd_agent_count > 1

- name: Create systemd service files for additional agents
ansible.builtin.template:
src: go-agent.service.j2
dest: /etc/systemd/system/go-agent-{{ item }}.service
mode: 0644
loop: "{{ range(2, gocd_agent_count + 1) | list }}"
when: gocd_agent_count > 1
notify: Restart additional go-agents

- name: Create config directories for additional agents
ansible.builtin.file:
path: /var/lib/go-agent-{{ item }}/config
state: directory
owner: go
group: go
mode: 0755
loop: "{{ range(2, gocd_agent_count + 1) | list }}"
when: gocd_agent_count > 1

- name: Configure auto-registration for additional agents
ansible.builtin.template:
src: autoregister.properties.j2
dest: /var/lib/go-agent-{{ item }}/config/autoregister.properties
owner: go
group: go
mode: 0600
loop: "{{ range(2, gocd_agent_count + 1) | list }}"
when: gocd_agent_count > 1 and gocd_agent_auto_register_key is defined
notify: Restart additional go-agents

- name: Enable and start additional go-agent services
ansible.builtin.systemd:
name: go-agent-{{ item }}
enabled: true
state: started
daemon_reload: true
loop: "{{ range(2, gocd_agent_count + 1) | list }}"
when: gocd_agent_count > 1
98 changes: 98 additions & 0 deletions ansible/roles/gocd-agent/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
---
# Installation from apt repository
- name: Ensure /etc/apt/keyrings directory exists
ansible.builtin.file:
path: /etc/apt/keyrings
state: directory
mode: 0755
when: not gocd_install_from_deb

- name: Download GoCD GPG key
ansible.builtin.get_url:
url: https://download.gocd.org/GOCD-GPG-KEY.asc
dest: /tmp/gocd-key.asc
mode: 0644
when: not gocd_install_from_deb

- name: Add GoCD GPG key to keyring
ansible.builtin.shell: |
gpg --dearmor -o /etc/apt/keyrings/gocd.gpg < /tmp/gocd-key.asc
chmod a+r /etc/apt/keyrings/gocd.gpg
args:
creates: /etc/apt/keyrings/gocd.gpg
when: not gocd_install_from_deb

- name: Add GoCD apt repository
ansible.builtin.apt_repository:
repo: 'deb [signed-by=/etc/apt/keyrings/gocd.gpg] https://download.gocd.org /'
state: present
filename: gocd
when: not gocd_install_from_deb

- name: Update apt cache
ansible.builtin.apt:
update_cache: true
cache_valid_time: 3600
when: not gocd_install_from_deb

- name: Install go-agent package from repository
ansible.builtin.apt:
name: go-agent
state: present
install_recommends: true
when: not gocd_install_from_deb

# Installation from .deb file
- name: Download go-agent .deb package
ansible.builtin.get_url:
url: "{{ gocd_deb_url }}"
dest: /tmp/go-agent.deb
mode: 0644
when: gocd_install_from_deb

- name: Install go-agent from .deb package
ansible.builtin.apt:
deb: /tmp/go-agent.deb
state: present
when: gocd_install_from_deb

- name: Configure GoCD server URL in /etc/default/go-agent
ansible.builtin.lineinfile:
path: /etc/default/go-agent
regexp: '^GO_SERVER_URL='
line: 'GO_SERVER_URL={{ gocd_server_url }}'
create: true
mode: 0644
when: gocd_server_url is defined and not gocd_install_from_deb
notify: Restart go-agent

- name: Configure GoCD server URL in wrapper-properties.conf
ansible.builtin.lineinfile:
path: /usr/share/go-agent/wrapper-config/wrapper-properties.conf
regexp: '^wrapper\.app\.parameter\.101='
line: 'wrapper.app.parameter.101={{ gocd_server_url }}'
backrefs: false
when: gocd_server_url is defined and gocd_install_from_deb
notify: Restart go-agent

- name: Configure auto-registration properties
ansible.builtin.template:
src: autoregister.properties.j2
dest: /var/lib/go-agent/config/autoregister.properties
owner: go
group: go
mode: 0600
when: gocd_agent_auto_register_key is defined
notify: Restart go-agent

- name: Enable and start go-agent service
ansible.builtin.systemd:
name: go-agent
enabled: true
state: started
daemon_reload: true

# Configure additional agents if gocd_agent_count > 1
- name: Include tasks for multiple agents
ansible.builtin.include_tasks: configure-multiple-agents.yml
when: gocd_agent_count > 1
13 changes: 13 additions & 0 deletions ansible/roles/gocd-agent/templates/autoregister.properties.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# GoCD Agent Auto-registration Configuration
# This file is managed by Ansible - manual changes will be overwritten

agent.auto.register.key={{ gocd_agent_auto_register_key }}
{% if gocd_agent_auto_register_resources is defined and gocd_agent_auto_register_resources %}
agent.auto.register.resources={{ gocd_agent_auto_register_resources }}
{% endif %}
{% if gocd_agent_auto_register_environments is defined and gocd_agent_auto_register_environments %}
agent.auto.register.environments={{ gocd_agent_auto_register_environments }}
{% endif %}
{% if gocd_agent_auto_register_hostname is defined and gocd_agent_auto_register_hostname %}
agent.auto.register.hostname={{ gocd_agent_auto_register_hostname }}
{% endif %}
18 changes: 18 additions & 0 deletions ansible/roles/gocd-agent/templates/go-agent.service.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[Unit]
Description=GoCD Agent {{ item }}
Documentation=https://docs.gocd.org
After=network.target

[Service]
Type=forking
User=go
Group=go
WorkingDirectory=/var/lib/go-agent-{{ item }}
PIDFile=/var/run/go-agent-{{ item }}/go-agent.pid
ExecStart=/usr/share/go-agent-{{ item }}/bin/go-agent start
ExecStop=/usr/share/go-agent-{{ item }}/bin/go-agent stop
Restart=on-failure
RestartSec=10

[Install]
WantedBy=multi-user.target