Feat: init refact with test
This commit is contained in:
33
roles/users/.yamllint
Normal file
33
roles/users/.yamllint
Normal file
@@ -0,0 +1,33 @@
|
||||
---
|
||||
# Based on ansible-lint config
|
||||
extends: default
|
||||
|
||||
rules:
|
||||
braces:
|
||||
max-spaces-inside: 1
|
||||
level: error
|
||||
brackets:
|
||||
max-spaces-inside: 1
|
||||
level: error
|
||||
colons:
|
||||
max-spaces-after: -1
|
||||
level: error
|
||||
commas:
|
||||
max-spaces-after: -1
|
||||
level: error
|
||||
comments: disable
|
||||
comments-indentation: disable
|
||||
document-start: disable
|
||||
empty-lines:
|
||||
max: 3
|
||||
level: error
|
||||
hyphens:
|
||||
level: error
|
||||
indentation: disable
|
||||
key-duplicates: enable
|
||||
line-length: disable
|
||||
new-line-at-end-of-file: disable
|
||||
new-lines:
|
||||
type: unix
|
||||
trailing-spaces: disable
|
||||
truthy: disable
|
||||
38
roles/users/README.md
Normal file
38
roles/users/README.md
Normal file
@@ -0,0 +1,38 @@
|
||||
Role Name
|
||||
=========
|
||||
|
||||
A brief description of the role goes here.
|
||||
|
||||
Requirements
|
||||
------------
|
||||
|
||||
Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required.
|
||||
|
||||
Role Variables
|
||||
--------------
|
||||
|
||||
A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well.
|
||||
|
||||
Dependencies
|
||||
------------
|
||||
|
||||
A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles.
|
||||
|
||||
Example Playbook
|
||||
----------------
|
||||
|
||||
Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too:
|
||||
|
||||
- hosts: servers
|
||||
roles:
|
||||
- { role: username.rolename, x: 42 }
|
||||
|
||||
License
|
||||
-------
|
||||
|
||||
BSD
|
||||
|
||||
Author Information
|
||||
------------------
|
||||
|
||||
An optional section for the role authors to include contact information, or a website (HTML is not allowed).
|
||||
21
roles/users/defaults/main.yml
Normal file
21
roles/users/defaults/main.yml
Normal file
@@ -0,0 +1,21 @@
|
||||
---
|
||||
# defaults file for users
|
||||
user1:
|
||||
username: 'user1'
|
||||
password: 'user1'
|
||||
group: users
|
||||
groups:
|
||||
shell: '/bin/zsh'
|
||||
|
||||
user2:
|
||||
username: 'user2'
|
||||
password: 'user2'
|
||||
group: users
|
||||
groups: wheel
|
||||
system: yes
|
||||
|
||||
users: ["{{ user1 }}", "{{ user2 }}"]
|
||||
|
||||
public_key:
|
||||
- user: "{{ user1 }}"
|
||||
key: id_encrypt_fool.pub
|
||||
1
roles/users/files/id_encrypt_fool.pub
Normal file
1
roles/users/files/id_encrypt_fool.pub
Normal file
@@ -0,0 +1 @@
|
||||
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIFdDO8452/DpTR8taSKa/i+rgAvrYP9Fv9hYLMuphHQ+ user1@fool
|
||||
2
roles/users/handlers/main.yml
Normal file
2
roles/users/handlers/main.yml
Normal file
@@ -0,0 +1,2 @@
|
||||
---
|
||||
# handlers file for users
|
||||
23
roles/users/molecule/default/INSTALL.rst
Normal file
23
roles/users/molecule/default/INSTALL.rst
Normal file
@@ -0,0 +1,23 @@
|
||||
*********************************
|
||||
Vagrant driver installation guide
|
||||
*********************************
|
||||
|
||||
Requirements
|
||||
============
|
||||
|
||||
* Vagrant
|
||||
* Virtualbox, Parallels, VMware Fusion, VMware Workstation or VMware Desktop
|
||||
|
||||
Install
|
||||
=======
|
||||
|
||||
Please refer to the `Virtual environment`_ documentation for installation best
|
||||
practices. If not using a virtual environment, please consider passing the
|
||||
widely recommended `'--user' flag`_ when invoking ``pip``.
|
||||
|
||||
.. _Virtual environment: https://virtualenv.pypa.io/en/latest/
|
||||
.. _'--user' flag: https://packaging.python.org/tutorials/installing-packages/#installing-to-the-user-site
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
$ pip install 'molecule_vagrant'
|
||||
7
roles/users/molecule/default/converge.yml
Normal file
7
roles/users/molecule/default/converge.yml
Normal file
@@ -0,0 +1,7 @@
|
||||
---
|
||||
- name: Converge
|
||||
hosts: all
|
||||
tasks:
|
||||
- name: "Include users"
|
||||
include_role:
|
||||
name: "users"
|
||||
14
roles/users/molecule/default/molecule.yml
Normal file
14
roles/users/molecule/default/molecule.yml
Normal file
@@ -0,0 +1,14 @@
|
||||
---
|
||||
dependency:
|
||||
name: galaxy
|
||||
driver:
|
||||
name: vagrant
|
||||
platforms:
|
||||
- name: archlinux
|
||||
box: "archlinux/archlinux"
|
||||
- name: Debian
|
||||
box: "debian/bullseye64"
|
||||
provisioner:
|
||||
name: ansible
|
||||
verifier:
|
||||
name: testinfra
|
||||
Binary file not shown.
Binary file not shown.
22
roles/users/molecule/default/tests/conftest.py
Normal file
22
roles/users/molecule/default/tests/conftest.py
Normal file
@@ -0,0 +1,22 @@
|
||||
"""PyTest Fixtures."""
|
||||
from __future__ import absolute_import
|
||||
|
||||
import os
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def pytest_runtest_setup(item):
|
||||
"""Run tests only when under molecule with testinfra installed."""
|
||||
try:
|
||||
import testinfra
|
||||
except ImportError:
|
||||
pytest.skip("Test requires testinfra", allow_module_level=True)
|
||||
if "MOLECULE_INVENTORY_FILE" in os.environ:
|
||||
pytest.testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
|
||||
os.environ["MOLECULE_INVENTORY_FILE"]
|
||||
).get_hosts("all")
|
||||
else:
|
||||
pytest.skip(
|
||||
"Test should run only from inside molecule.", allow_module_level=True
|
||||
)
|
||||
10
roles/users/molecule/default/tests/test_default.py
Normal file
10
roles/users/molecule/default/tests/test_default.py
Normal file
@@ -0,0 +1,10 @@
|
||||
"""Role testing files using testinfra."""
|
||||
|
||||
|
||||
def test_hosts_file(host):
|
||||
"""Validate /etc/hosts file."""
|
||||
f = host.file("/etc/hosts")
|
||||
|
||||
assert f.exists
|
||||
assert f.user == "root"
|
||||
assert f.group == "root"
|
||||
26
roles/users/tasks/main.yml
Normal file
26
roles/users/tasks/main.yml
Normal file
@@ -0,0 +1,26 @@
|
||||
---
|
||||
# tasks file for users
|
||||
- name: users -- Ensure wheel group exists
|
||||
group:
|
||||
name: wheel
|
||||
state: present
|
||||
|
||||
- name: create users
|
||||
ansible.builtin.user:
|
||||
name: "{{ item.username }}"
|
||||
update_password: on_create
|
||||
password: "{{ item.password | password_hash('sha512')}}"
|
||||
group: "{{ item.group }}"
|
||||
groups: "{{ item.groups }}"
|
||||
shell: "{{ item.shell | default('/bin/bash') }}"
|
||||
state: present
|
||||
system: "{{ item.system | default('no') }}"
|
||||
createhome: "{{ item.createhome | default('no') }}"
|
||||
with_items: "{{ users }}"
|
||||
|
||||
- name: ssh -- Add public key
|
||||
authorized_key:
|
||||
user: "{{ item.user.username }}"
|
||||
key: "{{ lookup('file', item.key) }}"
|
||||
state: present
|
||||
with_items: "{{ public_key }}"
|
||||
2
roles/users/tests/inventory
Normal file
2
roles/users/tests/inventory
Normal file
@@ -0,0 +1,2 @@
|
||||
localhost
|
||||
|
||||
5
roles/users/tests/test.yml
Normal file
5
roles/users/tests/test.yml
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
- hosts: localhost
|
||||
remote_user: root
|
||||
roles:
|
||||
- users
|
||||
2
roles/users/vars/main.yml
Normal file
2
roles/users/vars/main.yml
Normal file
@@ -0,0 +1,2 @@
|
||||
---
|
||||
# vars file for users
|
||||
Reference in New Issue
Block a user