Beyond the major cloud providers (AWS, Azure, GCP), Ansible supports many other cloud platforms. This guide covers DigitalOcean, Linode, Oracle Cloud, IBM Cloud, VMware, and other platforms.
DigitalOcean is a popular cloud provider for developers, offering simple pricing and easy-to-use infrastructure.
# Install DigitalOcean collection
ansible-galaxy collection install community.digitalocean
# Set API token
export DO_API_TOKEN="your_digitalocean_api_token"
- name: Create DigitalOcean Droplet
hosts: localhost
gather_facts: false
vars:
do_token: "{{ lookup('env', 'DO_API_TOKEN') }}"
tasks:
- name: Create droplet
community.digitalocean.digital_ocean_droplet:
state: present
name: web-server-01
oauth_token: "{{ do_token }}"
size: s-1vcpu-1gb
region: nyc3
image: ubuntu-22-04-x64
wait_timeout: 500
unique_name: true
ipv6: true
monitoring: true
tags:
- webserver
- production
register: droplet
- name: Display droplet IP
ansible.builtin.debug:
var: droplet.data.ip_address
- name: Create multiple DigitalOcean Droplets
hosts: localhost
gather_facts: false
vars:
do_token: "{{ lookup('env', 'DO_API_TOKEN') }}"
tasks:
- name: Create droplets
community.digitalocean.digital_ocean_droplet:
state: present
name: "app-server-{{ item }}"
oauth_token: "{{ do_token }}"
size: s-2vcpu-2gb
region: nyc3
image: ubuntu-22-04-x64
ssh_keys:
- my-ssh-key
wait_timeout: 500
unique_name: true
tags:
- appserver
- production
loop: "{{ range(1, 4) | list }}"
- name: Create DigitalOcean Floating IP
hosts: localhost
gather_facts: false
tasks:
- name: Reserve floating IP
community.digitalocean.digital_ocean_floating_ip:
state: present
oauth_token: "{{ lookup('env', 'DO_API_TOKEN') }}"
region: nyc3
- name: Assign floating IP to droplet
community.digitalocean.digital_ocean_floating_ip_assignment:
state: present
oauth_token: "{{ lookup('env', 'DO_API_TOKEN') }}"
floating_ip: "{{ floating_ip.data.ip_address }}"
droplet_id: "{{ droplet.data.id }}"
- name: Create DigitalOcean Load Balancer
hosts: localhost
gather_facts: false
tasks:
- name: Create load balancer
community.digitalocean.digital_ocean_load_balancer:
state: present
oauth_token: "{{ lookup('env', 'DO_API_TOKEN') }}"
name: web-lb
region: nyc3
size_unit: 1
forwarding_rules:
- entry_protocol: http
entry_port: 80
target_protocol: http
target_port: 80
- entry_protocol: https
entry_port: 443
target_protocol: https
target_port: 443
health_check:
protocol: http
port: 80
path: /health
check_interval_seconds: 10
response_timeout_seconds: 5
healthy_threshold: 5
unhealthy_threshold: 3
droplet_ids:
- "{{ hostvars['app-server-1'].digital_ocean_droplet.id }}"
- "{{ hostvars['app-server-2'].digital_ocean_droplet.id }}"
Linode offers affordable cloud hosting with a simple API and global data centers.
# Install Linode collection
ansible-galaxy collection install community.linode
# Set API token
export LINODE_API_TOKEN="your_linode_api_token"
- name: Create Linode instance
hosts: localhost
gather_facts: false
vars:
linode_token: "{{ lookup('env', 'LINODE_API_TOKEN') }}"
tasks:
- name: Create instance
community.linode.linode:
api_token: "{{ linode_token }}"
label: web-server-01
type: g6-standard-1
region: us-east
image: linode/ubuntu22.04
root_pass: "{{ vault_root_password }}"
authorized_keys:
- ~/.ssh/id_rsa.pub
tags:
- webserver
- production
wait: true
register: linode
- name: Display instance IP
ansible.builtin.debug:
var: linode.ipv4[0]
- name: Create multiple Linode instances
hosts: localhost
gather_facts: false
vars:
linode_token: "{{ lookup('env', 'LINODE_API_TOKEN') }}"
tasks:
- name: Create instances
community.linode.linode:
api_token: "{{ linode_token }}"
label: "app-server-{{ item }}"
type: g6-standard-2
region: us-east
image: linode/ubuntu22.04
root_pass: "{{ vault_root_password }}"
authorized_keys:
- ~/.ssh/id_rsa.pub
tags:
- appserver
- production
wait: true
loop: "{{ range(1, 4) | list }}"
- name: Create Linode NodeBalancer
hosts: localhost
gather_facts: false
vars:
linode_token: "{{ lookup('env', 'LINODE_API_TOKEN') }}"
tasks:
- name: Create NodeBalancer
community.linode.linode_nodebalancer:
api_token: "{{ linode_token }}"
label: web-nodebalancer
region: us-east
client_conn_throttle: 20
register: nodebalancer
- name: Create NodeBalancer config
community.linode.linode_nodebalancer_config:
api_token: "{{ linode_token }}"
nodebalancer_id: "{{ nodebalancer.id }}"
port: 80
protocol: http
check: http
check_path: /health
check_interval: 30
check_timeout: 3
check_attempts: 3
- name: Add nodes to NodeBalancer
community.linode.linode_nodebalancer_node:
api_token: "{{ linode_token }}"
nodebalancer_id: "{{ nodebalancer.id }}"
config_id: "{{ nodebalancer_config.id }}"
label: "app-server-{{ item }}"
address: "{{ hostvars['app-server-' + item | string].linode.ipv4[0] }}:80"
weight: 100
mode: balanced
loop: "{{ range(1, 4) | list }}"
Oracle Cloud offers cloud services with strong enterprise features.
# Install OCI collection
ansible-galaxy collection install oracle.oci
# Configure OCI CLI
oci setup config
# Or set environment variables
export OCI_USER_OCID="ocid1.user.oc1..xxx"
export OCI_FINGERPRINT="xx:xx:xx..."
export OCI_TENANCY_OCID="ocid1.tenancy.oc1..xxx"
export OCI_REGION="us-ashburn-1"
export OCI_PRIVATE_KEY_PATH="~/.oci/oci_api_key.pem"
- name: Create OCI Compute Instance
hosts: localhost
gather_facts: false
vars:
compartment_id: "ocid1.compartment.oc1..xxx"
tasks:
- name: Create instance
oracle.oci.oci_compute_instance:
compartment_id: "{{ compartment_id }}"
availability_domain: "Uocm:US-ASHBURN-AD-1"
shape: VM.Standard.E4.Flex
shape_config:
ocpus: 1
memory_in_gbs: 6
source_details:
source_type: image
image_id: "ocid1.image.oc1..xxx"
display_name: web-server-01
create_vnic_details:
subnet_id: "ocid1.subnet.oc1..xxx"
assign_public_ip: true
metadata:
ssh_authorized_keys: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"
defined_tags:
Environment: Production
Application: WebServer
register: instance
- name: Display instance IP
ansible.builtin.debug:
var: instance.instance.public_ip
- name: Create OCI VCN
hosts: localhost
gather_facts: false
vars:
compartment_id: "ocid1.compartment.oc1..xxx"
tasks:
- name: Create VCN
oracle.oci.oci_core_virtual_network:
compartment_id: "{{ compartment_id }}"
display_name: production-vcn
cidr_block: 10.0.0.0/16
dns_label: prodvcn
- name: Create subnet
oracle.oci.oci_core_subnet:
compartment_id: "{{ compartment_id }}"
vcn_id: "{{ vcn.id }}"
display_name: public-subnet
cidr_block: 10.0.1.0/24
dns_label: pubsubnet
prohibit_public_ip_on_vnic: false
route_table_id: "{{ route_table.id }}"
security_list_ids:
- "{{ security_list.id }}"
- name: Create Internet Gateway
oracle.oci.oci_core_internet_gateway:
compartment_id: "{{ compartment_id }}"
vcn_id: "{{ vcn.id }}"
display_name: internet-gw
is_enabled: true
- name: Create OCI Load Balancer
hosts: localhost
gather_facts: false
vars:
compartment_id: "ocid1.compartment.oc1..xxx"
subnet_id: "ocid1.subnet.oc1..xxx"
tasks:
- name: Create load balancer
oracle.oci.oci_lb_load_balancer:
compartment_id: "{{ compartment_id }}"
display_name: web-lb
shape_name: flexible
shape_details:
minimum_bandwidth_in_mbps: 10
maximum_bandwidth_in_mbps: 100
subnet_ids:
- "{{ subnet_id }}"
is_private: false
- name: Create backend set
oracle.oci.oci_lb_backend_set:
load_balancer_id: "{{ load_balancer.id }}"
name: backend-set
policy: ROUND_ROBIN
health_checker:
protocol: HTTP
port: 80
url_path: /health
interval_ms: 10000
timeout_in_millis: 3000
retries: 3
- name: Add backends
oracle.oci.oci_lb_backend:
load_balancer_id: "{{ load_balancer.id }}"
backendset_name: backend-set
name: "app-server-{{ item }}"
ip_address: "{{ hostvars['app-server-' + item | string].oci_instance.public_ip }}"
port: 80
loop: "{{ range(1, 4) | list }}"
- name: Create listener
oracle.oci.oci_lb_listener:
load_balancer_id: "{{ load_balancer.id }}"
name: http-listener
default_backend_set_name: backend-set
port: 80
protocol: HTTP
IBM Cloud provides enterprise-grade cloud services with strong AI and data capabilities.
# Install IBM Cloud collection
ansible-galaxy collection install ibm.cloud
# Set API key
export IC_API_KEY="your_ibm_cloud_api_key"
- name: Create IBM Cloud VSI
hosts: localhost
gather_facts: false
vars:
resource_group: "Default"
tasks:
- name: Create VSI
ibm.cloud.ibm_is_instance:
name: web-server-01
profile: bx2-2x8
image: r014-0f6e1d91-3a90-4d60-9092-8a7e8c7f8a90
zone: us-south-1
resource_group: "{{ resource_group }}"
keys:
- "{{ ssh_key_id }}"
vpc: "{{ vpc_id }}"
primary_network_interface:
subnet: "{{ subnet_id }}"
security_groups:
- "{{ security_group_id }}"
boot_volume:
name: boot-volume
capacity: 100
profile: 10iops-tier
register: vsi
- name: Display VSI IP
ansible.builtin.debug:
var: vsi.data.primary_network_interface.primary_ipv4_address
- name: Create IBM Cloud VPC
hosts: localhost
gather_facts: false
tasks:
- name: Create VPC
ibm.cloud.ibm_is_vpc:
name: production-vpc
resource_group: "Default"
address_prefix_management: manual
classic_access: false
- name: Create subnet
ibm.cloud.ibm_is_subnet:
name: public-subnet
vpc: "{{ vpc.id }}"
zone: us-south-1
ipv4_cidr_block: 10.0.1.0/24
public_gateway: "{{ public_gateway.id }}"
- name: Create security group
ibm.cloud.ibm_is_security_group:
name: web-sg
vpc: "{{ vpc.id }}"
- name: Create security group rules
ibm.cloud.ibm_is_security_group_rule:
group: "{{ security_group.id }}"
direction: inbound
remote: 0.0.0.0/0
port_min: 80
port_max: 80
type: tcp
Ansible can manage VMware vSphere environments for private cloud infrastructure.
# Install VMware collection
ansible-galaxy collection install community.vmware
# Install Python dependencies
pip install pyvmomi
# Set environment variables
export VMWARE_HOST="vcenter.example.com"
export VMWARE_USER="administrator@vsphere.local"
export VMWARE_PASSWORD="your_password"
export VMWARE_VALIDATE_CERTS=false
- name: Create VMware VM
hosts: localhost
gather_facts: false
vars:
vmware_host: "{{ lookup('env', 'VMWARE_HOST') }}"
vmware_user: "{{ lookup('env', 'VMWARE_USER') }}"
vmware_password: "{{ lookup('env', 'VMWARE_PASSWORD') }}"
validate_certs: false
tasks:
- name: Create VM from template
community.vmware.vmware_guest:
hostname: "{{ vmware_host }}"
username: "{{ vmware_user }}"
password: "{{ vmware_password }}"
validate_certs: "{{ validate_certs }}"
datacenter: DC1
cluster: Cluster1
name: web-server-01
template: ubuntu-22-04-template
folder: /Production
state: poweredon
hardware:
memory_mb: 2048
num_cpus: 2
disk:
- size_gb: 50
type: thin
datastore: datastore1
networks:
- name: VM Network
device_type: vmxnet3
ip: 192.168.1.100
netmask: 255.255.255.0
gateway: 192.168.1.1
dns_servers:
- 8.8.8.8
- 8.8.4.4
annotation: "Managed by Ansible"
register: vm
- name: Display VM IP
ansible.builtin.debug:
var: vm.instance.ipv4
- name: Clone multiple VMware VMs
hosts: localhost
gather_facts: false
vars:
vmware_host: "{{ lookup('env', 'VMWARE_HOST') }}"
vmware_user: "{{ lookup('env', 'VMWARE_USER') }}"
vmware_password: "{{ lookup('env', 'VMWARE_PASSWORD') }}"
validate_certs: false
tasks:
- name: Clone VMs
community.vmware.vmware_guest:
hostname: "{{ vmware_host }}"
username: "{{ vmware_user }}"
password: "{{ vmware_password }}"
validate_certs: "{{ validate_certs }}"
datacenter: DC1
cluster: Cluster1
name: "app-server-{{ item }}"
template: ubuntu-22-04-template
folder: /Production
state: poweredon
hardware:
memory_mb: 4096
num_cpus: 2
disk:
- size_gb: 100
type: thin
networks:
- name: VM Network
device_type: vmxnet3
start_connected: true
loop: "{{ range(1, 4) | list }}"
- name: Manage VMware VM power state
hosts: localhost
gather_facts: false
vars:
vmware_host: "{{ lookup('env', 'VMWARE_HOST') }}"
vmware_user: "{{ lookup('env', 'VMWARE_USER') }}"
vmware_password: "{{ lookup('env', 'VMWARE_PASSWORD') }}"
validate_certs: false
tasks:
- name: Shutdown VM
community.vmware.vmware_guest:
hostname: "{{ vmware_host }}"
username: "{{ vmware_user }}"
password: "{{ vmware_password }}"
validate_certs: "{{ validate_certs }}"
name: web-server-01
state: shutdowned
- name: Power on VM
community.vmware.vmware_guest:
hostname: "{{ vmware_host }}"
username: "{{ vmware_user }}"
password: "{{ vmware_password }}"
validate_certs: "{{ validate_certs }}"
name: web-server-01
state: poweredon
- name: Reset VM
community.vmware.vmware_guest:
hostname: "{{ vmware_host }}"
username: "{{ vmware_user }}"
password: "{{ vmware_password }}"
validate_certs: "{{ validate_certs }}"
name: web-server-01
state: rebooted
- name: Create VMware port group
hosts: localhost
gather_facts: false
vars:
vmware_host: "{{ lookup('env', 'VMWARE_HOST') }}"
vmware_user: "{{ lookup('env', 'VMWARE_USER') }}"
vmware_password: "{{ lookup('env', 'VMWARE_PASSWORD') }}"
validate_certs: false
tasks:
- name: Create port group
community.vmware.vmware_portgroup:
hostname: "{{ vmware_host }}"
username: "{{ vmware_user }}"
password: "{{ vmware_password }}"
validate_certs: "{{ validate_certs }}"
switch_name: vSwitch0
name: production-pg
vlan_id: 100
Proxmox is an open-source virtualization platform.
# Install Proxmox collection
ansible-galaxy collection install community.proxmox
# Install Python dependencies
pip install proxmoxer requests
# Set environment variables
export PROXMOX_HOST="proxmox.example.com"
export PROXMOX_USER="admin@pam"
export PROXMOX_PASSWORD="your_password"
- name: Create Proxmox VM
hosts: localhost
gather_facts: false
vars:
proxmox_host: "{{ lookup('env', 'PROXMOX_HOST') }}"
proxmox_user: "{{ lookup('env', 'PROXMOX_USER') }}"
proxmox_password: "{{ lookup('env', 'PROXMOX_PASSWORD') }}"
tasks:
- name: Create VM
community.proxmox.proxmox_kvm:
api_host: "{{ proxmox_host }}"
api_user: "{{ proxmox_user }}"
api_password: "{{ proxmox_password }}"
node: pve
vmid: 100
name: web-server-01
state: present
cores: 2
memory: 2048
net:
- model: virtio
bridge: vmbr0
disks:
- size: 50G
storage: local-lvm
iso: local:iso/ubuntu-22.04.iso
boot: order=scsi0;net0
ostype: l26
- name: Create Proxmox container
hosts: localhost
gather_facts: false
vars:
proxmox_host: "{{ lookup('env', 'PROXMOX_HOST') }}"
proxmox_user: "{{ lookup('env', 'PROXMOX_USER') }}"
proxmox_password: "{{ lookup('env', 'PROXMOX_PASSWORD') }}"
tasks:
- name: Create LXC container
community.proxmox.proxmox_container:
api_host: "{{ proxmox_host }}"
api_user: "{{ proxmox_user }}"
api_password: "{{ proxmox_password }}"
node: pve
vmid: 200
hostname: app-server-01
state: present
ostemplate: local:vztmpl/ubuntu-22.04-standard.tar.gz
storage: local-lvm
disk: local-lvm:8
memory: 1024
swap: 512
cores: 1
netif:
- name: eth0
bridge: vmbr0
ip: 192.168.1.100/24
gw: 192.168.1.1
features:
- nesting=1
OpenStack is an open-source cloud computing platform.
# Install OpenStack collection
ansible-galaxy collection install openstack.cloud
# Install Python dependencies
pip install openstacksdk
# Source OpenStack credentials
source openstack.rc
- name: Create OpenStack instance
hosts: localhost
gather_facts: false
tasks:
- name: Create instance
openstack.cloud.server:
state: present
name: web-server-01
image: Ubuntu-22.04
flavor: m1.small
key_name: my-keypair
security_groups:
- default
nics:
- net-id: "{{ network_id }}"
wait: true
timeout: 300
register: server
- name: Display instance IP
ansible.builtin.debug:
var: server.openstack_public_ip
- name: Create OpenStack network
hosts: localhost
gather_facts: false
tasks:
- name: Create network
openstack.cloud.network:
state: present
name: production-net
external: false
- name: Create subnet
openstack.cloud.subnet:
state: present
network_name: production-net
name: production-subnet
cidr: 10.0.0.0/24
gateway_ip: 10.0.0.1
enable_dhcp: true
dns_nameservers:
- 8.8.8.8
- 8.8.4.4
- name: Create router
openstack.cloud.router:
state: present
name: production-router
network: external-net
enable_snat: true
interfaces:
- production-subnet
This example demonstrates deploying to multiple cloud providers:
- name: Deploy multi-cloud infrastructure
hosts: localhost
gather_facts: false
vars:
# DigitalOcean
do_token: "{{ lookup('env', 'DO_API_TOKEN') }}"
# Linode
linode_token: "{{ lookup('env', 'LINODE_API_TOKEN') }}"
tasks:
# DigitalOcean deployment
- name: Create DigitalOcean droplets
community.digitalocean.digital_ocean_droplet:
state: present
name: "do-web-{{ item }}"
oauth_token: "{{ do_token }}"
size: s-1vcpu-1gb
region: nyc3
image: ubuntu-22-04-x64
wait_timeout: 500
unique_name: true
tags:
- webserver
- digitalocean
loop: "{{ range(1, 3) | list }}"
register: do_droplets
# Linode deployment
- name: Create Linode instances
community.linode.linode:
api_token: "{{ linode_token }}"
label: "linode-app-{{ item }}"
type: g6-standard-1
region: us-east
image: linode/ubuntu22.04
root_pass: "{{ vault_root_password }}"
authorized_keys:
- ~/.ssh/id_rsa.pub
tags:
- appserver
- linode
wait: true
loop: "{{ range(1, 3) | list }}"
register: linode_instances
# Output summary
- name: Display deployment summary
ansible.builtin.debug:
msg: |
Deployment Complete!
DigitalOcean:
{% for droplet in do_droplets.results %}
- {{ droplet.data.name }}: {{ droplet.data.ip_address }}
{% endfor %}
Linode:
{% for instance in linode_instances.results %}
- {{ instance.label }}: {{ instance.ipv4[0] }}
{% endfor %}
Most cloud providers have inventory plugins for dynamic inventory:
# digitalocean_inventory.yml
plugin: community.digitalocean.digitalocean
oauth_token: "{{ lookup('env', 'DO_API_TOKEN') }}"
keyed_groups:
- key: do_tags
prefix: tag
- key: do_region
prefix: region
# Use dynamic inventory
ansible-inventory -i digitalocean_inventory.yml --list
ansible all -i digitalocean_inventory.yml -m ping
tags:
- Environment:Production
- Application:WebServer
- ManagedBy:Ansible
- CostCenter:12345
- name: Create instance with retry
community.digitalocean.digital_ocean_droplet:
# ... parameters ...
register: droplet
retries: 3
delay: 10
until: droplet.data.status == 'active'
All cloud modules support idempotent operations:
- name: Ensure instance exists
community.digitalocean.digital_ocean_droplet:
state: present
# ... parameters ...
- name: Ensure old instance is removed
community.digitalocean.digital_ocean_droplet:
state: absent
name: old-server
Any questions?
Feel free to contact us. Find all contact information on our contact page.