Ansible for serverplus: infrastructure and server setup
Ansible for serverplus
Ansible on serverplus works at two levels. First, the openstack.cloud collection creates and deletes the infrastructure itself (servers, keys, networks) declaratively. Second, ordinary playbooks configure already-running servers over SSH. And a dynamic inventory pulls the list of servers straight from OpenStack — no need to maintain a host list by hand.
Installation
pip install "ansible>=9" openstacksdk
ansible-galaxy collection install openstack.cloud
ansible --versionAccess comes from the same ~/.config/openstack/clouds.yaml as for the CLI/Terraform. The modules read it by cloud name (the OS_CLOUD variable).
Creating a server
The create.yml playbook runs locally and talks to the serverplus API:
- hosts: localhost
gather_facts: false
environment:
OS_CLOUD: serverplus
tasks:
- name: SSH key
openstack.cloud.keypair:
name: ansible-key
public_key_file: ~/.ssh/id_rsa.pub
- name: Server
openstack.cloud.server:
name: web-1
image: "Ubuntu 22.04"
flavor: m1.small
key_name: ansible-key
network: private
security_groups: [default]
auto_ip: true
register: srv
- debug:
var: srv.server.access_ipv4About networking. As everywhere: the server's network must have a router with an external gateway, otherwise there is no internet.
Dynamic inventory
An inventory.openstack.yml file — Ansible pulls your servers from OpenStack automatically:
plugin: openstack.cloud.openstack
expand_hostvars: true
all_projects: falseexport OS_CLOUD=serverplus
ansible-inventory -i inventory.openstack.yml --list
ansible -i inventory.openstack.yml all -m pingConfiguring servers over SSH
From here it is plain Ansible. An example site.yml: install and start nginx:
- hosts: all
become: true
tasks:
- name: nginx
apt:
name: nginx
state: present
update_cache: true
- name: start and enable
service:
name: nginx
state: started
enabled: trueConnection parameters are conveniently set in ansible.cfg:
[defaults]
remote_user = ubuntu
private_key_file = ~/.ssh/id_rsa
host_key_checking = FalseSecurity
- Keep
clouds.yamlprotected:chmod 600, out of git; use application credentials, not your account password. - Secrets go in ansible-vault (
ansible-vault encrypt), not in plaintext. - Separate SSH keys for automation, rotated periodically.
FAQ
"openstacksdk is required" error. Install openstacksdk into the same Python environment where Ansible lives.
The inventory is empty. Check OS_CLOUD=serverplus and that servers actually exist (openstack server list).
SSH won't connect. You need a floating IP, an open port 22 and the right user/key (for Ubuntu — ubuntu).