ansible-roles/jenkins/master/tasks/main.yml

144 lines
5.2 KiB
YAML
Raw Normal View History

---
- block:
- name: Jenkins package repository key
apt_key: url='{{ jenkins_repo_key }}'
- name: Install the jenkins stable repository
apt_repository: repo='{{ jenkins_stable_repo }}' update_cache=yes
when: not jenkins_use_latest
- name: Install the jenkins latest repository
apt_repository: repo='{{ jenkins_latest_repo }}' update_cache=yes
when: jenkins_use_latest
- name: Install jenkins
apt: pkg={{ item }} state={{ jenkins_pkg_state }} update_cache=yes cache_valid_time=3600
register: jenkins_install
with_items: '{{ jenkins_packages }}'
- name: Install some jenkins requirements
apt: pkg={{ item }} state={{ jenkins_pkg_state }} update_cache=yes cache_valid_time=3600
with_items: '{{ jenkins_package_requirements }}'
- name: install sbt launcher
copy: src={{ item }} dest=/usr/local/lib/{{ item }}
with_items: '{{ jenkins_sbt_launch_jars }}'
- name: Set the startup jenkins options
template: src=jenkins.default.j2 dest=/etc/default/jenkins owner=root group=root mode=0444
notify: Restart jenkins
- name: Ensure that jenkins is started and enabled
service: name=jenkins state=started enabled=yes
when: jenkins_install
tags: [ 'jenkins', 'jenkins_master' ]
- block:
# Handle plugins
# If Jenkins is installed or updated, wait for pulling the Jenkins CLI, assuming 10s should be sufficiant
- name: 120 seconds delay while starting Jenkins
wait_for: port={{ jenkins_http_port }} delay={{ jenkins_restart_delay }}
# Create Jenkins CLI destination directory
- name: "Create Jenkins CLI destination directory"
file: path={{ jenkins_dest }} state=directory
- name: Get Jenkins CLI
get_url: url=http://localhost:{{ jenkins_http_port }}/jnlpJars/jenkins-cli.jar dest={{ jenkins_cli_dest }} mode=0440
# Create the Jenkins administrative user password file
- name: Create the Jenkins administrative user password file
copy: content={{ jenkins_admin_pwd }} dest={{ jenkins_admin_user_pwd_file }} mode=600
- name: Install plugins without a specific version
jenkins_plugin: name="{{ item.key }}" params='{{ jenkins_access_params }}'
register: my_jenkins_plugin_unversioned
when: '"version" not in item.value'
with_dict: '{{ jenkins_plugins }}'
- name: Install plugins with a specific version
jenkins_plugin: name="{{ item.key }}" version="{{ item.value['version'] }}" params='{{ jenkins_access_params }}'
register: my_jenkins_plugin_versioned
when: '"version" in item.value'
with_dict: '{{ jenkins_plugins }}'
- name: Initiate the jenkins_restart_required fact
set_fact:
jenkins_restart_required: no
- name: Check if restart is required by any of the versioned plugins
set_fact:
jenkins_restart_required: yes
when: item.changed
with_items: '{{ my_jenkins_plugin_versioned.results }}'
- name: Check if restart is required by any of the unversioned plugins
set_fact:
jenkins_restart_required: yes
when: item.changed
with_items: '{{ my_jenkins_plugin_unversioned.results }}'
- name: Restart Jenkins if required
become_user: root
service: name=jenkins state=restarted
when: jenkins_restart_required
- name: Wait for Jenkins to start up
uri:
url: '{{ jenkins_local_url }}'
status_code: 200
timeout: 5
register: jenkins_service_status
# Keep trying for 5 mins in 5 sec intervals
retries: 60
delay: 5
until: >
'status' in jenkins_service_status and
jenkins_service_status['status'] == 200
when: jenkins_restart_required
- name: Reset the jenkins_restart_required fact
set_fact:
jenkins_restart_required: no
when: jenkins_restart_required
- name: Plugin pinning
jenkins_plugin: name="{{ item.key }}" state="{{ 'pinned' if item.value['pinned'] else 'unpinned'}}" params='{{ jenkins_access_params }}'
when: '"pinned" in item.value'
with_dict: '{{ jenkins_plugins }}'
- name: Plugin enabling
jenkins_plugin: name="{{ item.key }}" state="{{ 'enabled' if item.value['enabled'] else 'disabled'}}" params='{{ jenkins_access_params }}'
when: '"enabled" in item.value'
with_dict: '{{ jenkins_plugins }}'
become: True
become_user: '{{ jenkins_username }}'
when: jenkins_install
tags: [ 'jenkins', 'jenkins_plugins', 'jenkins_master' ]
- block:
- name: Ensure that jenkins is stoppend and disabled
service: name=jenkins state=stopped enabled=no
- name: Remove the jenkins requirements
apt: pkg={{ item }} state=absent
with_items: '{{ jenkins_package_requirements }}'
- name: Remove jenkins
apt: pkg={{ item }} state=absent
with_items: '{{ jenkins_packages }}'
- name: Remove the jenkins stable repository
apt_repository: repo='{{ jenkins_stable_repo }}' state=absent update_cache=yes
- name: Remove the jenkins latest repository
apt_repository: repo='{{ jenkins_latest_repo }}' state=absent update_cache=yes
- name: Remove the jenkins package repository key
apt_key: url='{{ jenkins_repo_key }}' state=absent
when: not jenkins_install
tags: [ 'jenkins', 'jenkins_master' ]