Skip to content

Instantly share code, notes, and snippets.

@danrough
danrough / create_ssh_user.yml
Last active December 26, 2015 20:59
Getting a file based on a variable in Ansible
---
############################################################
# The lookup on line 9 is currenlty failing.
# Can anyone recommend how I can achieve the following?
# Is there a better way to go about doing what I've set out to do here?
############################################################
- name: Add user {{ user }}
user: name={{user}}
@danrough
danrough / main.yml
Created October 29, 2013 15:47
Using parameterised includes
---
# (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
# ^
@danrough
danrough / dictionary_items
Created November 26, 2013 16:15
Passing in a dictionary of variables
- 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 November 27, 2013 22:37
Shows an implementation of the ansible template task which is iterated upon using a list of dictionary objects.
- 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 / service_init_script.j2
Created November 27, 2013 22:41
Generic service init script written using a jinja2 template which is then called by a template task in an ansible play.
#!/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 / hostgroups.cfg.j2
Created February 12, 2014 12:29
A portion of a template used in one of our ansible playbooks to generate nagios hostgroup definitions file.
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 / test-environment
Created August 4, 2014 17:04
Create test environment using rax modules playbook
---
- 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 / main.yml
Created February 6, 2016 09:40
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.
---
- 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 February 6, 2016 20:56
Uses the meta module, introduced in Ansible 2.0, to refresh the inventory mid play
---
- 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"
---
- 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(',') }}"