Last active
February 24, 2023 14:48
-
-
Save gcanales75/b965ed300c224064e92f to your computer and use it in GitHub Desktop.
Playbook de Ansible para crear VM en vSphere, instala Apache y envia notificación a Slack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- | |
| - name: Nueva VM, instala Apache y envia notificacion a Slack | |
| hosts: localhost | |
| user: root | |
| sudo: true | |
| ## Prompt que nos pregunta como queremos llamar a la nueva maquina virtual | |
| vars_prompt: | |
| - name: "guest" | |
| prompt: "Nombre VM?" | |
| private: no | |
| ## Variables para conectar a vCenter, Template y ubicacion de la nueva VM | |
| vars: | |
| vcenter: IP_o_FQDN_de_vCenter | |
| user: usuario_de_vCenter | |
| passwd: password_del_usuario_de_vCenter | |
| template: nombre_del_template | |
| rpool: "/Resources" ## <- Default cuando no se especifica Resource Pool | |
| cluster: "mi_cluster" ## En el Playbook no especifico ESXi porque el Cluster tiene DRS | |
| ## Task que crea la máquina virtual a partir del Template. Go! | |
| tasks: | |
| - vsphere_guest: | |
| vcenter_hostname: "{{ vcenter }}" | |
| username: "{{ user }}" | |
| password: "{{ passwd }}" | |
| guest: "{{ guest }}" | |
| from_template: "yes" | |
| template_src: "{{ template }}" | |
| resource_pool: "{{ rpool }}" | |
| cluster: "{{ cluster }}" | |
| - pause: seconds=60 ## Hace una pausa para esperar que encienda la VM y vCenter capture la IP | |
| ## Las siguiente lineas recolectan "facts" de la VM y permite tomar la IP que tomó por DHCP | |
| - vsphere_guest: | |
| vcenter_hostname: "{{ vcenter }}" | |
| username: "{{ user }}" | |
| password: "{{ passwd }}" | |
| guest: "{{ guest }}" | |
| vmware_guest_facts: yes | |
| ## Debug para validar que el Playbook tiene la IP de la VM | |
| - debug: msg="la IP es {{ hw_eth0.ipaddresses[0] }}" | |
| ## Agrega la IP al inventario de hosts para customizarlo | |
| - add_host: name={{ hw_eth0.ipaddresses[0] }} groups=virtual | |
| - | |
| hosts: virtual | |
| ## Debug para validar que tenemos el nombre de la VM para cambiar el hostname (Revisar Fact Caching) | |
| tasks: | |
| - debug: msg="{{ hostvars['localhost']['hw_name'] }}" | |
| ## Tareas. Instala Apache, lo inicia y le cambia el hostname a la máquina virtual. | |
| tasks: | |
| - name: instalar apache2 | |
| apt: name=apache2 update_cache=yes state=latest | |
| - name: iniciar apache | |
| service: name=apache2 state=started | |
| - name: renombrar host | |
| hostname: name="{{ hostvars['localhost']['hw_name'] }}" | |
| - | |
| hosts: localhost | |
| ## Ahora solo por showoff enviamos una notificacion a Slack | |
| tasks: | |
| - name: Notificacion a Slack | |
| local_action: | |
| module: slack | |
| token: admfñfxfm---API-key-para-recibir-notificaciones---akjnelner | |
| msg: "Server {{ hostvars['localhost']['hw_name'] }} listo! IP: {{ hw_eth0.ipaddresses[0] }}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment