Skip to content

Instantly share code, notes, and snippets.

View dictionary-lookup.yml
---
- hosts: localhost
gather_facts: no
vars:
database_clusters:
- { cluster_name: "test_lon", port: 5433 }
- { cluster_name: "prod_lon", port: 5434 }
tasks:
- name: Port for {{ environment }}_{{ region | lower }}
debug: var="{{ database_clusters | selectattr('cluster_name', 'equalto', '{{ environment }}_{{ region | lower }}') | map(attribute='port') | join(',') }}"
View squid-servers.j2
{%
for host in hostvars[target_environment + '-' + region]("app_servers")
if (is even(loop.index)) %}
write_the_app_02_squid_address_here
{% else %}
write_the_app_01_squid_address_here
{% endfor %}
@danrough
danrough / main.yml
Created Feb 6, 2016
Uses the meta module, introduced in Ansible 2.0, to refresh the inventory mid play
View main.yml
---
- name: App servers
local_action:
module: rax
credentials: ~/.rackspace_cloud_credentials
region: "{{ region }}"
name: "{{ target_environment }}-{{ region }}-app-%02d"
count: "{{ app_server_count }}"
exact_count: yes
group: "{{ target_environment }}_app_servers"
@danrough
danrough / main.yml
Created Feb 6, 2016
Creates a server and registers the new server's facts to an ansible variable. Uses the registered variable to perform an additional task on the newly created server.
View main.yml
---
- name: App servers
local_action:
module: rax
credentials: ~/.rackspace_cloud_credentials
region: "{{ region }}"
name: "{{ target_environment }}-{{ region }}-app-%02d"
count: "{{ app_server_count }}"
exact_count: yes
group: "{{ target_environment }}_app_servers"
@danrough
danrough / test-environment
Created Aug 4, 2014
Create test environment using rax modules playbook
View test-environment
---
- name: Gather information from the inventory file about all hosts for later use
hosts: all
gather_facts: no
- name: Build test infrastructure
hosts: localhost
gather_facts: no
connection: local
@danrough
danrough / hostgroups.cfg.j2
Created Feb 12, 2014
A portion of a template used in one of our ansible playbooks to generate nagios hostgroup definitions file.
View hostgroups.cfg.j2
define hostgroup {
hostgroup_name web_servers
alias Web servers
members {% for host in groups['web'] %}{{ hostvars[host]['ansible_ssh_host'] }}{% if not loop.last %}, {% endif %}{% endfor %}
}
@danrough
danrough / service_init_script.j2
Created Nov 27, 2013
Generic service init script written using a jinja2 template which is then called by a template task in an ansible play.
View service_init_script.j2
#!/bin/bash
### BEGIN INIT INFO
# Provides: {{ item.service_name }}
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: {{ item.service_description }}
### END INIT INFO
@danrough
danrough / main.yml
Created Nov 27, 2013
Shows an implementation of the ansible template task which is iterated upon using a list of dictionary objects.
View main.yml
- name: Create the service init scripts yo
template: src=service_init_script.j2 dest=/etc/init.d/{{ item.service_name }} owner=root group=root mode=755
with_items:
- { service_name: paymentHandler, service_description: 'Handles payments', service_user: blackjack_attack, service_exec: "{{ application_directory }}/apps/paymentHandler.js" }
- { service_name: tablePool, service_description: 'The pool of tables available', service_user: blackjack_attack, service_exec: "{{ application_directory }}/apps/tablePool.js" }
- { service_name: userManager, service_description: 'Manages users, apparently', service_user: blackjack_attack, service_exec: "{{ application_directory }}/apps/userManager.js" }
sudo: yes
@danrough
danrough / dictionary_items
Created Nov 26, 2013
Passing in a dictionary of variables
View dictionary_items
- name: Spin up the services
template: src=service_init.j2 dest=/etc/init.d/{{ item.service_name }} owner=root group=root mode=777
with_items:
- { service_name: paymentHandler, service_user: blackjack_attack, service_exec: "{{ application_directory }}/apps/paymentHandler.js" }
@danrough
danrough / main.yml
Created Oct 29, 2013
Using parameterised includes
View main.yml
---
# (Ansible version 1.4)
#
# ERROR: Syntax Error while loading YAML script, /Users/danrough/Documents/Development/OpsScripts/ansible/base-build/roles/common/tasks/main.yml
# Note: The error may actually appear before this position: line 14, column 3
#
# - { include: user.yml, user: {{ users.username }}, sudo: {{ users.isSudo }} }
# with_items: users
# ^
You can’t perform that action at this time.