Skip to main content
All CollectionsVPSAPI
Using Hostinger Ansible collection

Using Hostinger Ansible collection

Introducing the Hostinger Ansible Collection

Updated this week

The Hostinger Ansible collection is a powerful tool for automating the management of your Hostinger Virtual Private Servers (VPS) using Ansible. This article explains the collection and how to install it, as well as provides examples of how to use it in your automation workflows.

What is the Hostinger Ansible collection?

The Hostinger Ansible collection is a set of Ansible modules that interact with Hostinger's public API. These modules enable you to manage all aspects of your Hostinger VPS infrastructure through Ansible playbooks, including:

  • Managing firewalls and firewall rules

  • Controlling VPS power state (start, stop, restart)

  • Setting hostnames and SSH keys

  • Creating and managing snapshots

  • Installing malware protection

  • Retrieving performance metrics

  • Automating server provisioning

  • Creating and managing post-install scripts

By using this collection, you can implement Infrastructure as Code (IaC) practices with your Hostinger VPS resources, making your server management more efficient, consistent, and repeatable.

Installation

You can install the Hostinger Ansible collection using Ansible Galaxy, the official repository for Ansible collections.

Installing from Ansible Galaxy

ansible-galaxy collection install hostinger.vps

Installing locally for development

If you want to contribute to the collection or need to install it from a local build:

ansible-galaxy collection build
ansible-galaxy collection install hostinger-vps-*.tar.gz

Available modules

The Hostinger Ansible collection includes a comprehensive set of modules that cover all aspects of VPS management, from firewall configuration and power management to post-install scripts and snapshots.

Some of the key modules include:

  • VPS provisioning and power management

  • Firewall management and rules

  • Hostname and SSH key configuration

  • Post-install script management

  • Snapshot creation and restoration

Additionally, the collection provides a dynamic inventory plugin that automatically discovers your Hostinger VPS instances.

For the complete list of available modules and detailed documentation, please visit the Hostinger Ansible collection GitHub repository.

Configuration

To use the Hostinger Ansible collection, you must configure your Ansible environment with your Hostinger API credentials.

API Authentication

Create a configuration file in your Ansible project (e.g., hostinger_api.yaml):

yaml--- hostinger_api_token: "your_api_token_here"

You can then reference this file in your playbooks or include it using the vars_files directive.

Usage Examples

Here are some practical examples of how to use the Hostinger Ansible collection in your playbooks.

Retrieving VPS Information

---
- name: Get VPS Information
hosts: localhost
vars_files:
- hostinger_api.yaml
tasks:
- name: Get VPS details
hostinger.vps.hostinger_vps_get_info:
token: "{{ hostinger_api_token }}"
vps_id: 123456
register: vps_info

- name: Display VPS information
debug:
var: vps_info

Restart VPS

---
- name: Restart VPS
hosts: localhost
vars_files:
- hostinger_api.yaml
tasks:
- name: Restart VPS
hostinger.vps.hostinger_vps_power:
token: "{{ hostinger_api_token }}"
vps_id: 123456
state: restarted
register: restart_result

- name: Display restart result
debug:
var: restart_result

Creating and using post-install scripts

---
- name: Manage Post-Install Scripts
hosts: localhost
vars_files:
- hostinger_api.yaml
tasks:
- name: Create LAMP stack post-install script
hostinger.vps.hostinger_vps_postinstall_create:
token: "{{ hostinger_api_token }}"
name: "Install Docker"
content: |
#!/bin/bash

apt update && apt install -y docker.io
register: post_install_script

- name: Reinstall VPS with post-install script
hostinger.vps.hostinger_vps_reinstall:
token: "{{ hostinger_api_token }}"
vps_id: 123456
template_id: 4 # Ubuntu 22.04
post_install_script_id: "{{ post_install_script.id }}"

Troubleshooting

Common Issues

  1. Authentication Errors: Ensure your API token is correct and has the necessary permissions.

  2. Module Not Found: Ensure you have installed the collection correctly. Try reinstalling with ansible-galaxy collection install hostinger.vps --force.

  3. API Rate Limiting: If you make many API calls in a short period, you might hit rate limits. If needed, add delays between tasks.

  4. Error Messages: Always check the returned error messages, which often provide specific details about what went wrong.

The Hostinger Ansible collection provides a powerful way to automate your Hostinger VPS management tasks using Ansible. By incorporating these modules into your automation workflows, you can save time, reduce errors, and implement infrastructure as code practices for your Hostinger VPS resources.

If you would like more information about Ansible collections and how to use them, please refer to the official Ansible documentation.

Did this answer your question?