Skip to content

Commit

Permalink
Merge pull request #3 from Temelio/feature/skel_management
Browse files Browse the repository at this point in the history
Feature/skel management
  • Loading branch information
achaussier committed May 25, 2016
2 parents 29780f0 + 45a1053 commit c3bef67
Show file tree
Hide file tree
Showing 15 changed files with 259 additions and 25 deletions.
6 changes: 0 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ before_install:
[[ $(sudo grep secure_path /etc/sudoers) ]]
&& export rvmsudo_secure_path=1 || export rvmsudo_secure_path=0
install:

# Add ansible.cfg to pick up roles path.
- "cp ./tests/ansible.cfg ./"

script:
# Check the role/playbook's syntax.
- ansible-playbook -i tests/inventory tests/test_travis.yml --syntax-check
Expand Down Expand Up @@ -65,4 +60,3 @@ script:

notifications:
webhooks: https://galaxy.ansible.com/api/v1/notifications/

84 changes: 82 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

[![Build Status](https://travis-ci.org/Temelio/ansible-role-skel.svg?branch=master)](https://travis-ci.org/Temelio/ansible-role-skel)

Manage the skeleton structures used with account creation.
Manage the skeleton structures used with account creation or another file
prerequisites.

## Requirements

Expand Down Expand Up @@ -36,6 +37,86 @@ This role contains two tests methods :

### Default role variables

skel_default_owner: 'root'
skel_default_group: 'root'
skel_default_directory_mode: '0750'
skel_default_file_mode: '0640'

skel_default_link_force: False
skel_default_link_mode: '0640'
skel_default_link_state: 'link'

skel_entries: []

## How manage skels

Skels are defined in **skel_entries** var, and follow this syntax:

skel_entries:
- directories:
- path: '/etc/skel/.ssh'
mode: '0700'
to_remove:
- path: '/etc/skel/.bashrc'
- directories:
- path: '/etc/skel/foo'
- path: '/etc/skel/bar'
mode: '0755'
links:
- src: '/etc/skel/foo'
dest: '/etc/skel/bar/foolink'
to_copy:
- src: './files/foo.txt'
dest: '/etc/skel/bar/'
to_template:
- src: './files/bar.j2'
dest: '/etc/skel/bar/bar.txt'

All lv1 keys are optional, you can mix them as you want.

### to_remove key

Use it to remove files from structure. It's the first task. Only one arg:
* path *(mandatory)*: full element path

### directories key

It manage directories creation. You can use these sub keys:
* path *(mandatory)*: full directory path
* state *(optional)*: default is 'directory'
* owner *(optional)*: default is managed by *skel_default_owner* var
* group *(optional)*: default is managed by *skel_default_group* var
* mode *(optional)*: default is managed by *skel_default_directory_mode* var

### links key

It manage links creation. You can use these sub keys:
* src *(mandatory)*: full source path
* dest *(mandatory)*: full destination path
* state *(optional)*: default is managed by *skel_default_link_state* var
* owner *(optional)*: default is managed by *skel_default_owner* var
* group *(optional)*: default is managed by *skel_default_group* var
* mode *(optional)*: default is managed by *skel_default_link_mode* var
* force *(optional)*: default is managed by *skel_default_link_force* var

### to_copy key

It manage element to be copied. You can use these sub keys:
* src *(mandatory)*: full source path
* dest *(mandatory)*: full destination path
* owner *(optional)*: default is managed by *skel_default_owner* var
* group *(optional)*: default is managed by *skel_default_group* var
* mode *(optional)*: default is managed by *skel_default_file_mode* var

### to_template key

It manage templating. You can use these sub keys:
* src *(mandatory)*: full source path
* dest *(mandatory)*: full destination path
* owner *(optional)*: default is managed by *skel_default_owner* var
* group *(optional)*: default is managed by *skel_default_group* var
* mode *(optional)*: default is managed by *skel_default_file_mode* var

## Dependencies

None
Expand All @@ -55,4 +136,3 @@ MIT
Alexandre Chaussier (for Temelio company)
- http://www.temelio.com
- alexandre.chaussier [at] temelio.com

1 change: 0 additions & 1 deletion Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,3 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
end
end
end

10 changes: 10 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,13 @@

# Defaults vars file for skel role

skel_default_owner: 'root'
skel_default_group: 'root'
skel_default_directory_mode: '0750'
skel_default_file_mode: '0640'

skel_default_link_force: False
skel_default_link_mode: '0640'
skel_default_link_state: 'link'

skel_entries: []
65 changes: 58 additions & 7 deletions spec/installation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,67 @@

describe 'skel Ansible role' do

# Declare variables
packages = Array[]
describe 'first user home files' do

if ['debian', 'ubuntu'].include?(os[:family])
packages = Array[ 'apt' ]
describe file('/home/foo/.ssh') do
it { should_not exist }
end

describe file('/home/foo/.bashrc') do
it { should exist }
it { should be_file }
end

describe file('/home/foo/foo') do
it { should_not exist }
end

describe file('/home/foo/bar') do
it { should_not exist }
end
end

it 'install role packages' do
packages.each do |pkg_name|
expect(package(pkg_name)).to be_installed
describe 'second user home files' do

describe file('/home/bar/.ssh') do
it { should exist }
it { should be_directory }
it { should be_mode 700 }
end

describe file('/home/bar/.bashrc') do
it { should_not exist }
end

describe file('/home/bar/foo') do
it { should exist }
it { should be_directory }
it { should be_mode 750 }
end

describe file('/home/bar/bar') do
it { should exist }
it { should be_directory }
it { should be_mode 755 }
end

describe file('/home/bar/bar/foolink') do
it { should exist }
it { should be_symlink }
it { should be_linked_to '/home/bar/foo' }
end

describe file('/home/bar/bar/foo.txt') do
it { should exist }
it { should be_file }
it { should be_mode 640 }
end

describe file('/home/bar/bar/bar.txt') do
it { should exist }
it { should be_file }
it { should be_mode 640 }
its(:content) { should match /Ansible managed/ }
end
end
end
Expand Down
67 changes: 67 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,70 @@

# Main tasks file for skel role

- name: 'Manage skels element to be removed'
become: True
file:
path: "{{ item.1.path }}"
state: 'absent'
with_subelements:
- "{{ skel_entries }}"
- 'to_remove'
- skip_missing: True


- name: 'Manage skels directories'
become: True
file:
path: "{{ item.1.path }}"
state: "{{ item.1.state | default('directory') }}"
owner: "{{ item.1.owner | default(skel_default_owner) }}"
group: "{{ item.1.group | default(skel_default_group) }}"
mode: "{{ item.1.mode | default(skel_default_directory_mode) }}"
with_subelements:
- "{{ skel_entries }}"
- 'directories'
- skip_missing: True


- name: 'Manage skels links'
become: True
file:
src: "{{ item.1.src }}"
dest: "{{ item.1.dest }}"
state: "{{ item.1.state | default(skel_default_link_state)}}"
owner: "{{ item.1.owner | default(skel_default_owner) }}"
group: "{{ item.1.group | default(skel_default_group) }}"
mode: "{{ item.1.mode | default(skel_default_link_mode) }}"
force: "{{ item.1.force | default(skel_default_link_force) }}"
with_subelements:
- "{{ skel_entries }}"
- 'links'
- skip_missing: True


- name: 'Manage skels files to copy'
become: True
copy:
src: "{{ item.1.src }}"
dest: "{{ item.1.dest }}"
owner: "{{ item.1.owner | default(skel_default_owner) }}"
group: "{{ item.1.group | default(skel_default_group) }}"
mode: "{{ item.1.mode | default(skel_default_file_mode) }}"
with_subelements:
- "{{ skel_entries }}"
- 'to_copy'
- skip_missing: True


- name: 'Manage skels files to template'
become: True
template:
src: "{{ item.1.src }}"
dest: "{{ item.1.dest }}"
owner: "{{ item.1.owner | default(skel_default_owner) }}"
group: "{{ item.1.group | default(skel_default_group) }}"
mode: "{{ item.1.mode | default(skel_default_file_mode) }}"
with_subelements:
- "{{ skel_entries }}"
- 'to_template'
- skip_missing: True
4 changes: 0 additions & 4 deletions tests/ansible.cfg

This file was deleted.

1 change: 1 addition & 0 deletions tests/files/bar.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# {{ ansible_managed }}
Empty file added tests/files/foo.txt
Empty file.
2 changes: 0 additions & 2 deletions tests/test_common.yml

This file was deleted.

4 changes: 4 additions & 0 deletions tests/test_common_post_tasks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- name: 'Add second user once skel deployed'
become: True
user:
name: 'bar'
4 changes: 4 additions & 0 deletions tests/test_common_pre_tasks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- name: 'Add first user before skel deployment'
become: True
user:
name: 'foo'
6 changes: 5 additions & 1 deletion tests/test_travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@

- hosts: localhost
remote_user: root
pre_tasks:
- include: './test_common_pre_tasks.yml'
roles:
- "{{ role_name }}"
post_tasks:
- include: ./test_common.yml
- include: './test_common_post_tasks.yml'
vars:
role_name: "{{ lookup('env','TRAVIS_REPO_SLUG') }}"
vars_files:
- "./test_vars.yml"

7 changes: 5 additions & 2 deletions tests/test_vagrant.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

- hosts: all
remote_user: vagrant
pre_tasks:
- include: './test_common_pre_tasks.yml'
roles:
- "{{ role_name }}"
post_tasks:
- include: ./test_common.yml
- include: './test_common_post_tasks.yml'
vars:
role_name: "{{ lookup('env','ANSIBLE_ROLE_NAME') }}"

vars_files:
- "./test_vars.yml"
23 changes: 23 additions & 0 deletions tests/test_vars.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---

# Role test vars

skel_entries:
- directories:
- path: '/etc/skel/.ssh'
mode: '0700'
to_remove:
- path: '/etc/skel/.bashrc'
- directories:
- path: '/etc/skel/foo'
- path: '/etc/skel/bar'
mode: '0755'
links:
- src: '/etc/skel/foo'
dest: '/etc/skel/bar/foolink'
to_copy:
- src: './files/foo.txt'
dest: '/etc/skel/bar/'
to_template:
- src: './files/bar.j2'
dest: '/etc/skel/bar/bar.txt'

0 comments on commit c3bef67

Please sign in to comment.