├── README.md ├── Share_RSA.yml ├── cloudevops-badge.png ├── deploy.json ├── destroy.json ├── install.yml ├── page.html ├── vRA-deploy-VM.yml └── vRA-destroy-VM.yml /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | # Ansible roles for vRealize Automation - VMware vCloud IaaS solution 8 | 9 | 10 | ## Description 11 | 12 | vRA-deploy-VM is a playbook that use ansible uri core module to request a VM from vRealize automation solution, the Vmware Cloud Automation Center (vCAC) 13 | This script is built to be run from a jenkins slave with a minmal installation in order to create an ephemeral deployement machine to deploy test and destroy the machines after testing. 14 | 15 | HOW It WORKS 16 | ================ 17 | vRA-deploy-VM is a playbook that use ansible uri core module to request a VM from vRealize automation solution, the vmware cloud. 18 | This script can either be played form jenkins with ansible or in adhoc mode. 19 | 20 | requierement : ** A valid vRA account able to create/destroy machines ** 21 | 22 | As you can see there is two main role file : 23 | 24 | ## vRA-deploy-VM.yml : 25 | This file is used to request a VM to vRealize Automation it will play a full mimed json request to ask ressource from vcloud and then get ip and hostname. 26 | Please store user and password in a separate yml vault file 27 | 28 | ### HOW to use it 29 | ```shell 30 | ansible-playbook vRA-deploy-VM.yml --ask-vault-pass 31 | ``` 32 | 33 | ###### Input : 34 | ```yaml 35 | VRA_HOST: {{ VRA_HOST}} 36 | VRA_USER: {{ VRA_USER }} 37 | VRA_PASS: {{ VRA_PASS }} 38 | VRA_TENANT: {{ VRA_TENANT }} 39 | ``` 40 | 41 | ###### Output : 42 | ```yaml 43 | VM_Destroy_id="{{ list.json.content[item].id }}" 44 | VM_name="{{ list.json.content[item].name }}" 45 | VM_IP="{{ list.json.content[item].resourceData }} 46 | ``` 47 | 48 | ## vRA-destroy-VM.yml : 49 | 50 | ### HOW to use it 51 | ```shell 52 | ansible-playbook vRA-destroy-VM.yml --ask-vault-pass 53 | ``` 54 | 55 | ###### Input : 56 | ```yaml 57 | VRA_HOST: {{ VRA_HOST}} 58 | VRA_USER: {{ VRA_USER }} 59 | VRA_PASS: {{ VRA_PASS }} 60 | VRA_TENANT: {{ VRA_TENANT }} 61 | {{ DESTROY_ID }} 62 | ``` 63 | ###### Output : 64 | NONE 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /Share_RSA.yml: -------------------------------------------------------------------------------- 1 | - hosts: '{{ host }}' 2 | remote_user: '{{ user }}' 3 | 4 | tasks: 5 | - name: Add RSA key to the remote host 6 | authorized_key: user='{{ user }}' key="{{ lookup('file', '{{home_user}}/.ssh/id_rsa.pub') }}" 7 | -------------------------------------------------------------------------------- /cloudevops-badge.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ClouDevops101/Ansible-VMware-vRealize-Automation/67d3b1cf19c3a3eae1ece4522ba088a1eeccd651/cloudevops-badge.png -------------------------------------------------------------------------------- /deploy.json: -------------------------------------------------------------------------------- 1 | { 2 | "@type": "CatalogItemRequest", 3 | "catalogItemRef": { 4 | "id": "##CATALOG_ITEM_ID##", 5 | "label": "##CATALOG_ITEM_LABEL##" 6 | }, 7 | "organization": { 8 | "tenantRef": "##VRA_TENANT##", 9 | "tenantLabel": "##VRA_TENANT##", 10 | "subtenantRef": "##BG_ID##", 11 | "subtenantLabel": "##BG##" 12 | }, 13 | "requestedFor": "##USER_REQUESTEDFOR##", 14 | "state": "SUBMITTED", 15 | "requestNumber": 0, 16 | "requestData": { 17 | "entries": [ 18 | { 19 | "key": "provider-blueprintId", 20 | "value": { 21 | "type": "string", 22 | "value": "##BINDING_ID##" 23 | } 24 | }, 25 | { 26 | "key": "provider-provisioningGroupId", 27 | "value": { 28 | "type": "string", 29 | "value": "##BG_ID##" 30 | } 31 | }, 32 | { 33 | "key": "provider-Cafe.Shim.VirtualMachine.Description", 34 | "value": { 35 | "type": "string", 36 | "value": "##DESCRIPTION##" 37 | } 38 | }, 39 | { 40 | "key": "provider-Cafe.Shim.VirtualMachine.Reason", 41 | "value": 42 | { 43 | "type": "string", 44 | "value": "##REASON##" 45 | } 46 | }, 47 | { 48 | "key": "provider-Cafe.Shim.VirtualMachine.NumberOfInstances", 49 | "value": 50 | { 51 | "type": "integer", 52 | "value": 1 53 | } 54 | }, 55 | { 56 | "key": "provider-VirtualMachine.CPU.Count", 57 | "value": 58 | { 59 | "type": "integer", 60 | "value": ##CPU_COUNT## 61 | } 62 | }, 63 | { 64 | "key": "provider-__Notes", 65 | "value": 66 | { 67 | "type": "string", 68 | "value": "##DESCRIPTION##" 69 | } 70 | }, 71 | { 72 | "key": "provider-VirtualMachine.Disk0.Size", 73 | "value": 74 | { 75 | "type": "string", 76 | "value": "45" 77 | } 78 | }, 79 | { 80 | "key": "provider-VirtualMachine.LeaseDays", 81 | "value": 82 | { 83 | "type": "integer", 84 | "value": ##CPU_COUNT## 85 | } 86 | }, 87 | { 88 | "key": "provider-VirtualMachine.Memory.Size", 89 | "value": 90 | { 91 | "type": "integer", 92 | "value": ##MEMORY_SIZE## 93 | } 94 | } 95 | ] 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /destroy.json: -------------------------------------------------------------------------------- 1 | { 2 | "@type": "ResourceActionRequest", 3 | "resourceRef": { 4 | "id": "##DESTROY_ID##" 5 | }, 6 | "resourceActionRef": { 7 | "id": "a4fd545b-7e13-4c10-8582-682a7a515486" 8 | }, 9 | "organization": { 10 | "tenantRef": "##VRA_TENANT##", 11 | "tenantLabel": "##VRA_TENANT##", 12 | "subtenantRef": "334be3f3-c8bf-4e06-9813-88aa9ad8c285", 13 | "subtenantLabel": "##BG##" 14 | }, 15 | "state": "SUBMITTED", 16 | "requestNumber": 0, 17 | "requestData": { 18 | "entries": [] 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /install.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - hosts: localhost 3 | # Defining used variable 4 | vars: 5 | Object: '{{ Object }}' 6 | 7 | tasks: 8 | # Install it if it's an rpm package 9 | - name: install rpm with yum 10 | yum: name='{{ Object }}' state=present 11 | when: '{{ Object | match("^.*.rpm$") }}' 12 | register: result_yum 13 | # Execute it if it's a shekk script 14 | - name: install a shell script # iOK only if isFirstRun is defined 15 | command: '{{ Object }}' 16 | when: '{{ Object | match("^.*.sh$") }}' 17 | register: result_shell - debug: msg="yum {{ result_yum }} shell {{ result_shell.stdout }} " 18 | -------------------------------------------------------------------------------- /page.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Document 6 | 7 | 8 | 9 | 14 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /vRA-deploy-VM.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - hosts: localhost 3 | # Defining used variable 4 | vars: 5 | var_files: 6 | - vars.yml 7 | VRA_HOST: {{ VRA_HOST}} 8 | VRA_USER: {{ VRA_USER }} 9 | VRA_PASS: {{ VRA_PASS }} 10 | VRA_TENANT: {{ VRA_TENANT }} 11 | USER_REQUESTEDFOR: {{ VRA_USER }}@{{ VRA_HOST}} 12 | DESCRIPTION: {{ DESCRIPTION }} 13 | REASON: {{ REASON }} 14 | CPU_COUNT: {{ CPU_COUNT }} 15 | MEMORY_SIZE: {{ MEMORY_SIZE }} 16 | user_data: 17 | 18 | tasks: 19 | 20 | - uri: 21 | url: https://{{ VRA_HOST }}/identity/api/tokens 22 | method: POSTs 23 | HEADER_Content-Type: application/json 24 | HEADER_Accept: application/json 25 | body: '{ "username" : "{{ VRA_USER }}", "" : "{{ VRA_PASS }}", "" : "{{ VRA_TENANT }}" }' 26 | validate_certs: no 27 | return_content: yes 28 | HEADER_Content-Type: application/json 29 | register: token 30 | # Send a inventory request to VERA 31 | - uri: 32 | url: https://{{ VRA_HOST }}/catalog-service/api/consumer/entitledCatalogItems 33 | method: GET 34 | HEADER_Content-Type: application/json 35 | HEADER_Accept: application/json 36 | HEADER_authorization: Bearer {{ token.json['id'] }} 37 | validate_certs: no 38 | return_content: yes 39 | register: list 40 | 41 | - command: "sed -i {{ item }} /etc/ansible/roles/deploy.json" 42 | with_items: 43 | - s/##CATALOG_ITEM_ID##/{{ list.json.content[0].catalogItem.id }}/g 44 | - s/##CATALOG_ITEM_LABEL##/{{ list.json.content[0].catalogItem.name }}/g 45 | - s/##USER_REQUESTEDFOR##/{{ USER_REQUESTEDFOR }}/g 46 | - s/##VRA_TENANT##/{{ VRA_TENANT }}/g 47 | - s/##BG##/{{ list.json.content[0].entitledOrganizations[0].subtenantLabel }}/g 48 | - s/##BG_ID##/{{ list.json.content[0].entitledOrganizations[0].subtenantRef }}/g 49 | - s/##BINDING_ID##/{{ list.json.content[0].catalogItem.providerBinding.bindingId }}/g 50 | - s/##DESCRIPTION##/{{ DESCRIPTION }}/g 51 | - s/##REASON##/{{ REASON }}/g 52 | - s/##CPU_COUNT##/{{ CPU_COUNT }}/g 53 | - s/##MEMORY_SIZE##/{{ MEMORY_SIZE }}/g 54 | # Send a request for a new VM 55 | - set_fact: body_json="{{ lookup('template','deploy.json',convert_data=False)}}" 56 | 57 | - uri: 58 | url: https://{{ VRA_HOST }}/catalog-service/api/consumer/requests 59 | method: POST 60 | HEADER_Accept: application/json 61 | HEADER_Content-Type: "application/json" 62 | HEADER_authorization: Bearer {{ token.json['id'] }} 63 | body: "{{ body_json }}" 64 | validate_certs: no 65 | timeout: 30 66 | status_code: 201 67 | return_content: yes 68 | register: result 69 | 70 | # Pause for 5 minutes to build app cache. 71 | - pause: seconds=250 prompt="Making sur the VM is deployed by vRealize Automation Tool" 72 | # GET REQUEST ID 73 | - uri: 74 | url: "{{ result.location }}" 75 | method: GET 76 | HEADER_Content-Type: application/json 77 | HEADER_Accept: application/json 78 | HEADER_authorization: Bearer {{ token.json['id'] }} 79 | validate_certs: no 80 | return_content: yes 81 | register: list 82 | # Get VM name in raw mode 83 | # Do some ninja filter 84 | - set_fact: Demandetofilter="{{ list.json.requestCompletion.completionDetails | regex_replace ('Request succeeded. Created ', '') }}" 85 | 86 | # Store VM Name demande 87 | - set_fact: Demande="{{ Demandetofilter | regex_replace ('\.', '') }}" 88 | # Ansible get VM Destroy id and @IP by VMname 89 | # Send a inventory request to vRA 90 | - uri: 91 | url: "https://{{ VRA_HOST }}/catalog-service/api/consumer/resources/types/Infrastructure.Machine/?page=Last&limit=3&$" 92 | method: GET 93 | HEADER_Content-Type: application/json 94 | HEADER_Accept: application/json 95 | HEADER_authorization: Bearer {{ token.json['id'] }} 96 | validate_certs: no 97 | return_content: yes 98 | register: list 99 | 100 | # Looking for VM BY VM_NUMBER 101 | 102 | - set_fact: VM_GLOBAL_EXIST="{{ list.json.metadata.totalElements}}" 103 | - set_fact: VM_Destroy_id="{{ list.json.content[item].id }}" VM_name="{{ list.json.content[item].name }}" VM_IP="{{ list.json.content[item].resourceData }}" 104 | with_items: [ 0, 1 ,2] 105 | when: ( "{{ list.json.content[item].name }}" == "{{ Demande }}" ) 106 | - shell: echo {{ VM_IP }} | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" 107 | register: IP 108 | - set_fact: VM_IP="{{ IP.stdout }}" 109 | - debug: msg="{{ VM_name }} {{ VM_IP }} {{ VM_Destroy_id }}" 110 | -------------------------------------------------------------------------------- /vRA-destroy-VM.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - hosts: localhost 3 | # Defining used variable 4 | vars: 5 | VRA_HOST: {{ VRA_HOST}} 6 | VRA_USER: {{ VRA_USER }} 7 | VRA_PASS: {{ VRA_PASS }} 8 | VRA_TENANT: {{ VRA_TENANT }} 9 | USER_REQUESTEDFOR: {{ VRA_USER }}@{{ VRA_HOST}} 10 | DESCRIPTION: {{ DESCRIPTION }} 11 | REASON: {{ REASON }} 12 | CPU_COUNT: {{ CPU_COUNT }} 13 | MEMORY_SIZE: {{ MEMORY_SIZE }} 14 | user_data: 15 | 16 | tasks: 17 | 18 | - uri: 19 | url: https://{{ VRA_HOST }}/identity/api/tokens 20 | method: POST 21 | HEADER_Content-Type: application/json 22 | HEADER_Accept: application/json 23 | body: '{ "username" : "{{ VRA_USER }}", "" : "{{ VRA_PASS }}", "" : "{{ VRA_TENANT }}" }' 24 | validate_certs: no 25 | return_content: yes 26 | HEADER_Content-Type: application/json 27 | register: token 28 | 29 | 30 | # send a inventory request to VERA 31 | 32 | - uri: 33 | url: https://{{ VRA_HOST }}/catalog-service/api/consumer/entitledCatalogItems 34 | method: GET 35 | HEADER_Content-Type: application/json 36 | HEADER_Accept: application/json 37 | HEADER_authorization: Bearer {{ token.json['id'] }} 38 | validate_certs: no 39 | return_content: yes 40 | register: list 41 | - debug: var=list 42 | 43 | - command: " sed -i {{ item }} /etc/ansible/roles/destroy.json" 44 | with_items: 45 | - s/##DESTROY_ID##/{{ DESTROY_ID }}/g 46 | 47 | - set_fact: body_json="{{ lookup('template','/etc/ansible/roles/destroy.json',convert_data=False) }}" 48 | - debug: var=body_json 49 | - uri: 50 | url: https://{{ VRA_HOST }}/catalog-service/api/consumer/requests 51 | method: POST 52 | HEADER_Accept: application/json 53 | HEADER_Content-Type: "application/json" 54 | HEADER_authorization: Bearer {{ token.json['id'] }} 55 | body: "{{ body_json }}" 56 | validate_certs: no 57 | timeout: 30 58 | status_code: 201 59 | return_content: yes 60 | register: result 61 | - debug: var=result 62 | --------------------------------------------------------------------------------