Skip to content

Instantly share code, notes, and snippets.

@wtcross
Created June 14, 2016 15:01
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wtcross/18fddda02db3b6a9da5a0cfadb36260e to your computer and use it in GitHub Desktop.
Save wtcross/18fddda02db3b6a9da5a0cfadb36260e to your computer and use it in GitHub Desktop.
Example of an Ansible playbook that interacts with OpenStack. It is runnable via Ansible and Ansible Tower.
```yaml
- name: Provision a compute instance
hosts: localhost
gather_facts: no
become: no
vars:
os_config_file: "{{ lookup('env', 'OS_CLIENT_CONFIG_FILE') }}"
os_auth:
project_name: "{{ lookup('env', 'OS_PROJECT') }}"
auth_url: "{{ lookup('env', 'OS_AUTH_URL') }}"
username: "{{ lookup('env', 'OS_USERNAME') }}"
password: "{{ lookup('env', 'OS_PASSWORD') }}"
tasks:
- name: Load Tower cloud credential config when it is present
include_vars: "{{ os_config_file }}"
when: os_config_file != ''
- name: Set necessary variables when Tower cloud credential config is present
set_fact:
os_auth:
auth_url: "{{ clouds.devstack.auth.auth_url }}"
username: "{{ clouds.devstack.auth.username }}"
password: "{{ clouds.devstack.auth.password }}"
project_name: "{{ clouds.devstack.auth.project_name }}"
when: os_config_file != ''
- name: Fail if fallback environment variables are not set
fail:
msg: "missing required environment variable: '{{ item }}'"
when: os_config_file == '' and lookup('env', item) is undefined
with_items:
- OS_AUTH_URL
- OS_USERNAME
- OS_PASSWORD
- OS_PROJECT
- name: Launch compute instances
os_server:
auth: "{{ os_auth }}"
name: compute-node-demo
auto_ip: no
image: rhel-7.2
flavor: m1.small
key_name: demo-key
nics:
- net-name: direct
security_groups:
- ssh
wait: no
state: present
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment