Skip to content

Commit

Permalink
Merge pull request #12 from johanmeiring/v2
Browse files Browse the repository at this point in the history
V2
  • Loading branch information
Johan Meiring authored Feb 1, 2017
2 parents 3ccf2c5 + 636761e commit 204e3a5
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 34 deletions.
11 changes: 8 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@
sudo: required

env:
- ansible: 1.9.6
- ansible: 2.0.2.0
distribution: ubuntu
version: 14.04
init: /sbin/init
run_opts: ""
- ansible: 2.0.2.0
- ansible: 2.1.0.0
distribution: ubuntu
version: 14.04
init: /sbin/init
run_opts: ""
- ansible: 2.1.0.0
- ansible: 2.2.1.0
distribution: ubuntu
version: 14.04
init: /sbin/init
run_opts: ""
- ansible: 2.2.1.0
distribution: ubuntu
version: 16.04
init: /sbin/init
run_opts: ""

services:
- docker
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ It is advisable that `scp_if_ssh` be set to `true` in the `ssh_connection` secti
scp_if_ssh=True
```

Other than that, only Ansible itself is required. Tested using Ansible 1.9, 2.0.2.0 and 2.1.0.0. Works on Ubuntu 14.04 and 16.04, untested on other versions.
Other than that, only Ansible itself is required. Tested using Ansible 2.0.2.0, 2.1.0.0 and 2.2.1.0. Works on Ubuntu 14.04 and 16.04, untested on other versions. Some work has been done on supporting RHEL, though this is not currently officially supported by the original author (further contributions are obviously welcome ;-)

## Role Variables

Expand Down
6 changes: 4 additions & 2 deletions handlers/main.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
---
- name: SFTP-Server | Restart sshd
service: name={{ 'ssh' if ansible_os_family == 'Debian' else 'sshd' }}
state=restarted
service:
name: "{{ 'ssh' if ansible_os_family == 'Debian' else 'sshd' }}"
state: restarted
ignore_errors: Yes
2 changes: 1 addition & 1 deletion meta/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ galaxy_info:
author: Johan Meiring
description: "Setup chrooted SFTP service on top of OpenSSH"
license: MIT
min_ansible_version: 1.9
min_ansible_version: 2.0

platforms:
- name: Ubuntu
Expand Down
63 changes: 37 additions & 26 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
---
# Creates group for SFTP users.
- name: SFTP-Server | Create sftp user group
group: name={{ sftp_group_name }}
state=present
group:
name: "{{ sftp_group_name }}"
state: present

# Necessary for chrooting of SFTP users.
- name: SFTP-Server | Alter sftp subsystem entry
lineinfile: dest=/etc/ssh/sshd_config
regexp="^Subsystem(\s+)sftp"
line="Subsystem sftp internal-sftp -f AUTH -l VERBOSE"
state=present
lineinfile:
dest: /etc/ssh/sshd_config
regexp: '^Subsystem(\s+)sftp'
line: "Subsystem sftp internal-sftp -f AUTH -l VERBOSE"
state: present
notify: SFTP-Server | Restart sshd

# Some sed magic to grab the entire sshd_config, excluding our custom config block.
Expand All @@ -19,7 +21,9 @@
changed_when: True == False

- name: SFTP-Server | Ensure SELinux management package is present
package: name=libsemanage-python state=present
package:
name: libsemanage-python
state: present
when: ansible_selinux

- name: SFTP-Server | Set SELinux booleans
Expand All @@ -34,48 +38,55 @@

# Create/recreate ssh_config.
- name: SFTP-Server | Apply sshd_config template
template: src=sshd_config.j2
dest=/etc/ssh/sshd_config
owner=root
template:
src: sshd_config.j2
dest: /etc/ssh/sshd_config
owner: root
notify: SFTP-Server | Restart sshd

# Create each SFTP user with home directory on the correct patition, and add to SFTP group.
- name: SFTP-Server | Create sftp users
user: name={{ item.name }}
groups={{ sftp_group_name }}
home="{{ sftp_home_partition }}/{{ item.name }}"
state=present
user:
name: "{{ item.name }}"
groups: "{{ sftp_group_name }}"
home: "{{ sftp_home_partition }}/{{ item.name }}"
state: present
with_items: "{{ sftp_users }}"

# A working chrooted SFTP setup requires root:sftgroup ownership of a user's home directory.
- name: SFTP-Server | Correct ownership and permission of home directories
file: path="{{ sftp_home_partition }}/{{ item.name }}"
owner=root
group={{ sftp_group_name }}
mode="0750"
file:
path: "{{ sftp_home_partition }}/{{ item.name }}"
owner: root
group: "{{ sftp_group_name }}"
mode: "0750"
with_items: "{{ sftp_users }}"

# Install all relevant public keys.
- name: SFTP-Server | Install public keys
authorized_key: user={{ item.0.name }} key='{{ lookup('file', item.1) }}'
authorized_key:
user: "{{ item.0.name }}"
key: "{{ lookup('file', item.1) }}"
with_subelements:
- "{{ sftp_users }}"
- authorized

# Update user passwords, if they were specified.
- name: SFTP-Server | Update user passwords
user: name={{ item.name }}
password={{ item.password }}
user:
name: "{{ item.name }}"
password: "{{ item.password }}"
with_items: "{{ sftp_users }}"
when: item.password is defined

# Create directories for SFTP users. Optional, but recommended.
- name: SFTP-Server | Create directories
file: path="{{ sftp_home_partition }}/{{ item[0].name }}/{{ item[1].name | default(item[1]) }}"
owner={{ item[0].name }}
group={{ item[0].name }}
mode="{{ item[1].mode | default(0750) }}"
state=directory
file:
path: "{{ sftp_home_partition }}/{{ item[0].name }}/{{ item[1].name | default(item[1]) }}"
owner: "{{ item[0].name }}"
group: "{{ item[0].name }}"
mode: "{{ item[1].mode | default(0750) }}"
state: directory
with_nested:
- "{{ sftp_users }}"
- "{{ sftp_directories }}"
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ RUN apt-get install -y openssh-server

# Install Ansible
RUN apt-get install -y software-properties-common git python-pip python-dev libffi-dev libssl-dev
RUN pip install 'ansible==1.9.6'
RUN pip install -U setuptools
RUN pip install 'ansible==2.2.1.0'

# Install Ansible inventory file
RUN mkdir /etc/ansible/ && echo "[local]\nlocalhost ansible_connection=local" > /etc/ansible/hosts
13 changes: 13 additions & 0 deletions tests/Dockerfile.ubuntu-16.04.ansible-2.2.1.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM ubuntu:16.04
RUN apt-get update

# Install OpenSSH server
RUN apt-get install -y openssh-server

# Install Ansible
RUN apt-get install -y software-properties-common git python-pip python-dev libffi-dev libssl-dev
RUN pip install -U setuptools
RUN pip install 'ansible==2.2.1.0'

# Install Ansible inventory file
RUN mkdir /etc/ansible/ && echo "[local]\nlocalhost ansible_connection=local" > /etc/ansible/hosts

0 comments on commit 204e3a5

Please sign in to comment.