How to create multiple virtual machines in azure using ansible

1,013 views
Skip to first unread message

naveen....@gmail.com

unread,
Oct 29, 2015, 8:07:50 AM10/29/15
to Ansible Project

I tried with the following playbook and its not working. Not sure what I am missing here.

  hosts: localhost
  connection: local
  gather_facts: no
  vars_files:
    - vars/azure.yml
  tasks:
    - name: create VM
      azure:
        name: '{{ vm_name }}'
        role_size: '{{ role_size }}'
        image: '{{ image_name }}'
        location: '{{ location }}'
        user: '{{ username }}'
        password: '{{ password }}'
        subscription_id: '{{ subscription_id }}'
        management_cert_path: '{{ certificate_path }}'
        storage_account: '{{ storage_account_name }}'
        virtual_network_name: '{{ virtual_network_name }}'
        wait: yes
      with_items: 
        - { name: "{{ vm_names_list }}" } 
      register : azure

    - name  : Debug Azure output
      debug :
        var : azure

I have mentioned my vm_names_list like this :

vm_names_list:
    - "loferVM123"
    - "loferVM234"

It does not do what I expect. I expected it to create three VMs. But it creates only one VM. In the debug output it gives me the items in the list but only creates one VM.

This is the debug output:

"virtual_network_name": "myVirtualNetwork"
                },
                "invocation": {
                    "module_args": "",
                    "module_name": "azure"
                },
                "item": {
                    "vm_name": [
                        "loferVM123",
                        "loferVM234"
                    ]
                },
                "public_dns_name": "lofervm175.cloudapp.net"

My variable file:

subscription_id: "ID"
certificate_path: "/path/to/certs/mycert.pem"
image_name: "5112500ae3b842c8b9c604889f8753c3__OpenLogic-CentOS-67-20150815"
storage_account_name: "mystorageacc"
virtual_network_name: "myVirtualNetwork"
username: "user"
password: "password"
vm_name: "Testvm"
vm_names_list:
    - "loferVM123"
    - "loferVM234"
role_size: "Small"
location: "Central US"
data_disk_size: "100"
commitlog_disk_size: "10"

This is the output from azure-cli vm show

data:    DNSName "lofervm234.cloudapp.net"
data:    Location "Central US"
data:    VMName "loferVM234"
data:    IPAddress "10.0.2.6"
data:    InstanceStatus "ReadyRole"
data:    InstanceSize "Small"
data:    Image "5112500ae3b842c8b9c604889f8753c3__OpenLogic-CentOS-67-20150815"
data:    OSDisk hostCaching "ReadWrite"
data:    OSDisk name "loferVM234-loferVM234-0-201510262116050841"
data:    OSDisk mediaLink "http://portalvhdskswbj32fl5cx8.blob.core.windows.net/vhds/loferVM234-loferVM234-2015-10-26.vhd"
data:    OSDisk sourceImageName "5112500ae3b842c8b9c604889f8753c3__OpenLogic-CentOS-67-20150815"
data:    OSDisk operatingSystem "Linux"
data:    OSDisk iOType "Standard"
data:    DataDisks 0 hostCaching "None"
data:    DataDisks 0 label "loferVM234-loferVM234-loferVM234-0"
data:    DataDisks 0 name "loferVM234-loferVM234-0-201510262116380749"
data:    DataDisks 0 logicalDiskSizeInGB 100
data:    DataDisks 0 mediaLink "https://portalvhdskswbj32fl5cx8.blob.core.windows.net/vhds/loferVM234-b73613825dfb0bcb.vhd"
data:    DataDisks 0 iOType "Standard"
data:    DataDisks 1 hostCaching "None"
data:    DataDisks 1 label "loferVM234-loferVM234-loferVM234-1"
data:    DataDisks 1 name "loferVM234-loferVM234-1-201510262117180308"
data:    DataDisks 1 logicalUnitNumber 1
data:    DataDisks 1 logicalDiskSizeInGB 100
data:    DataDisks 1 mediaLink "https://portalvhdskswbj32fl5cx8.blob.core.windows.net/vhds/loferVM234-cf18946abcbfe4c8.vhd"
data:    DataDisks 1 iOType "Standard"
data:    ReservedIPName ""
data:    VirtualIPAddresses 0 address "104.43.202.234"
data:    VirtualIPAddresses 0 name "loferVM234ContractContract"
data:    VirtualIPAddresses 0 isDnsProgrammed true
data:    Network Endpoints 0 localPort 22
data:    Network Endpoints 0 name "TCP-22"
data:    Network Endpoints 0 port 22
data:    Network Endpoints 0 protocol "tcp"
data:    Network Endpoints 0 virtualIPAddress "104.43.202.234"
data:    Network Endpoints 0 enableDirectServerReturn false
data:    Network Endpoints 0 idleTimeoutInMinutes 4
Message has been deleted

Brian Coca

unread,
Oct 31, 2015, 4:03:30 PM10/31/15
to Ansible Project
try this:

- hosts: localhost
connection: local
gather_facts: no
vars_files:
- vars/azure.yml
tasks:
- name: create VM
azure:
name: '{{ item }}'
role_size: '{{ role_size }}'
image: '{{ image_name }}'
location: '{{ location }}'
user: '{{ username }}'
password: '{{ password }}'
subscription_id: '{{ subscription_id }}'
management_cert_path: '{{ certificate_path }}'
storage_account: '{{ storage_account_name }}'
virtual_network_name: '{{ virtual_network_name }}'
wait: yes
with_items:
- "{{ vm_names_list }}" }
register : azure

- name : Debug Azure output
debug :
var : azure


--
Brian Coca

J Hawkesworth

unread,
Nov 2, 2015, 6:59:02 AM11/2/15
to Ansible Project
Is the trailing } on 
              - "{{ vm_names_list }}" } 
intended / needed?

Brian Coca

unread,
Nov 2, 2015, 7:29:46 AM11/2/15
to Ansible Project
nope, that is a typo
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/462f6e89-8ea1-4e40-baa0-a5ef37b0c295%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Brian Coca

yaniv refael

unread,
Apr 10, 2016, 6:34:31 AM4/10/16
to Ansible Project, naveen....@gmail.com
Can you share the ansible-palybook command? 
Reply all
Reply to author
Forward
0 new messages