Search code examples
azureansibleyamlcloud-init

Using cloud-init as custom_data with ansible


I deploy a VM using an ansible playbook, similar to this demo.

- name: Create VM
  azure_rm_virtualmachine:
    resource_group: myResourceGroup
    name: myVM
    ...
    custom_data: cloud-init.yml

Now I also want to install some packages and do some minor preparations. I made a cloud-config.yml

#cloud-config

package_upgrade: true
packages:
  - npm
  - nodejs-legacy

runcmd:
- sudo mkdir -p /data/projects/

It seems that cloud-init.yml is not executed, so I guess this is not the correct syntax. How should you pass cloud-init files in an ansible playbook? Or is there another method to reach this goal?


Solution

  • custom_data parameter in azure_rm_virtualmachine requires the data, not a filename.

    You can use file lookup plugin to fetch the data from a file on an Ansible controller:

    - name: Create VM
      azure_rm_virtualmachine:
        resource_group: myResourceGroup
        name: myVM
        ...
        custom_data: "{{ lookup('file', 'cloud-init.yml') }}"