Skip to content

Commit

Permalink
Merge pull request #6 from ahelal/feature/extra_info
Browse files Browse the repository at this point in the history
Feature/extra info
  • Loading branch information
ahelal committed Sep 17, 2015
2 parents 471bddf + 6803994 commit 9000dfa
Show file tree
Hide file tree
Showing 19 changed files with 198 additions and 166 deletions.
42 changes: 29 additions & 13 deletions .kitchen.yml
Original file line number Diff line number Diff line change
@@ -1,52 +1,68 @@
---
driver :
name : localhost
# TRAVIS: local
# NO TRAVIS: vagrant
name : <%= if ENV['TRAVIS'] then 'localhost' else 'vagrant' end %>

provisioner:
name : ansible_push
verbose : "vvvv"
ansible_config : "test/ansible.cfg"
idempotency_test : True
sudo : True

# TRAVIS: True
# NO TRAVIS: False
sudo : <%= if ENV['TRAVIS'] then 'True' else 'False' end %>

platforms:
- name : travis
provisioner :
raw_arguments : "-c local"
# travistravis
- name : <%= if ENV['TRAVIS'] then 'travis' else 'ubuntu-14.04' end %>
provisioner :
raw_arguments : <%= if ENV['TRAVIS'] then '-c local' else '' end %>
# Should be safily ignored by Travis since connection local
driver:
box: ubuntu/trusty64
customize:
cpus : 2
memory : 2048


suites:
- name : group
provisioner :
playbook : "test/application/group.yml"
extra_vars : { 'kitchen_connection': 'smart' }
extra_vars : { 'kitchen_connection': 'smart', 'kitchen_hosts': '<%= if ENV['TRAVIS'] then 'localhost' else 'all' end %>' }

- name : simple
provisioner :
playbook : "test/application/simple.yml"
extra_vars : { 'kitchen_connection': 'smart' }
extra_vars : { 'kitchen_connection': 'smart', 'kitchen_hosts': '<%= if ENV['TRAVIS'] then 'localhost' else 'all' end %>' }

- name : advanced
provisioner :
playbook : "test/application/advanced.yml"
extra_vars : { 'kitchen_connection': 'smart' }
extra_vars : { 'kitchen_connection': 'smart', 'kitchen_hosts': '<%= if ENV['TRAVIS'] then 'localhost' else 'all' end %>' }

- name : source-files
provisioner :
playbook : "test/application/source_files.yml"
extra_vars : { 'kitchen_connection': 'smart' }
extra_vars : { 'kitchen_connection': 'smart', 'kitchen_hosts': '<%= if ENV['TRAVIS'] then 'localhost' else 'all' end %>' }

- name : source-databag
provisioner :
playbook : "test/application/source_databag.yml"
extra_vars : { 'kitchen_connection': 'smart' }
extra_vars : { 'kitchen_connection': 'smart', 'kitchen_hosts': '<%= if ENV['TRAVIS'] then 'localhost' else 'all' end %>' }

- name : multi-source-files
provisioner :
playbook : "test/application/multi_source_files.yml"
extra_vars : { 'kitchen_connection': 'smart' }
extra_vars : { 'kitchen_connection': 'smart', 'kitchen_hosts': '<%= if ENV['TRAVIS'] then 'localhost' else 'all' end %>' }

- name : team
provisioner :
playbook : "test/application/team.yml"
extra_vars : { 'kitchen_connection': 'smart' }
extra_vars : { 'kitchen_connection': 'smart', 'kitchen_hosts': '<%= if ENV['TRAVIS'] then 'localhost' else 'all' end %>' }

- name : extra-simple
provisioner :
playbook : "test/application/exta_simple.yml"
extra_vars : { 'kitchen_connection': 'smart', 'kitchen_hosts': '<%= if ENV['TRAVIS'] then 'localhost' else 'all' end %>' }
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ install:

script:
- ansible --version
- export TRAVIS=True
- bundle install
- bundle exec kitchen test travis

Expand Down
4 changes: 1 addition & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ source 'https://rubygems.org'


group :development do
# Use Berkshelf for resolving cookbook dependencies

gem 'test-kitchen'
gem 'kitchen-vagrant'
gem 'kitchen-ansiblepush'
gem 'kitchen-localhost'
end
end
36 changes: 32 additions & 4 deletions library/usersdb.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
#!/usr/bin/python

USERVALUES = [ 'append', 'comment', 'createhome', 'expires', 'force', 'generate_ssh_key', 'group', 'groups', 'home',
'login_class', 'move_home', 'name', 'non_unique', 'password', 'remove', 'shell', 'skeleton', 'ssh_key_bits',
'ssh_key_comment', 'ssh_key_file', 'ssh_key_passphrase', 'ssh_key_type', 'state', 'system', 'uid', 'update_password',
'keys']

