UpCloudLtd / upcloud-ansible

Dynamic inventory and modules for managing servers via UpCloud's API
50 stars 17 forks source link
ansible ansible-playbook inventory playbook upcloud upcloud-api

upcloud-ansible

DEPRECATION NOTICE

This repo has been deprecated and won't receive more fixes, features or version updates. We suggest using UpCloud Terraform Provider for creating infrastructure and UpCloud Ansible Collection for provisioning and modification.

Dynamic inventory and modules for managing servers via UpCloud's API

The inventory script and modules contain documentation and examples as per Ansible's developer guidelines.

Dependencies and supported versions

Note for OS X users:

Inventory script

Installation

Usage

# match all servers
ansible all -m ping -i /path/to/upcloud.py

# match all servers from upcloud inventory script
ansible uc_all -m ping -i /path/to/upcloud.py

# inventory group servers by upcloud Tags
ansible <any-upcloud-tag> -m <module> -i <path-to-upcloud-inventory>

UpCloud modules

Installation

Usage


# you can specify inventory and Modules pathes via cli
ansible-playbook create-servers.yml -i /path/to/upcloud.py -M /path/to/upcloud/modules

See the source files for documentation and examples. You may also want to refer to UpCloud's API documentation

The following example shows off some of the features of upcloud, upcloud_tag and upcloud_firewall modules:

---
- hosts: localhost
  connection: local
  serial: 1
  gather_facts: no

  tasks:
    - name: Create upcloud server
      upcloud:
        state: present
        hostname: web1.example.com
        title: web1.example.com
        zone: uk-lon1
        plan: 1xCPU-1GB
        storage_devices:
          - { size: 30, os: 01000000-0000-4000-8000-000030200200 } # Note this is Ubuntu server 20.04 template UUID
          - { size: 100 }
        api_user: <YOUR UPCLOUD USERNAME>
        api_passwd: <YOUR UPCLOUD PASSWORD>
        ssh_keys:
          - ssh-rsa AAAAB3NzaC1yc2EAA[...]ptshi44x user@some.host
          - ssh-dss AAAAB3NzaC1kc3MAA[...]VHRzAA== someuser@some.other.host
      register: upcloud_server # upcloud_server.server will contain the API response body

    # upcloud_server.public_ip shortcut will contain a public IPv4 (preferred) or IPv6 address
    # this task is not needed if host_key_checking=False in ansible
    - name: remove new server from known_hosts in case of IP collision
      known_hosts:
        state: absent
        host: "{{ upcloud_server.public_ip }}"

    - name: Wait for SSH to come up
      wait_for: host={{ upcloud_server.public_ip }} port=22 delay=5 timeout=320 state=started

    - name: tag the created server
      upcloud_tag:
        state: present
        uuid: "{{ upcloud_server.server.uuid }}"
        tags: [webservers, london]

    - name: configure firewall
      upcloud_firewall:
        state: present
        uuid: "{{ upcloud_server.server.uuid }}"
        firewall_rules:
          - direction: in
            family: IPv4
            protocol: udp
            destination_port_start: 53
            destination_port_end: 53
            action: accept

          - direction: in
            family: IPv4
            protocol: tcp
            destination_port_start: 22
            destination_port_end: 22
            action: accept

          - direction: in
            family: IPv4
            protocol: tcp
            destination_port_start: 80
            destination_port_end: 80
            action: accept

          - direction: in
            family: IPv4
            protocol: tcp
            destination_port_start: 443
            destination_port_end: 443
            action: accept

          # default rule last:
          - direction: in
            action: drop

Testing

Tests located in project_root/test/ directory. Run with:

pytest test/

To test against all supported python versions, run (will also run flake8 checks):

tox

To check for possible vulnerabilities in python packages, run:

safety check

To check for flake8 errors, run:

flake8

For tests, flake8 and safety check to work - additional dependencies need to be installed:

pip install -r requirements-dev.txt

Bugs, Issues, Problems, Ideas

Please report issues and features requests through the issues page.