|
| 1 | +# Copyright (c) 2021, 2022 Oracle and/or its affiliates. All rights reserved. |
| 2 | +# Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl. |
| 3 | +# |
| 4 | +--- |
| 5 | + |
| 6 | +# validations |
| 7 | +- hosts: target_hosts |
| 8 | + vars: |
| 9 | + # common vars |
| 10 | + tasks: |
| 11 | + - name: collect facts about system services |
| 12 | + service_facts: |
| 13 | + |
| 14 | + - name: Check if the mgmt_agent service is already installed |
| 15 | + fail: |
| 16 | + msg: "mgmt_agent is already installed" |
| 17 | + when: "'mgmt_agent.service' in services" |
| 18 | + |
| 19 | + - name: check java executable is present on remote host |
| 20 | + become: yes |
| 21 | + become_user: root |
| 22 | + command: |
| 23 | + "which java" |
| 24 | + register: java_result |
| 25 | + tags: check-java-exists |
| 26 | + - debug: |
| 27 | + msg="{{java_result.stdout_lines}}" |
| 28 | + verbosity=1 |
| 29 | + |
| 30 | + - name: Checking Java version |
| 31 | + shell: "java -version 2>&1 | awk -F '\"' '/version/ {print $2}' | head -c3" |
| 32 | + register: java_version |
| 33 | + - debug: |
| 34 | + msg="{{java_version.stdout_lines[0]}}" |
| 35 | + verbosity=1 |
| 36 | + |
| 37 | + - name: Check if Java Version is greater than 1.8 |
| 38 | + fail: |
| 39 | + msg: "Java version is less than 1.8, Exiting.." |
| 40 | + when: java_version.stdout_lines[0]|float < 1.8 |
| 41 | + |
| 42 | +# workflows |
| 43 | +- hosts: localhost |
| 44 | + collections: |
| 45 | + - oracle.oci |
| 46 | + tasks: |
| 47 | + - name: Check pre-requisites |
| 48 | + fail: |
| 49 | + msg: "Environment variable {{item}} not set. Please declare an environment variable with an appropriate value for the sample to work." |
| 50 | + when: item not in ansible_env |
| 51 | + with_items: |
| 52 | + - "compartment_ocid" |
| 53 | + |
| 54 | + - name: Create a directory if it does not exist |
| 55 | + ansible.builtin.file: |
| 56 | + path: "./{{remote_mgmt_agent_scratch_dir}}" |
| 57 | + state: directory |
| 58 | + mode: '0755' |
| 59 | + |
| 60 | + - name: Create management_agent_install_key |
| 61 | + oci_management_agent_install_key: |
| 62 | + # required |
| 63 | + compartment_id: "{{compartment_ocid}}" |
| 64 | + display_name: "{{mgmt_agent_install_key_name}}" |
| 65 | + # optional |
| 66 | + #allowed_key_install_count: 56 |
| 67 | + #time_expires: time_expires_example |
| 68 | + is_unlimited: true |
| 69 | + register: key_result |
| 70 | + - debug: |
| 71 | + msg="{{key_result.management_agent_install_key}}" |
| 72 | + - set_fact: |
| 73 | + management_agent_install_key: "{{key_result.management_agent_install_key.key}}" |
| 74 | + |
| 75 | + - name: Creating management agent input.rsp file |
| 76 | + copy: |
| 77 | + content: "######################################################################## \n |
| 78 | + # Please refer the following Management Agent Installation Guide for more details. \n |
| 79 | + # \n |
| 80 | + # https://docs.cloud.oracle.com/iaas/management-agents/index.html \n |
| 81 | + #\n |
| 82 | + # Since this file has sensitive information, please make sure that after \n |
| 83 | + # executing setup.sh you either delete this file or store it in a secure \n |
| 84 | + # location. \n |
| 85 | + # \n |
| 86 | + ######################################################################## \n |
| 87 | + ManagementAgentInstallKey = {{management_agent_install_key}} \n |
| 88 | + AgentDisplayName = \n |
| 89 | + #Please uncomment the below tags properties and provide values as needed \n |
| 90 | + #FreeFormTags = [{\"<key1>\":\"<value1>\"}, {\"<key2>\":\"<value2>\"}]\n |
| 91 | + #DefinedTags = [{\"namespace1\":{\"<key1>\":\"<value1>\"}}, {\"namespace2\":{\"<key2>\":\"<value2>\"}}]\n |
| 92 | + ProxyHost = \n |
| 93 | + ProxyPort = \n |
| 94 | + ProxyUser = \n |
| 95 | + ProxyPassword = \n |
| 96 | + ProxyRealm = \n |
| 97 | + CredentialWalletPassword = \n |
| 98 | + #Service.plugin.appmgmt.download=true \n |
| 99 | + #Service.plugin.jms.download=true \n |
| 100 | + #Service.plugin.dbaas.download=true \n |
| 101 | + Service.plugin.logan.download=true |
| 102 | + #Service.plugin.opsiHost.download=true \n |
| 103 | + #Service.plugin.jm.download=true" |
| 104 | + dest: "./{{remote_mgmt_agent_scratch_dir}}/input.rsp" |
| 105 | + |
| 106 | + - name: List management agent images |
| 107 | + oci_management_agent_image_facts: |
| 108 | + compartment_id: "{{compartment_ocid}}" |
| 109 | + install_type: AGENT |
| 110 | + register: image_result |
| 111 | + - set_fact: |
| 112 | + object_url: "{{ item.object_url | split('/')}}" |
| 113 | + namespace: "{{[4]|map('extract', item.object_url.split('/')) | join()}}" |
| 114 | + bucket_name: "{{[6]|map('extract', item.object_url.split('/')) | join ()}}" |
| 115 | + object_name: "{{[8,9,10]|map('extract', item.object_url.split('/')) | join('/')}}" |
| 116 | + |
| 117 | + with_items: "{{image_result.management_agent_images}}" |
| 118 | + when: |
| 119 | + - item.platform_name == "Linux-x86_64" |
| 120 | + - item.package_type == "ZIP" |
| 121 | + - debug: |
| 122 | + msg: "Extracted the agent image details as follows Namespace: {{namespace}} Bucket: {{bucket_name}} Object name: {{object_name}}" |
| 123 | + |
| 124 | + - name: Download Agent ZIP object |
| 125 | + oci_object_storage_object: |
| 126 | + # required |
| 127 | + namespace_name: "{{namespace}}" |
| 128 | + bucket_name: "{{bucket_name}}" |
| 129 | + object_name: "{{object_name}}" |
| 130 | + dest: "./{{remote_mgmt_agent_scratch_dir}}/oracle.mgmt_agent.zip" |
| 131 | + tags: download_agent |
| 132 | + |
| 133 | +- hosts: target_hosts |
| 134 | + vars: |
| 135 | + # common vars |
| 136 | + tasks: |
| 137 | + - name: Transfer all Management Agent files over to the hosts |
| 138 | + become: yes |
| 139 | + become_user: root |
| 140 | + copy: |
| 141 | + src: "./{{remote_mgmt_agent_scratch_dir}}" |
| 142 | + dest: "{{remote_mgmt_agent_scratch_parent}}" |
| 143 | + owner: root |
| 144 | + group: root |
| 145 | + mode: '0644' |
| 146 | + |
| 147 | + - name: Unarchive a Management Agent zip on the remote machine |
| 148 | + ansible.builtin.unarchive: |
| 149 | + src: "{{remote_mgmt_agent_scratch_path}}/oracle.mgmt_agent.zip" |
| 150 | + dest: "{{remote_mgmt_agent_scratch_path}}/" |
| 151 | + remote_src: yes |
| 152 | + become: yes |
| 153 | + |
| 154 | + - name: Install the Management Agent zip |
| 155 | + become: yes |
| 156 | + become_user: root |
| 157 | + shell: |
| 158 | + "/bin/bash {{remote_mgmt_agent_scratch_path}}/mgmt_agent/installer.sh {{remote_mgmt_agent_scratch_path}}/input.rsp" |
| 159 | + tags: install-agent |
| 160 | + |
| 161 | + - name: collect facts about system services |
| 162 | + service_facts: |
| 163 | + |
| 164 | + - name: Check if the mgmt_agent service is in running state |
| 165 | + fail: |
| 166 | + msg: "mgmt_agent is not in running state" |
| 167 | + when: "'mgmt_agent.service' not in services or ansible_facts.services['mgmt_agent.service'].state != 'running'" |
| 168 | + |
| 169 | + - name: Cleanup management agent scratch |
| 170 | + become: yes |
| 171 | + become_user: root |
| 172 | + file: |
| 173 | + path: "{{remote_mgmt_agent_scratch_path}}" |
| 174 | + state: absent |
| 175 | + |
| 176 | +- hosts: localhost |
| 177 | + collections: |
| 178 | + - oracle.oci |
| 179 | + tasks: |
| 180 | + - name: Cleanup management agent scratch |
| 181 | + file: |
| 182 | + path: "./{{remote_mgmt_agent_scratch_dir}}" |
| 183 | + state: absent |
0 commit comments