Skip to content

Instantly share code, notes, and snippets.

@ianling
Created December 9, 2015 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianling/f99fba4d63c1d3f2da7f to your computer and use it in GitHub Desktop.
Save ianling/f99fba4d63c1d3f2da7f to your computer and use it in GitHub Desktop.
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
local_action: command /usr/bin/ssh-keygen -b 2048 -t rsa -f /etc/ansible/sshkeys/{{ inventory_hostname }} -q -N ""
become: no
- name: Create /backup/.ssh recursively
file: path=/backup/.ssh state=directory owner=backup group=backup mode=0700 recurse=yes
- name: Change backup user home to /backup and shell to bash
user: name=backup
home=/backup
shell=/bin/bash
- name: Add public key to backup user
authorized_key: user=backup
key="{{ lookup('file', '/etc/ansible/sshkeys/'+inventory_hostname+'.pub') }}"
state=present
- name: Set up passwordless sudo for backup user
lineinfile: "dest=/etc/sudoers
regexp='^backup ALL'
line='backup ALL=(ALL:ALL) NOPASSWD: ALL'
state=present
insertafter=EOF"
- name: Disallow password login for SSH for backup user
blockinfile:
dest: /etc/ssh/sshd_config
block: |
Match User backup
PasswordAuthentication no
notify:
- restart ssh
handlers:
- name: restart ssh
service: name=ssh state=restarted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment