Manage Cloud with Ansible

Manage your Cloud servers, firewalls, SSH keys and volumes with Ansible playbooks, and configure your servers through an inventory built from their tags.

Install the collection

The betterstack.betterstack collection needs ansible-core 2.15 or later.

Install the Better Stack collection
ansible-galaxy collection install betterstack.betterstack

Get an API token

Create a global API token in Settings → API tokens → Global tokens, and set it as the BETTERSTACK_API_TOKEN environment variable in the shell that runs Ansible.

Create your first server

Each data center is managed separately, so the playbook sets the one your resources are in as availability_zone, once for every task with module_defaults.

Save this playbook as cloud.yml. It adds your SSH key, a firewall that lets in SSH and HTTPS, a server that uses both, and a 10 GiB volume that the server mounts. Replace t123 with your team's ID from betterstack team list in the Cloud CLI:

cloud.yml
- name: Create a web server in Cloud
  hosts: localhost
  gather_facts: false
  module_defaults:
    group/betterstack.betterstack.cloud:
      availability_zone: us-west-2a
  tasks:
    - name: Add your SSH key
      betterstack.betterstack.ssh_key:
        slug: my-laptop
        team_id: t123
        public_key: "{{ lookup('file', '~/.ssh/id_ed25519.pub') }}"

    - name: Add a firewall that lets in SSH and HTTPS
      betterstack.betterstack.firewall:
        name: web
        team_id: t123
        rules:
          - { direction: inbound, protocol: tcp, ip_addresses: [0.0.0.0/0, "::/0"], port_from: 22 }
          - { direction: inbound, protocol: tcp, ip_addresses: [0.0.0.0/0, "::/0"], port_from: 443 }
          - { direction: outbound, protocol: any, ip_addresses: [0.0.0.0/0, "::/0"], port_from: 0, port_to: 65535 }
      register: firewall

    - name: Create the server
      betterstack.betterstack.server:
        name: web-1
        team_id: t123
        server_type: AM1
        image: ubuntu-26-04
        ssh_keys: [my-laptop]
        firewall_ids: ["{{ firewall.firewall.id }}"]
        metadata:
          role: web
      register: server

    - name: Add a volume to the server
      betterstack.betterstack.volume:
        name: web-1-data
        size_gb: 10
        format: ext4
        auto_mount: true
        server_id: "{{ server.server.id }}"
Create the server
ansible-playbook cloud.yml

The rules list is every rule the firewall has, so keep the outbound rule to let the server reach the internet. The modules find each resource by its name, or its slug for SSH keys, so running the playbook again changes only what differs.

To remove the resources, add state: absent to each task and run them in reverse order: the volume, the server, then the firewall.

Configure your servers

The betterstack.betterstack.servers inventory lists your servers in every data center as hosts and connects to each one's IPv4 address. Every tag in a server's metadata puts it in a group named tag_<key>_<value>, like tag_role_web.

Save the inventory as inventory.betterstack_cloud.yml. The plugin reads only a file whose name ends in betterstack_cloud.yml:

inventory.betterstack_cloud.yml
plugin: betterstack.betterstack.servers

Install nginx on every server tagged role: web:

web.yml
- name: Set up the web servers
  hosts: tag_role_web
  remote_user: root
  vars:
    ansible_ssh_common_args: -o StrictHostKeyChecking=accept-new
  tasks:
    - name: Install nginx
      ansible.builtin.apt:
        name: nginx
        update_cache: true
Configure the web servers
ansible-playbook -i inventory.betterstack_cloud.yml web.yml

Need help?

Please let us know at hello@betterstack.com.
We're happy to help! 🙏