class UsersDB(object):
def __init__(self, module):
self.module = module
self.users_db = self.module.params["usersdb"]
self.source_user_db = self.module.params["source_userdb"]
self.extract_extra_keys = self.module.params["extract_extra_keys"]
# If we have to userdb and source db lets merge them if not
if self.users_db and self.source_user_db:
self.users_db.update(self.source_user_db)
Expand All @@ -22,6 +27,7 @@ def __init__(self, module):
self.expanded_users_key_db = [] # Used in simple mode
self.expanded_server_db = [] # Used in advanced mode for merged User + server
self.expanded_server_key_db = [] # Used in advanced mode
self.extra_users_data = [] # Used for extra data that is not related to user module

def _concat_keys(self, user_name, user_keys=None, server_keys=None, user_status=False):
# Concat keys (if possible) and update username to keys
Expand Down Expand Up @@ -153,15 +159,31 @@ def expand_users(self):
# Get User database which is a dic and create expendaded_user_db and key_db
# Put keys in right dictionary format
for username, user_options in self.users_db.iteritems():
# 1- Convert dic to list (servers_db style)
user = {"name": username} # create the account name

# 1- Check for extra keys that dont translate to ansible user module
if self.extract_extra_keys:
extra_user_data = None
for dic_key in user_options.keys():
if dic_key not in USERVALUES:
# Add user and state
if not extra_user_data:
extra_user_data = dict(user)
extra_user_data.update({ "state": user_options.get("state", "present")})

extra_user_data.update({ dic_key: user_options[dic_key] })
user_options.pop(dic_key, None) # Remove item from user DB
# Add extras to a list if any
if extra_user_data:
self.extra_users_data.append(dict(extra_user_data))
# 2- Convert dic to list (servers_db style)
user.update(user_options) # update all other option
# 2- Compile key
# 3- Compile key
unformatted_keys = user_options.get("keys", [])
keys = self.expand_keys(unformatted_keys, user)
# 3- remove keys from userdb if exists
# 4- remove keys from userdb if exists
user.pop("keys", None)
# 4- Populate DBs
# 5- Populate DBs
self.expanded_users_db.append(user) # Populate new list user db
self.expanded_users_key_db.append({"user": username, "keys": keys})
if len(keys) > 0:
Expand All @@ -181,6 +203,11 @@ def main(self):
result = {"changed": False, "msg": "",
"users_db": self.expanded_users_db,
"key_db": self.expanded_users_key_db}

# Add extras if options
if self.extract_extra_keys:
result.update({ "extra" : self.extra_users_data })

self.module.exit_json(**result)


Expand All @@ -191,6 +218,7 @@ def main():
source_userdb=dict(default=None, required=False, type="dict"),
teamsdb=dict(default=None, required=False), # Should be dict but would break if value is false/none
serversdb=dict(default=None, required=False),
extract_extra_keys=dict(default=True, required=False),
),
supports_check_mode=False
)
Expand Down
6 changes: 6 additions & 0 deletions tasks/debug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
sudo: no
when: usermanage_debug_level >= 2

- name: debug | Display Extra
debug:
var=cuser.extra
sudo: no
when: usermanage_debug_level >= 2

- name: debug | Display lookup_key_db keys
debug:
var=cuser.lookup_key_db
Expand Down
22 changes: 0 additions & 22 deletions test/.kitchen.yml

This file was deleted.

2 changes: 1 addition & 1 deletion test/application/advanced.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- name : Advanced Mode (1)
hosts : localhost
hosts : "{{ kitchen_hosts }}"
gather_facts : no
sudo : True
connection : "{{ kitchen_connection | default('local') }}"
Expand Down
26 changes: 26 additions & 0 deletions test/application/exta_simple.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
- name : Extra user info Simple Mode (1)
hosts : "{{ kitchen_hosts }}"
gather_facts : no
sudo : True
connection : "{{ kitchen_connection | default('local') }}"
vars :
usermanage_debug : true
vars_files :
- "extra_var_simple_db.yml"
pre_tasks :
- debug: var=kitchen_connection
roles :
- "ansible-usermanage"
post_tasks :

- name: Print the user db in a yaml file
copy:
content="{{ cuser.users_db | to_yaml }}"
dest="/tmp/extra_simple_users_db.yaml"

- name: Print the extra user options in a yaml file
copy:
content="{{ cuser.extra | to_yaml }}"
dest="/tmp/extra_simple_extra_db.yaml"

