Proxmox VM provisioning with ansible - how to run task on created vm?

970 views
Skip to first unread message

Łukasz Boczkaja

unread,
Apr 21, 2020, 6:23:00 AM4/21/20
to Ansible Project

Hi,


I want to create a role that would do all of this tasks:

1. Create vm from template with preconfigured IP address

2. Modify vm parameters

3. Power up and wait for vm to be network reachable

4. Connect to newly created vm through template hardcoded IP and change its network settings


Now i don't want a static entry in inventory of vm template name and its ip as i want it both to be configured as role variable - in hosts i have specified only hypervisors IPs which it should run 1-3 tasks on but obviously the 4th task should be run on newly created VM reachable by templates IP. 

How can i make the 4th task run solely on the new vm?

What would we the best possible route to achieve this?

David Foley

unread,
Apr 21, 2020, 8:07:09 AM4/21/20
to Ansible Project
Hi

I currently have the Following Playbook that creates Windows Virtual Machines: 
If you need to do other tasks on the Virtual Machine; I'll recommend creating a Pipeline within a CI/CD platform:

For Example:
I have a number of Tasks 
  • Create Machine
  • Create AD Object
  • Join Domain
  • Install Applications On Server
  • Update SCCM 
  • Configure Second HardDrive on machine if Requested in Form:
  • Add DSC Agent onto Server

Tasks 4 will be Taken Care for you if you set AutoLogin:



---
   
- name: Running Create Virtual Machine Playbook
      hosts
: localhost
      gather_facts
: false
      connection
: local
      tasks
:
     
- name: Virtual Machine and customization
        vmware_guest
:
          validate_certs
: no
          hostname
: "{{ vcenter }}"
          username
: "{{ username }}"
          password
: "{{ password }}"
          cluster
: "{{ cluster }}"
          datacenter
: "{{ datacenter }}"
          name
: "{{ VM_Name }}"
          folder
: /
          template: "{{ template }}"
          state: poweredon
          datastore: "{{ datastore }}"
          disk:
          - size_gb: 40
            type: thin
            datastore: "{{ datastore }}"
          networks:
          - name: "{{ network_name }}"
            ip: "{{ ip }}"
            netmask: "{{ netmask }}"
            gateway: "{{ gateway }}"
            dns_servers:
            - 10.10.10.10
            - 10.10.10.20
            type: static
          wait_for_ip_address: yes
          wait_for_customization: yes
          customization:
            hostname: "{{ VM_Name }}"
            dns_servers:
            - 10.10.10.10
            - 10.10.10.20
            dns_suffix:
            - localhost.com
            - ap.localhost.com
            domain: "{{ domain }}"
            autologon: yes
            password: "{{ local_pass }}"
            runonce:
            - powershell.exe -ExecutionPolicy Unrestricted wget https:/
/raw.githubusercontent.com/ansible/ansible/devel/examples/scripts/ConfigureRemotingForAnsible.ps1 -OutFile C:\Windows\Temp\ConfigureRemotingForAnsible.ps1
           
- powershell.exe -ExecutionPolicy Unrestricted -File C:\Windows\Temp\ConfigureRemotingForAnsible.ps1 -EnableCredSSP -DisableBasicAuth -Verbose
          hardware
:
            memory_mb
: "{{ mb }}"
            num_cpus
: "{{ cpu }}"
        delegate_to
: localhost



Reply all
Reply to author
Forward
0 new messages