|
| 1 | +--- |
| 2 | +# Copyright (c) 2023 Oracle and/or its affiliates. |
| 3 | +# |
| 4 | +# The Universal Permissive License (UPL), Version 1.0 |
| 5 | +# |
| 6 | +# Subject to the condition set forth below, permission is hereby granted to any |
| 7 | +# person obtaining a copy of this software, associated documentation and/or data |
| 8 | +# (collectively the "Software"), free of charge and under any and all copyright |
| 9 | +# rights in the Software, and any and all patent rights owned or freely |
| 10 | +# licensable by each licensor hereunder covering either (i) the unmodified |
| 11 | +# Software as contributed to or provided by such licensor, or (ii) the Larger |
| 12 | +# Works (as defined below), to deal in both |
| 13 | +# |
| 14 | +# (a) the Software, and |
| 15 | +# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 16 | +# one is included with the Software (each a "Larger Work" to which the Software |
| 17 | +# is contributed by such licensors), |
| 18 | +# without restriction, including without limitation the rights to copy, create |
| 19 | +# derivative works of, display, perform, and distribute the Software and make, |
| 20 | +# use, sell, offer for sale, import, export, have made, and have sold the |
| 21 | +# Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 22 | +# either these or other terms. |
| 23 | +# |
| 24 | +# This license is subject to the following condition: |
| 25 | +# The above copyright notice and either this complete permission notice or at |
| 26 | +# a minimum a reference to the UPL must be included in all copies or |
| 27 | +# substantial portions of the Software. |
| 28 | +# |
| 29 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 30 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 31 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 32 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 33 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 34 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 35 | +# SOFTWARE. |
| 36 | + |
| 37 | +- name: Install and configure Jenkins |
| 38 | + hosts: all |
| 39 | + remote_user: opc |
| 40 | +# collections: |
| 41 | +# - oracle.oci |
| 42 | + vars: |
| 43 | + jenkins_port: "{{ jenkins_port }}" |
| 44 | + jenkins_casc_path: /var/lib/jenkins/jenkins_config.yaml |
| 45 | + jenkins_plugins: "configuration-as-code job-dsl github credentials workflow-multibranch workflow-aggregator pipeline-stage-view git oracle-cloud-infrastructure-devops oracle-cloud-infrastructure-compute bouncycastle-api ssh-credentials" |
| 46 | + jenkins_admin_pwd: "{{ jenkins_admin_pwd }}" |
| 47 | + |
| 48 | + |
| 49 | + tasks: |
| 50 | + - set_fact: |
| 51 | + public_ip: "{{ instance_host }}" |
| 52 | + - name: Instance public ip |
| 53 | + debug: |
| 54 | + var: public_ip |
| 55 | + - block: |
| 56 | + - name: Add Jenkins yum repository |
| 57 | + ansible.builtin.yum_repository: |
| 58 | + name: jenkins-rpm-lts |
| 59 | + description: Jenkins RPM packages |
| 60 | + baseurl: http://pkg.jenkins.io/redhat-stable |
| 61 | + become: true |
| 62 | + |
| 63 | + - name: Import jenkins key |
| 64 | + ansible.builtin.rpm_key: |
| 65 | + state: present |
| 66 | + key: https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key |
| 67 | + become: true |
| 68 | + |
| 69 | + - name: yum update |
| 70 | + yum: |
| 71 | + name: '*' |
| 72 | + state: latest |
| 73 | + become: true |
| 74 | + |
| 75 | + - name: Install git |
| 76 | + yum: |
| 77 | + name: git |
| 78 | + state: present |
| 79 | + become: true |
| 80 | + |
| 81 | + - name: Install java |
| 82 | + yum: |
| 83 | + name: java-17-openjdk |
| 84 | + state: present |
| 85 | + become: true |
| 86 | + |
| 87 | + - name: Install jenkins dependencies |
| 88 | + yum: |
| 89 | + name: fontconfig |
| 90 | + state: present |
| 91 | + become: true |
| 92 | + |
| 93 | + - name: Install Jenkins |
| 94 | + ansible.builtin.yum: |
| 95 | + name: jenkins |
| 96 | + state: latest |
| 97 | + become: true |
| 98 | + |
| 99 | + - name: Start jenkins |
| 100 | + ansible.builtin.systemd: |
| 101 | + daemon_reload: yes |
| 102 | + enabled: true |
| 103 | + name: jenkins |
| 104 | + state: started |
| 105 | + become: true |
| 106 | + |
| 107 | + - name: Get Jenkins CLI |
| 108 | + get_url: |
| 109 | + url: http://localhost:8080/jnlpJars/jenkins-cli.jar |
| 110 | + dest: /home/opc/jenkins-cli.jar |
| 111 | + mode: "0777" |
| 112 | + |
| 113 | + - name: Get initial admin password |
| 114 | + command: "cat /var/lib/jenkins/secrets/initialAdminPassword" |
| 115 | + register: result |
| 116 | + become: true |
| 117 | + - set_fact: |
| 118 | + initial_admin_pass: "{{ result.stdout }}" |
| 119 | + |
| 120 | + - name: Check if plugins folder is empty before proceeding |
| 121 | + find: |
| 122 | + paths: '/var/lib/jenkins/plugins/' |
| 123 | + register: pluginsFound |
| 124 | + |
| 125 | + - name: Install plugins |
| 126 | + shell: | |
| 127 | + java -jar jenkins-cli.jar -s http://127.0.0.1:{{ jenkins_port }}/ -auth admin:{{ initial_admin_pass }} install-plugin {{ jenkins_plugins }} |
| 128 | + when: pluginsFound.matched == 0 # Only install the plugin with default admin password if it is a first installation |
| 129 | + |
| 130 | + - name: Copy Jenkins CasC configs |
| 131 | + template: |
| 132 | + src: ./templates/jenkins_config.yaml.j2 |
| 133 | + dest: "{{ jenkins_casc_path }}" |
| 134 | + owner: opc |
| 135 | + group: opc |
| 136 | + mode: '0644' |
| 137 | + become: true |
| 138 | + |
| 139 | + - name: Create jenkins.service.d directory |
| 140 | + file: |
| 141 | + path: /etc/systemd/system/jenkins.service.d/ |
| 142 | + state: directory |
| 143 | + owner: root |
| 144 | + group: root |
| 145 | + mode: 0755 |
| 146 | + become: true |
| 147 | + |
| 148 | + - name: Copy jenkins.service drop-in |
| 149 | + template: |
| 150 | + src: ./templates/jenkins.service.j2 |
| 151 | + dest: /etc/systemd/system/jenkins.service.d/override.conf |
| 152 | + owner: root |
| 153 | + group: root |
| 154 | + mode: 0644 |
| 155 | + become: true |
| 156 | + |
| 157 | + - name: Install plugins |
| 158 | + shell: | |
| 159 | + java -jar jenkins-cli.jar -s http://127.0.0.1:{{ jenkins_port }}/ -auth admin:{{ jenkins_admin_pwd }} install-plugin {{ jenkins_plugins }} |
| 160 | + when: pluginsFound.matched > 0 |
| 161 | + |
| 162 | + - name: Restart Jenkins |
| 163 | + systemd: |
| 164 | + daemon_reload: yes |
| 165 | + name: jenkins |
| 166 | + state: restarted |
| 167 | + become: true |
| 168 | + |
| 169 | + - name: Add firewall rules |
| 170 | + shell: | |
| 171 | + firewall-cmd --permanent --zone=public --add-service=jenkins |
| 172 | + firewall-cmd --zone=public --add-port=50000/tcp --permanent |
| 173 | + firewall-cmd --reload |
| 174 | + become: true |
| 175 | + |
| 176 | + rescue: |
| 177 | + - import_tasks: rollback.yaml |
| 178 | + |
0 commit comments