Skip to content

Instantly share code, notes, and snippets.

View ianling's full-sized avatar

Ian ianling

View GitHub Profile
@ianling
ianling / backuppc_full_backup.yml
Last active December 9, 2015 22:00
Runs a full backup of all hosts using BackupPC
---
#first, dump all MySQL databases and move them to a folder that gets backed up by BackupPC
- hosts: mysql
tasks:
- name: Create /backup/sql recursively
file: path=/backup/sql state=directory owner=backup group=backup mode=0766 recurse=yes
- name: Dump all MySQL databases, where applicable
shell: "/usr/bin/mysqldump --all-databases > /backup/sql/mysql.sql"
@ianling
ianling / backuppc_inc_backup.yml
Created December 9, 2015 22:01
Run an incremental backup using BackupPC
---
- name: Incrementally back up host using BackupPC
hosts: all
remote_user: backup
tasks:
- name: Back up the host
local_action: command /usr/share/backuppc/bin/BackupPC_dump -i {{ inventory_hostname }}
@ianling
ianling / backup_user_setup.yml
Created December 9, 2015 22:03
Sets up a backup user with passwordless sudo. Sets up key-based ssh authentication automatically.
---
- name: Set up backup user to be used by Ansible
hosts: all
remote_user: <an existing user>
roles:
- yaegashi.blockinfile
tasks:
- name: Generate a unique key for this host
@ianling
ianling / change_root_pw.yml
Created December 10, 2015 00:39
Sets the root password of a remote host to a randomly generated one. The new password gets stored locally in plaintext at /backup/passwords/<hostname>
---
- name: Change the root password and store the updated password locally in /backup/passwords/<hostname>
hosts: all
remote_user: backup
vars:
plaintextpw: "{{ lookup('password', '/backup/passwords/'+inventory_hostname+' chars=ascii_letters,digits,hexdigits,punctuation,, length=20') }}"
tasks:
- name: Remove the old password file, if it exists
print 2 # ...
i = 3
while True:
divisorMax = (i/2)
prime = True
for divisor in range(3,divisorMax,2):
if i % divisor == 0:
prime = False
break
if prime:
@ianling
ianling / setup_user_sshcerts.yml
Created April 14, 2016 17:14
Creates SSH certificates for passwordless login
---
- name: Set up sshcerts for MYUSER
hosts: all
remote_user: MYUSER
tasks:
- name: Generate a unique key for this host
local_action: command /usr/bin/ssh-keygen -b 2048 -t rsa -f /home/MYUSER/sshkeys/{{ inventory_hostname }} -q -N ""
- name: Create /backup/.ssh recursively
@ianling
ianling / check_ssl_cert.yml
Created April 14, 2016 17:21
Runs a Bash script that checks SSL certificate expiry
---
- name: Check if SSL certs will expire soon
hosts: servers-with-ssl-certs
remote_user: backup
tasks:
- name: Run the SSL cert check script
script: /usr/local/bin/check_ssl_cert.sh
register: scriptoutput
- name: Print script output
@ianling
ianling / check_ssl_cert.sh
Last active April 14, 2016 17:25
Bash script that checks each Let's Encrypt cert to make sure it won't expire soon
#!/bin/bash
for cert in `find /etc/letsencrypt/live -name 'cert.pem'` ; do
certpath=`/usr/bin/dirname $cert`
website=`basename $certpath`
if /usr/bin/openssl x509 -checkend 2592000 -noout -in $cert ; then
echo "Cert for $website is good"
else
echo "**Cert for $website is bad**"
fi
done
@ianling
ianling / generate_ssh_certs.yml
Created October 3, 2017 16:49
Ansible Playbook for generating and adding SSH certificates for a user on remote hosts
---
- name: Set up sshcerts for ianl
hosts: all
remote_user: ianl
tasks:
- name: Generate a unique key for each host
local_action: command /usr/bin/ssh-keygen -b 2048 -t rsa -f /home/ianl/sshkeys/{{ inventory_hostname }} -q -N ""
become: no
@ianling
ianling / bootstrap.yml
Last active October 12, 2017 18:51
Used after backup_user_setup.yml to push basic config changes
---
- name: Roll out basic config changes to a new server
hosts: all
remote_user: backup
become: yes
tasks:
- name: Disable root login in sshd_config
lineinfile: "dest=/etc/ssh/sshd_config
regexp='^PermitRootLogin '