39 changes: 39 additions & 0 deletions test/application/extra_var_simple_db.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
usermanage_usersdb:
daniels2:
comment: "Jack daniels"
keys:
- key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDLql1khoXEH/pThpLSDwJNBIEHkjrBggjEvRCqC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JfJAfp6K4HGV5uXITSMVDY9KBYvepolrlul"
shell: /bin/false
rum2:
comment: "Pirates Grog Rum"
keys:
- key: "ssh-rsa AAAAB3NzaC2EAA1ycAADAAAQABABAQCootXD63hexEQnbCmKGC7DjFSWxkqJ2neaC5S6POEdgwo7jQ60JWy0VCm5JS5d2ZNDQh+vi1wZ2cTr6n9X9bAkYhQ/eACmzYBjf8I8AXcqaigAOjRMLADU2qQfZmnRGyOLaGDI/EH52yBjeZHbgQdJOGrB07qgAu2facA2bd6kvI8eLwCx5yjqA+mInYEewRYrr5tUduGFdPhmyoKSGpaEeWWkLhafTj9eGRMSB3unBcMtux+LxXH4TfWgVBmWNVbr2Mcv+M6tYxix/iKniBLBUH/AfM/dTHlk38y2mjemUMUc/HBW+HmH3NXMwOks8po6Iohh8JNhywUlLKN9MvB7"
state: absent
stewart2:
comment: "Daniel Stewart"
home: /opt/stewart
keys:
- key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDhiDPE4S4K6AGJFRSIB5xVoIlKtgjVUVHK1WhT0hD/I3nZ3AFszqDXUz4eREDlPSfMsvNCuAd1Mxwg2vx1Udbzf0M5OH1DDgYyVeJXXB5/B2rpX7vm0A1Hxx17mMHg9OrCNKNn8B83g6IqAGM6P3VKHqnRQ9kLpPcki65gMx06R2dQ1Dh5kks2yOjyx7Mjut0rL9Ig/b9ysMMC1YjMupC8vb31Dhy8pVi1F/RT/7M6PwM4Kjh3fdzgqvQRxDrmky8kbXJj+TXU6pIM1mZtZyENddUCA0rDNtpi6yIaAR9aJkcPXxPpblkWjYAO++sukz88BKWt0Z+nLx9JUhwXtoW7"
vodaka2:
comment: "Savvy Vodka"
keys:
- key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC2Pq3M7AgeBjmNII6HGsKd2uaXFIfaljpNg1Rf6y3iGP84wC82bMfZTSIhnzJ4qwHj7Bzn8oiMmqtyeGcmEwLXm5f7mk3lj9NmxUEfbuWsPoSX4VHIney0F2cjrYRAHua8vZ50OrqYvRaBNttx+pCsub/Kw/t91PQvz7s5ML12DfhlfbE5f/g+ZrKHBxsn6Vw0VqN1Cx5cecaN+9NbdwTV25/RVsXC6v9TQlIqWR+znt4ZVxUSCTAbGc51tmauoleZee2XBkAO7xmJ7zPQEndhErq/zm0euZGx1xGIjQ7dVBK8t1ah2UdBS4pSHgjhDulo0hr4gIubQ0FSV+8cWCNN"
uid: 5300
vpn: true
raki2:
state: "absent"
comment: "Yeni Raki"
keys:
- key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCvy3wrbh9hwSP0WrwXSFW4AujinW+xiDQn7RLJ8UcVZ+Yf4EmV6GTvjjJjIegIKjnH2xiRDjZ1qaSZzxW7/uyVxPrjhGAE8iPQAGcLEEcpg4IwlFpd3+7NgCKLdVHozH6z+h3G95otKQZLmKq9lzkfVyhDzTjU2qsnSQPitDrPY/tngN1gnZHQm2CUh4gamtOfxtPKlOqQ/t0iXJU48QfyN33XWr8M5/2FWP9jDyvxEjpCRW2Qu2+8uQONPOHMtkLW70G2+KVN90fkOXMnDotm+aY1OluUcXgrBakEctHNIWoKSUbcdOw1JkLY3Ojl11lIdXn267A1t70FaEXG8cIf"
uid: 5400
aws: true
aws_group: Admin
brad2:
comment: "Brad"
2 changes: 1 addition & 1 deletion test/application/group.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- name : Group (1)
hosts : localhost
hosts : "{{ kitchen_hosts }}"
gather_facts : no
sudo : True
connection : "{{ kitchen_connection | default('local') }}"
Expand Down
2 changes: 1 addition & 1 deletion test/application/multi_source_files.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- name : Multi Sources files
hosts : localhost
hosts : "{{ kitchen_hosts }}"
gather_facts : no
sudo : True
connection : "{{ kitchen_connection | default('local') }}"
Expand Down
2 changes: 1 addition & 1 deletion test/application/simple.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- name : Simple Mode (1)
hosts : localhost
hosts : "{{ kitchen_hosts }}"
gather_facts : no
sudo : True
connection : "{{ kitchen_connection | default('local') }}"
Expand Down
2 changes: 1 addition & 1 deletion test/application/source_databag.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- name : Sources databag
hosts : localhost
hosts : "{{ kitchen_hosts }}"
gather_facts : no
sudo : True
connection : "{{ kitchen_connection | default('local') }}"
Expand Down
2 changes: 1 addition & 1 deletion test/application/source_files.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- name : Sources files
hosts : localhost
hosts : "{{ kitchen_hosts }}"
gather_facts : no
sudo : True
connection : "{{ kitchen_connection | default('local') }}"
Expand Down
2 changes: 1 addition & 1 deletion test/application/team.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
- name : Team (1)
hosts : localhost
hosts : "{{ kitchen_hosts }}"
gather_facts : no
sudo : True
connection : "{{ kitchen_connection | default('local') }}"
Expand Down
Loading

0 comments on commit 9000dfa

Please sign in to comment.