Ram
Ram

Reputation: 324

How to create instances and install packages on azure using ansible

We are trying to create and install packages on Azure using Ansible. We are able to create the instance using Ansible Azure module but we are stuck at installing the packages once the VM is created because we don't know what the IP address of the newly created VM is.

We want to complete this in single run. Is this possible?

Upvotes: 1

Views: 1982

Answers (1)

ydaetskcoR
ydaetskcoR

Reputation: 56877

I've not used the Azure module so could be wrong but you should be able to use register to store some data about the instances you've just created.

You can then pass this data into a dynamically defined host group in a task by iterating through the output of the first task by using the add_host module.

So your playbook may look something like:

- hosts: local
  connection: local
  tasks:
    - name  : Create Windows instance
      azure :
        name: "ben-Winows-23"
        hostname: "win123"
        os_type: windows
        enable_winrm: yes
        subscription_id: "{{ azure_sub_id }}"
        management_cert_path: "{{ azure_cert_path }}"
        role_size: Small
        image: 'bd507d3a70934695bc2128e3e5a255ba__RightImage-Windows-2012-x64-v13.5'
        location: 'East Asia'
        password: "xxx"
        storage_account: benooytes
        user: admin
        wait: yes
        virtual_network_name: "{{ vnet_name }}"
      register : azure

    - name  : Debug Azure output
      debug :
        var : azure

### Assumes that the output from the previous task has an instances key which in turn has a public_ip key. This may need updating to give the proper path to a resolvable hostname or connectable IP. Use the output of the debug task to help with this. ###

    - name     : Add new instance to host group
      add_host :
        hostname  : {{ item.public_ip }}
        groupname : launched
      with_items : azure.instances

### Only target newly launched instances from previous play ###

- hosts: launched
  tasks:
    - name        : Start foo service and make it auto start
      win_service :
        name       : foo
        start_mode : auto
        state      : started

    - name : Do some thing else
      ...

Upvotes: 2

Related Questions