Skip to content

Commit c5c139c

Browse files
committed
Initial commit of the ansible patterns repo with the developer-keys and server-density-agent patterns initially.
0 parents  commit c5c139c

10 files changed

+219
-0
lines changed

README

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Ansible Patterns
2+
3+
This is a collection of useful patterns for [ansible](http://ansible.cc/), a server management and orchestration tool.
4+
5+
You can use ansible to do things such as installing packages, restarting services, uploading files and enabling apache sites.
6+
7+
## Getting started
8+
9+
First, [install ansible](http://ansible.cc/docs/gettingstarted.html) and create your
10+
ansible hosts (inventory) file with the details of the servers you're going to manage with ansible.
11+
12+
For convenience, here is the format of the hosts file:
13+
14+
SERVERNAME ansible_ssh_user=USERNAME ansible_ssh_host=IPADDRESS ansible_connection=ssh
15+
16+
where
17+
18+
- `SERVERNAME` is the nickname of the server,
19+
- `USERNAME` is the SSH username, and
20+
- `IPADDRESS` is the IP address of the machine.
21+
22+
For example, your hosts file might look like this:
23+
24+
web1 ansible_ssh_user=daviddoran ansible_ssh_host=192.0.2.1 ansible_connection=ssh
25+
26+
## Using a pattern
27+
28+
`cd` into the directory of the pattern:
29+
30+
cd developer-keys
31+
32+
Configure the example-playbook file (follow the instructions in the folder's README file).
33+
34+
Run the example playbook on your hosts file:
35+
36+
ansible-playbook -i hosts example-playbook.yml
37+
38+
where `hosts` is the path to your hosts file.
39+
40+
## Requirements
41+
42+
- ansible on your development computer
43+
- the ansible prerequisites on your servers (usually just Python 2.6)
44+
- a [hosts file](http://ansible.cc/docs/patterns.html) (containing the hostnames of your server)
45+
46+
## License
47+
48+
This project is released under the MIT License - see the LICENSE file for details.

developer-keys/README

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Developer Public Keys
2+
3+
This pattern handles:
4+
5+
- creating a developer's home directory
6+
- uploading a developer's public key to allow password-free access to the server
7+
- removing access and deleting the developer's home directory when they leave the company
8+
9+
### To grant a developer access:
10+
11+
- list their username in the `$current_developers` variable in example-playbook.yml
12+
- add their public key to the `keyfiles` folder (their username is the filename and the extension is .pub)
13+
14+
For example, to grant Jane Doe (using the username janedoe) access to the servers, the variables would contain:
15+
16+
```yml
17+
vars:
18+
current_developers: [janedoe]
19+
retired_developers: []
20+
```
21+
22+
and the keyfiles folder would contain:
23+
24+
janedoe.pub
25+
26+
Running the playbook will create the developers' home directories (once) and upload the public keys.
27+
28+
### To revoke a developer's access:
29+
30+
- list their username in the `$retired_developers` variable
31+
- remove their public key from the `keyfiles` folder
32+
33+
Running the playbook will **permanently delete** their home directory and revoke SSH access.

developer-keys/developer-keys.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Uploads developer public keys to *nix servers
2+
#
3+
# To give access to a new developer add their username
4+
# to the $current_developers variable in your main playbook.
5+
---
6+
# add developer user accounts
7+
- name: ensure account exists
8+
user: name=$item comment="" shell=/bin/bash createhome=yes
9+
with_items: ${current_developers}
10+
11+
# send the developers' public keys to the server to allow passwordless SSH login
12+
- name: add public key
13+
authorized_key: user=$item state=present key='$FILE(keyfiles/$item.pub)'
14+
with_items: ${current_developers}
15+
16+
# ################################################################
17+
# Only edit below this line when a developer has left the company.
18+
# ################################################################
19+
20+
# remove a developer's account (list their username in with_items)
21+
# WARNING: This will delete everything in the user's home directory.
22+
- name: remove developer's account (and thus their SSH access)
23+
authorized_key: name=$item comment="" state=absent remove=yes
24+
with_items: ${retired_developers}

developer-keys/example-playbook.yml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Example playbook (to demo developer-keys pattern)
2+
#
3+
# To run this playbook on the command line:
4+
# ansible-playbook -c ssh -i hosts example-playbook.yml
5+
# where hosts is the name of your ansible hosts file (inventory).
6+
#
7+
# Edit current_developers and retired_developers as required.
8+
---
9+
- hosts: all
10+
sudo: yes
11+
vars:
12+
current_developers: [daviddoran, janedoe, johnsmith]
13+
retired_developers: []
14+
tasks:
15+
- include: developer-keys.yml tags=developer-keys

developer-keys/keyfiles/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Developer public key files
2+
3+
Place your developers' public keys in this folder.
4+
5+
To generate a new public/private key pair:
6+
7+
ssh-keygen -t rsa -C "developer name"
8+
9+
By default this will create two files:
10+
11+
~/.ssh/id_rsa
12+
~/.ssh/id_rsa.pub
13+
14+
To enable ansible to copy the public keys to the server:
15+
16+
1. Copy the id_rsa.pub file (not id_rsa) to ./keyfiles/
17+
2. Rename the file to the developer's username (e.g. daviddoran.pub)
18+
19+
The username is the username of the account on the server. If the developer
20+
uses a different username on their dev computer then they'll need to manually
21+
give the username on the command line or add an alias in ~/.ssh/config

hosts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# ansible hosts file
2+
#SERVERNAME ansible_ssh_user=USERNAME ansible_ssh_host=IPADDRESS ansible_connection=ssh
3+
#web1 ansible_ssh_user=daviddoran ansible_ssh_host=192.0.2.1 ansible_connection=ssh

server-density-agent/README

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Server Density Agent
2+
3+
This pattern installs the [Server Density](http://www.serverdensity.com/) monitoring agent.
4+
5+
To install the agent:
6+
7+
- define your server density credentials (in your main playbook, as demonstrated in `example-playbook.yml`)
8+
- edit `server-density-agent.cfg.j2` to customize your monitoring agent setup *
9+
- run the playbook on your selected servers
10+
11+
\* Tip: You can create variables for the custom settings you add to `server-density-agent.cfg.j2` (e.g. MySQL / MongoDB) if the configuration varies by server.
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Example playbook (to demo setting up the server density agent)
2+
#
3+
# To run this playbook on the command line:
4+
# ansible-playbook -c ssh -i hosts example-playbook.yml
5+
# where hosts is the name of your ansible hosts file (inventory).
6+
#
7+
# Edit vars below with your server density credentials.
8+
---
9+
- hosts: all
10+
sudo: yes
11+
vars:
12+
# server density agent credentials
13+
server_density_url: ""
14+
server_density_agent_key: ""
15+
tasks:
16+
- include: server-density-agent.yml tags=server-density-agent
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
###############################################################################
2+
# Generated by ansible. WARNING: Do not edit this file on the server. #
3+
###############################################################################
4+
5+
[Main]
6+
sd_url: {{ server_density_url }}
7+
agent_key: {{ server_density_agent_key }}
8+
9+
# Plugins
10+
# Leave blank to ignore. See www.serverdensity.com/docs/agent/plugins/
11+
plugin_directory:
12+
13+
# Optional status monitoring
14+
# See www.serverdensity.com/docs/agent/
15+
# Ignore these if you do not wish to monitor them
16+
17+
# Apache
18+
# See http://support.serverdensity.com/customer/portal/articles/72259-configuring-apache-monitoring
19+
#apache_status_url: http://127.0.0.1/server-status?auto
20+
21+
# MySQL
22+
# See http://support.serverdensity.com/customer/portal/articles/72265-configuring-mysql-monitoring
23+
24+
#mysql_server: localhost
25+
#mysql_user: username
26+
#mysql_pass: password
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Install and set up the Server Density agent
2+
#
3+
# See http://www.serverdensity.com/
4+
---
5+
- name: install boxed ice's repo public key
6+
apt_key: url=https://www.serverdensity.com/downloads/boxedice-public.key state=present
7+
8+
- name: add boxed ice's repository as an apt source
9+
apt_repository: repo='deb http://www.serverdensity.com/downloads/linux/deb all main'
10+
11+
- name: install sd-agent package from boxed ice's repository
12+
apt: pkg=sd-agent state=latest update_cache=yes
13+
14+
- name: ensure mysql dependencies are present
15+
apt: pkg=python-dev state=present
16+
apt: pkg=python-mysqldb state=present
17+
18+
- name: copy the sd-agent config file
19+
template: src=server-density-agent.cfg.j2 dest=/etc/sd-agent/config.cfg owner=sd-agent mode=0644
20+
21+
- name: ensure sd-agent is running
22+
service: name=sd-agent state=started enabled=yes

0 commit comments

Comments
 (0)