This commit is contained in:
commit
177429a353
109
.drone.jsonnet
Normal file
109
.drone.jsonnet
Normal file
@ -0,0 +1,109 @@
|
||||
local AnsibleVersions(version="latest", package="ansible") = {
|
||||
name: "ansible-" + version,
|
||||
image: "python:3.7",
|
||||
pull: "always",
|
||||
environment: {
|
||||
PY_COLORS: 1
|
||||
},
|
||||
commands: [
|
||||
"pip install " + package + " ansible-later~=0.2.0 -qq",
|
||||
"git clone https://gitea.rknet.org/ansible/ansible-later-policy.git ~/policy",
|
||||
"ansible-later -c ~/policy/config.yml"
|
||||
],
|
||||
depends_on: [
|
||||
"clone",
|
||||
],
|
||||
};
|
||||
|
||||
local PipelineLinting = {
|
||||
kind: "pipeline",
|
||||
name: "linting",
|
||||
platform: {
|
||||
os: "linux",
|
||||
arch: "amd64",
|
||||
},
|
||||
steps: [
|
||||
AnsibleVersions(version="latest", package="ansible"),
|
||||
AnsibleVersions(version="master", package="git+https://github.com/ansible/ansible.git@devel"),
|
||||
],
|
||||
trigger: {
|
||||
ref: ["refs/heads/master", "refs/tags/**", "refs/pull/**"],
|
||||
},
|
||||
};
|
||||
|
||||
local PipelineDeployment = {
|
||||
kind: "pipeline",
|
||||
name: "deployment",
|
||||
platform: {
|
||||
os: "linux",
|
||||
arch: "amd64",
|
||||
},
|
||||
workspace: {
|
||||
base: "/drone/src",
|
||||
path: "xoxys.scan2mail"
|
||||
},
|
||||
steps: [
|
||||
{
|
||||
name: "molecule",
|
||||
image: "xoxys/molecule:ec2-linux-amd64",
|
||||
pull: "always",
|
||||
environment: {
|
||||
ANSIBLE_ROLES_PATH: "/drone/src",
|
||||
AWS_ACCESS_KEY_ID: { "from_secret": "aws_access_key_id" },
|
||||
AWS_SECRET_ACCESS_KEY: { "from_secret": "aws_secret_access_key" },
|
||||
AWS_REGION: "eu-central-1",
|
||||
MOLECULE_CUSTOM_MODULES_REPO: "https://gitea.rknet.org/ansible/custom_modules",
|
||||
MOLECULE_CUSTOM_FILTERS_REPO: "https://gitea.rknet.org/ansible/custom_filters",
|
||||
PY_COLORS: 1
|
||||
},
|
||||
commands: [
|
||||
"/bin/bash /docker-entrypoint.sh",
|
||||
"molecule test --scenario-name ec2-centos-7",
|
||||
],
|
||||
},
|
||||
],
|
||||
depends_on: [
|
||||
"linting",
|
||||
],
|
||||
trigger: {
|
||||
ref: ["refs/heads/master", "refs/tags/**"],
|
||||
},
|
||||
};
|
||||
|
||||
local PipelineNotifications = {
|
||||
kind: "pipeline",
|
||||
name: "notifications",
|
||||
platform: {
|
||||
os: "linux",
|
||||
arch: "amd64",
|
||||
},
|
||||
clone: {
|
||||
disable: true,
|
||||
},
|
||||
steps: [
|
||||
{
|
||||
name: "matrix",
|
||||
image: "plugins/matrix",
|
||||
settings: {
|
||||
homeserver: "https://matrix.rknet.org",
|
||||
roomid: "MtidqQXWWAtQcByBhH:rknet.org",
|
||||
template: "Status: **{{ build.status }}**<br/> Build: [{{ repo.Owner }}/{{ repo.Name }}]({{ build.link }}) ({{ build.branch }}) by {{ build.author }}<br/> Message: {{ build.message }}",
|
||||
username: { "from_secret": "matrix_username" },
|
||||
password: { "from_secret": "matrix_password" },
|
||||
},
|
||||
},
|
||||
],
|
||||
depends_on: [
|
||||
"deployment",
|
||||
],
|
||||
trigger: {
|
||||
status: [ "success", "failure" ],
|
||||
ref: ["refs/heads/master", "refs/tags/**"],
|
||||
},
|
||||
};
|
||||
|
||||
[
|
||||
PipelineLinting,
|
||||
PipelineDeployment,
|
||||
PipelineNotifications,
|
||||
]
|
116
.drone.yml
Normal file
116
.drone.yml
Normal file
@ -0,0 +1,116 @@
|
||||
---
|
||||
kind: pipeline
|
||||
name: linting
|
||||
|
||||
platform:
|
||||
os: linux
|
||||
arch: amd64
|
||||
|
||||
steps:
|
||||
- name: ansible-latest
|
||||
pull: always
|
||||
image: python:3.7
|
||||
commands:
|
||||
- pip install ansible ansible-later~=0.2.0 -qq
|
||||
- git clone https://gitea.rknet.org/ansible/ansible-later-policy.git ~/policy
|
||||
- ansible-later -c ~/policy/config.yml
|
||||
environment:
|
||||
PY_COLORS: 1
|
||||
depends_on:
|
||||
- clone
|
||||
|
||||
- name: ansible-master
|
||||
pull: always
|
||||
image: python:3.7
|
||||
commands:
|
||||
- "pip install git+https://github.com/ansible/ansible.git@devel ansible-later~=0.2.0 -qq"
|
||||
- git clone https://gitea.rknet.org/ansible/ansible-later-policy.git ~/policy
|
||||
- ansible-later -c ~/policy/config.yml
|
||||
environment:
|
||||
PY_COLORS: 1
|
||||
depends_on:
|
||||
- clone
|
||||
|
||||
trigger:
|
||||
ref:
|
||||
- refs/heads/master
|
||||
- "refs/tags/**"
|
||||
- "refs/pull/**"
|
||||
|
||||
---
|
||||
kind: pipeline
|
||||
name: deployment
|
||||
|
||||
platform:
|
||||
os: linux
|
||||
arch: amd64
|
||||
|
||||
workspace:
|
||||
base: /drone/src
|
||||
path: xoxys.scan2mail
|
||||
|
||||
steps:
|
||||
- name: molecule
|
||||
pull: always
|
||||
image: xoxys/molecule:ec2-linux-amd64
|
||||
commands:
|
||||
- /bin/bash /docker-entrypoint.sh
|
||||
- molecule test --scenario-name ec2-centos-7
|
||||
environment:
|
||||
ANSIBLE_ROLES_PATH: /drone/src
|
||||
AWS_ACCESS_KEY_ID:
|
||||
from_secret: aws_access_key_id
|
||||
AWS_REGION: eu-central-1
|
||||
AWS_SECRET_ACCESS_KEY:
|
||||
from_secret: aws_secret_access_key
|
||||
MOLECULE_CUSTOM_FILTERS_REPO: https://gitea.rknet.org/ansible/custom_filters
|
||||
MOLECULE_CUSTOM_MODULES_REPO: https://gitea.rknet.org/ansible/custom_modules
|
||||
PY_COLORS: 1
|
||||
|
||||
trigger:
|
||||
ref:
|
||||
- refs/heads/master
|
||||
- "refs/tags/**"
|
||||
|
||||
depends_on:
|
||||
- linting
|
||||
|
||||
---
|
||||
kind: pipeline
|
||||
name: notifications
|
||||
|
||||
platform:
|
||||
os: linux
|
||||
arch: amd64
|
||||
|
||||
clone:
|
||||
disable: true
|
||||
|
||||
steps:
|
||||
- name: matrix
|
||||
image: plugins/matrix
|
||||
settings:
|
||||
homeserver: https://matrix.rknet.org
|
||||
password:
|
||||
from_secret: matrix_password
|
||||
roomid: MtidqQXWWAtQcByBhH:rknet.org
|
||||
template: "Status: **{{ build.status }}**<br/> Build: [{{ repo.Owner }}/{{ repo.Name }}]({{ build.link }}) ({{ build.branch }}) by {{ build.author }}<br/> Message: {{ build.message }}"
|
||||
username:
|
||||
from_secret: matrix_username
|
||||
|
||||
trigger:
|
||||
ref:
|
||||
- refs/heads/master
|
||||
- "refs/tags/**"
|
||||
status:
|
||||
- success
|
||||
- failure
|
||||
|
||||
depends_on:
|
||||
- deployment
|
||||
|
||||
---
|
||||
kind: signature
|
||||
hmac: 82202899aeadf908ad1114336972d9231b9f7a6daba1a7dcb88d523660d7cc9a
|
||||
|
||||
...
|
11
.gitignore
vendored
Normal file
11
.gitignore
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
# ---> Ansible
|
||||
*.retry
|
||||
filter/plugins/
|
||||
library
|
||||
|
||||
# ---> Python
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
8
LICENSE
Normal file
8
LICENSE
Normal file
@ -0,0 +1,8 @@
|
||||
MIT License
|
||||
Copyright (c) <year> <copyright holders>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
18
defaults/main.yml
Normal file
18
defaults/main.yml
Normal file
@ -0,0 +1,18 @@
|
||||
---
|
||||
scan2mail_packages_extra: []
|
||||
scan2mail_mail_domain: example.com
|
||||
scan2mail_from_address: "printer@{{ scan2mail_mail_domain }}"
|
||||
scan2mail_subject: "Scan2Mail attachments"
|
||||
# scan2mail_message: defaults to not set
|
||||
|
||||
scan2mail_incron_system_table_dir: /etc/incron.d
|
||||
|
||||
scan2mail_incron_jobs: []
|
||||
## Example:
|
||||
# scan2mail_incron_jobs:
|
||||
# - path: /path/to/scan/dir
|
||||
# mask: IN_CREATE
|
||||
# action: scan2mail
|
||||
# options:
|
||||
# - $@
|
||||
# - $#
|
10
handlers/main.yml
Normal file
10
handlers/main.yml
Normal file
@ -0,0 +1,10 @@
|
||||
---
|
||||
- name: Restart service
|
||||
systemd:
|
||||
name: "{{ __scan2mail_incron_service }}"
|
||||
state: restarted
|
||||
daemon_reload: yes
|
||||
enabled: yes
|
||||
listen: __incron_restart
|
||||
become: True
|
||||
become_user: root
|
15
meta/main.yml
Normal file
15
meta/main.yml
Normal file
@ -0,0 +1,15 @@
|
||||
# Standards: 0.1
|
||||
---
|
||||
galaxy_info:
|
||||
author: xoxys
|
||||
description:
|
||||
license: MIT
|
||||
min_ansible_version: 2.4
|
||||
platforms:
|
||||
- name: EL
|
||||
versions:
|
||||
- 7
|
||||
galaxy_tags:
|
||||
- incron
|
||||
- scan2mail
|
||||
dependencies:
|
22
molecule/default/INSTALL.rst
Normal file
22
molecule/default/INSTALL.rst
Normal file
@ -0,0 +1,22 @@
|
||||
*******
|
||||
Amazon Web Services driver installation guide
|
||||
*******
|
||||
|
||||
Requirements
|
||||
============
|
||||
|
||||
* An AWS credentials rc file
|
||||
|
||||
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[ec2]'
|
125
molecule/default/create.yml
Normal file
125
molecule/default/create.yml
Normal file
@ -0,0 +1,125 @@
|
||||
---
|
||||
- name: Create
|
||||
hosts: localhost
|
||||
connection: local
|
||||
gather_facts: false
|
||||
no_log: "{{ not (lookup('env', 'MOLECULE_DEBUG') | bool or molecule_yml.provisioner.log|default(false) | bool) }}"
|
||||
vars:
|
||||
ssh_user: ubuntu
|
||||
ssh_port: 22
|
||||
|
||||
security_group_name: molecule
|
||||
security_group_description: Security group for testing Molecule
|
||||
security_group_rules:
|
||||
- proto: tcp
|
||||
from_port: "{{ ssh_port }}"
|
||||
to_port: "{{ ssh_port }}"
|
||||
cidr_ip: '0.0.0.0/0'
|
||||
- proto: icmp
|
||||
from_port: 8
|
||||
to_port: -1
|
||||
cidr_ip: '0.0.0.0/0'
|
||||
security_group_rules_egress:
|
||||
- proto: -1
|
||||
from_port: 0
|
||||
to_port: 0
|
||||
cidr_ip: '0.0.0.0/0'
|
||||
|
||||
keypair_name: molecule_key
|
||||
keypair_path: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}/ssh_key"
|
||||
tasks:
|
||||
- name: Create security group
|
||||
ec2_group:
|
||||
name: "{{ security_group_name }}"
|
||||
description: "{{ security_group_name }}"
|
||||
rules: "{{ security_group_rules }}"
|
||||
rules_egress: "{{ security_group_rules_egress }}"
|
||||
|
||||
- name: Test for presence of local keypair
|
||||
stat:
|
||||
path: "{{ keypair_path }}"
|
||||
register: keypair_local
|
||||
|
||||
- name: Delete remote keypair
|
||||
ec2_key:
|
||||
name: "{{ keypair_name }}"
|
||||
state: absent
|
||||
when: not keypair_local.stat.exists
|
||||
|
||||
- name: Create keypair
|
||||
ec2_key:
|
||||
name: "{{ keypair_name }}"
|
||||
register: keypair
|
||||
|
||||
- name: Persist the keypair
|
||||
copy:
|
||||
dest: "{{ keypair_path }}"
|
||||
content: "{{ keypair.key.private_key }}"
|
||||
mode: 0600
|
||||
when: keypair.changed
|
||||
|
||||
- name: Create molecule instance(s)
|
||||
ec2:
|
||||
key_name: "{{ keypair_name }}"
|
||||
image: "{{ item.image }}"
|
||||
instance_type: "{{ item.instance_type }}"
|
||||
vpc_subnet_id: "{{ item.vpc_subnet_id }}"
|
||||
group: "{{ security_group_name }}"
|
||||
instance_tags:
|
||||
instance: "{{ item.name }}"
|
||||
wait: true
|
||||
assign_public_ip: true
|
||||
exact_count: 1
|
||||
count_tag:
|
||||
instance: "{{ item.name }}"
|
||||
register: server
|
||||
with_items: "{{ molecule_yml.platforms }}"
|
||||
async: 7200
|
||||
poll: 0
|
||||
|
||||
- name: Wait for instance(s) creation to complete
|
||||
async_status:
|
||||
jid: "{{ item.ansible_job_id }}"
|
||||
register: ec2_jobs
|
||||
until: ec2_jobs.finished
|
||||
retries: 300
|
||||
with_items: "{{ server.results }}"
|
||||
|
||||
# Mandatory configuration for Molecule to function.
|
||||
|
||||
- name: Populate instance config dict
|
||||
set_fact:
|
||||
instance_conf_dict: {
|
||||
'instance': "{{ item.instances[0].tags.instance }}",
|
||||
'address': "{{ item.instances[0].public_ip }}",
|
||||
'user': "{{ ssh_user }}",
|
||||
'port': "{{ ssh_port }}",
|
||||
'identity_file': "{{ keypair_path }}",
|
||||
'instance_ids': "{{ item.instance_ids }}", }
|
||||
with_items: "{{ ec2_jobs.results }}"
|
||||
register: instance_config_dict
|
||||
when: server.changed | bool
|
||||
|
||||
- name: Convert instance config dict to a list
|
||||
set_fact:
|
||||
instance_conf: "{{ instance_config_dict.results | map(attribute='ansible_facts.instance_conf_dict') | list }}"
|
||||
when: server.changed | bool
|
||||
|
||||
- name: Dump instance config
|
||||
copy:
|
||||
content: "{{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}"
|
||||
dest: "{{ molecule_instance_config }}"
|
||||
when: server.changed | bool
|
||||
|
||||
- name: Wait for SSH
|
||||
wait_for:
|
||||
port: "{{ ssh_port }}"
|
||||
host: "{{ item.address }}"
|
||||
search_regex: SSH
|
||||
delay: 10
|
||||
timeout: 320
|
||||
with_items: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}"
|
||||
|
||||
- name: Wait for boot process to finish
|
||||
pause:
|
||||
minutes: 2
|
47
molecule/default/destroy.yml
Normal file
47
molecule/default/destroy.yml
Normal file
@ -0,0 +1,47 @@
|
||||
---
|
||||
- name: Destroy
|
||||
hosts: localhost
|
||||
connection: local
|
||||
gather_facts: false
|
||||
no_log: "{{ not (lookup('env', 'MOLECULE_DEBUG') | bool or molecule_yml.provisioner.log|default(false) | bool) }}"
|
||||
tasks:
|
||||
- block:
|
||||
- name: Populate instance config
|
||||
set_fact:
|
||||
instance_conf: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}"
|
||||
skip_instances: false
|
||||
rescue:
|
||||
- name: Populate instance config when file missing
|
||||
set_fact:
|
||||
instance_conf: {}
|
||||
skip_instances: true
|
||||
|
||||
- name: Destroy molecule instance(s)
|
||||
ec2:
|
||||
state: absent
|
||||
instance_ids: "{{ item.instance_ids }}"
|
||||
register: server
|
||||
with_items: "{{ instance_conf }}"
|
||||
when: not skip_instances
|
||||
async: 7200
|
||||
poll: 0
|
||||
|
||||
- name: Wait for instance(s) deletion to complete
|
||||
async_status:
|
||||
jid: "{{ item.ansible_job_id }}"
|
||||
register: ec2_jobs
|
||||
until: ec2_jobs.finished
|
||||
retries: 300
|
||||
with_items: "{{ server.results }}"
|
||||
|
||||
# Mandatory configuration for Molecule to function.
|
||||
|
||||
- name: Populate instance config
|
||||
set_fact:
|
||||
instance_conf: {}
|
||||
|
||||
- name: Dump instance config
|
||||
copy:
|
||||
content: "{{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}"
|
||||
dest: "{{ molecule_instance_config }}"
|
||||
when: server.changed | bool
|
20
molecule/default/molecule.yml
Normal file
20
molecule/default/molecule.yml
Normal file
@ -0,0 +1,20 @@
|
||||
---
|
||||
dependency:
|
||||
name: galaxy
|
||||
driver:
|
||||
name: ec2
|
||||
lint:
|
||||
name: yamllint
|
||||
platforms:
|
||||
- name: instance
|
||||
image: ami-a5b196c0
|
||||
instance_type: t2.micro
|
||||
vpc_subnet_id: subnet-6456fd1f
|
||||
provisioner:
|
||||
name: ansible
|
||||
lint:
|
||||
name: ansible-lint
|
||||
verifier:
|
||||
name: testinfra
|
||||
lint:
|
||||
name: flake8
|
5
molecule/default/playbook.yml
Normal file
5
molecule/default/playbook.yml
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
- name: Converge
|
||||
hosts: all
|
||||
roles:
|
||||
- role: xoxys.scan2mail
|
9
molecule/default/prepare.yml
Normal file
9
molecule/default/prepare.yml
Normal file
@ -0,0 +1,9 @@
|
||||
---
|
||||
- name: Prepare
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Install python for Ansible
|
||||
raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal python-zipstream)
|
||||
become: true
|
||||
changed_when: false
|
14
molecule/default/tests/test_default.py
Normal file
14
molecule/default/tests/test_default.py
Normal file
@ -0,0 +1,14 @@
|
||||
import os
|
||||
|
||||
import testinfra.utils.ansible_runner
|
||||
|
||||
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
|
||||
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
|
||||
|
||||
|
||||
def test_hosts_file(host):
|
||||
f = host.file('/etc/hosts')
|
||||
|
||||
assert f.exists
|
||||
assert f.user == 'root'
|
||||
assert f.group == 'root'
|
22
molecule/ec2-centos-7/INSTALL.rst
Normal file
22
molecule/ec2-centos-7/INSTALL.rst
Normal file
@ -0,0 +1,22 @@
|
||||
*******
|
||||
Amazon Web Services driver installation guide
|
||||
*******
|
||||
|
||||
Requirements
|
||||
============
|
||||
|
||||
* An AWS credentials rc file
|
||||
|
||||
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[ec2]'
|
124
molecule/ec2-centos-7/create.yml
Normal file
124
molecule/ec2-centos-7/create.yml
Normal file
@ -0,0 +1,124 @@
|
||||
---
|
||||
- name: Create
|
||||
hosts: localhost
|
||||
connection: local
|
||||
gather_facts: false
|
||||
no_log: "{{ not (lookup('env', 'MOLECULE_DEBUG') | bool or molecule_yml.provisioner.log|default(false) | bool) }}"
|
||||
vars:
|
||||
ssh_user: centos
|
||||
ssh_port: 22
|
||||
|
||||
security_group_name: molecule
|
||||
security_group_description: Security group for testing Molecule
|
||||
security_group_rules:
|
||||
- proto: tcp
|
||||
from_port: "{{ ssh_port }}"
|
||||
to_port: "{{ ssh_port }}"
|
||||
cidr_ip: '0.0.0.0/0'
|
||||
- proto: icmp
|
||||
from_port: 8
|
||||
to_port: -1
|
||||
cidr_ip: '0.0.0.0/0'
|
||||
security_group_rules_egress:
|
||||
- proto: -1
|
||||
from_port: 0
|
||||
to_port: 0
|
||||
cidr_ip: '0.0.0.0/0'
|
||||
|
||||
keypair_name: molecule_key_scan2mail
|
||||
keypair_path: "{{ lookup('env', 'MOLECULE_EPHEMERAL_DIRECTORY') }}/ssh_key"
|
||||
tasks:
|
||||
- name: Create security group
|
||||
ec2_group:
|
||||
name: "{{ security_group_name }}"
|
||||
description: "{{ security_group_name }}"
|
||||
rules: "{{ security_group_rules }}"
|
||||
rules_egress: "{{ security_group_rules_egress }}"
|
||||
|
||||
- name: Delete remote keypair
|
||||
ec2_key:
|
||||
name: "{{ keypair_name }}"
|
||||
state: absent
|
||||
|
||||
- name: Create keypair
|
||||
ec2_key:
|
||||
name: "{{ keypair_name }}"
|
||||
register: keypair
|
||||
|
||||
- name: Persist the keypair
|
||||
copy:
|
||||
dest: "{{ keypair_path }}"
|
||||
content: "{{ keypair.key.private_key }}"
|
||||
mode: 0600
|
||||
when: keypair.changed
|
||||
|
||||
- name: Create molecule instance(s)
|
||||
ec2:
|
||||
key_name: "{{ keypair_name }}"
|
||||
image: "{{ item.image }}"
|
||||
instance_type: "{{ item.instance_type }}"
|
||||
vpc_subnet_id: "{{ item.vpc_subnet_id }}"
|
||||
group: "{{ security_group_name }}"
|
||||
instance_tags:
|
||||
instance: "{{ item.name }}"
|
||||
wait: true
|
||||
assign_public_ip: true
|
||||
exact_count: 1
|
||||
count_tag:
|
||||
instance: "{{ item.name }}"
|
||||
volumes:
|
||||
- device_name: /dev/sda1
|
||||
volume_type: gp2
|
||||
volume_size: 8
|
||||
delete_on_termination: yes
|
||||
register: server
|
||||
with_items: "{{ molecule_yml.platforms }}"
|
||||
async: 7200
|
||||
poll: 0
|
||||
|
||||
- name: Wait for instance(s) creation to complete
|
||||
async_status:
|
||||
jid: "{{ item.ansible_job_id }}"
|
||||
register: ec2_jobs
|
||||
until: ec2_jobs.finished
|
||||
retries: 300
|
||||
with_items: "{{ server.results }}"
|
||||
|
||||
# Mandatory configuration for Molecule to function.
|
||||
|
||||
- name: Populate instance config dict
|
||||
set_fact:
|
||||
instance_conf_dict: {
|
||||
'instance': "{{ item.instances[0].tags.instance }}",
|
||||
'address': "{{ item.instances[0].public_ip }}",
|
||||
'user': "{{ ssh_user }}",
|
||||
'port': "{{ ssh_port }}",
|
||||
'identity_file': "{{ keypair_path }}",
|
||||
'instance_ids': "{{ item.instance_ids }}", }
|
||||
with_items: "{{ ec2_jobs.results }}"
|
||||
register: instance_config_dict
|
||||
when: server.changed | bool
|
||||
|
||||
- name: Convert instance config dict to a list
|
||||
set_fact:
|
||||
instance_conf: "{{ instance_config_dict.results | map(attribute='ansible_facts.instance_conf_dict') | list }}"
|
||||
when: server.changed | bool
|
||||
|
||||
- name: Dump instance config
|
||||
copy:
|
||||
content: "{{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}"
|
||||
dest: "{{ molecule_instance_config }}"
|
||||
when: server.changed | bool
|
||||
|
||||
- name: Wait for SSH
|
||||
wait_for:
|
||||
port: "{{ ssh_port }}"
|
||||
host: "{{ item.address }}"
|
||||
search_regex: SSH
|
||||
delay: 10
|
||||
timeout: 320
|
||||
with_items: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}"
|
||||
|
||||
- name: Wait for boot process to finish
|
||||
pause:
|
||||
minutes: 2
|
47
molecule/ec2-centos-7/destroy.yml
Normal file
47
molecule/ec2-centos-7/destroy.yml
Normal file
@ -0,0 +1,47 @@
|
||||
---
|
||||
- name: Destroy
|
||||
hosts: localhost
|
||||
connection: local
|
||||
gather_facts: false
|
||||
no_log: "{{ not (lookup('env', 'MOLECULE_DEBUG') | bool or molecule_yml.provisioner.log|default(false) | bool) }}"
|
||||
tasks:
|
||||
- block:
|
||||
- name: Populate instance config
|
||||
set_fact:
|
||||
instance_conf: "{{ lookup('file', molecule_instance_config) | molecule_from_yaml }}"
|
||||
skip_instances: false
|
||||
rescue:
|
||||
- name: Populate instance config when file missing
|
||||
set_fact:
|
||||
instance_conf: {}
|
||||
skip_instances: true
|
||||
|
||||
- name: Destroy molecule instance(s)
|
||||
ec2:
|
||||
state: absent
|
||||
instance_ids: "{{ item.instance_ids }}"
|
||||
register: server
|
||||
with_items: "{{ instance_conf }}"
|
||||
when: not skip_instances
|
||||
async: 7200
|
||||
poll: 0
|
||||
|
||||
- name: Wait for instance(s) deletion to complete
|
||||
async_status:
|
||||
jid: "{{ item.ansible_job_id }}"
|
||||
register: ec2_jobs
|
||||
until: ec2_jobs.finished
|
||||
retries: 300
|
||||
with_items: "{{ server.results }}"
|
||||
|
||||
# Mandatory configuration for Molecule to function.
|
||||
|
||||
- name: Populate instance config
|
||||
set_fact:
|
||||
instance_conf: {}
|
||||
|
||||
- name: Dump instance config
|
||||
copy:
|
||||
content: "{{ instance_conf | to_json | from_json | molecule_to_yaml | molecule_header }}"
|
||||
dest: "{{ molecule_instance_config }}"
|
||||
when: server.changed | bool
|
22
molecule/ec2-centos-7/molecule.yml
Normal file
22
molecule/ec2-centos-7/molecule.yml
Normal file
@ -0,0 +1,22 @@
|
||||
---
|
||||
dependency:
|
||||
name: galaxy
|
||||
driver:
|
||||
name: ec2
|
||||
platforms:
|
||||
- name: centos-7-scan2mail
|
||||
image: ami-04cf43aca3e6f3de3
|
||||
instance_type: t2.micro
|
||||
vpc_subnet_id: subnet-9b6896f1
|
||||
lint:
|
||||
name: yamllint
|
||||
enabled: False
|
||||
provisioner:
|
||||
name: ansible
|
||||
lint:
|
||||
name: ansible-lint
|
||||
enabled: False
|
||||
verifier:
|
||||
name: testinfra
|
||||
lint:
|
||||
name: flake8
|
5
molecule/ec2-centos-7/playbook.yml
Normal file
5
molecule/ec2-centos-7/playbook.yml
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
- name: Converge
|
||||
hosts: all
|
||||
roles:
|
||||
- role: xoxys.scan2mail
|
9
molecule/ec2-centos-7/prepare.yml
Normal file
9
molecule/ec2-centos-7/prepare.yml
Normal file
@ -0,0 +1,9 @@
|
||||
---
|
||||
- name: Prepare
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Install python for Ansible
|
||||
raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal python-zipstream)
|
||||
become: true
|
||||
changed_when: false
|
20
molecule/ec2-centos-7/tests/test_default.py
Normal file
20
molecule/ec2-centos-7/tests/test_default.py
Normal file
@ -0,0 +1,20 @@
|
||||
import os
|
||||
|
||||
import testinfra.utils.ansible_runner
|
||||
|
||||
import warnings
|
||||
warnings.filterwarnings("ignore", category=DeprecationWarning)
|
||||
|
||||
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
|
||||
os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')
|
||||
|
||||
|
||||
def test_incron_is_installed(host):
|
||||
incron = host.package("incron")
|
||||
assert incron.is_installed
|
||||
|
||||
|
||||
def test_incron_running_and_enabled(host):
|
||||
incron = host.service("incrond")
|
||||
assert incron.is_running
|
||||
assert incron.is_enabled
|
3
molecule/pytest.ini
Normal file
3
molecule/pytest.ini
Normal file
@ -0,0 +1,3 @@
|
||||
[pytest]
|
||||
filterwarnings =
|
||||
ignore::DeprecationWarning
|
13
tasks/main.yml
Normal file
13
tasks/main.yml
Normal file
@ -0,0 +1,13 @@
|
||||
---
|
||||
- include_vars: "{{ lookup('first_found', params) }}"
|
||||
vars:
|
||||
params:
|
||||
files:
|
||||
- "{{ ansible_lsb.id | default('') | lower }}.yml"
|
||||
- "{{ ansible_os_family | lower }}.yml"
|
||||
- default.yml
|
||||
paths:
|
||||
- "vars"
|
||||
|
||||
- include_tasks: setup.yml
|
||||
- include_tasks: post_tasks.yml
|
9
tasks/post_tasks.yml
Normal file
9
tasks/post_tasks.yml
Normal file
@ -0,0 +1,9 @@
|
||||
---
|
||||
- name: Ensure incron service is up and running
|
||||
systemd:
|
||||
name: "{{ __scan2mail_incron_service }}"
|
||||
state: started
|
||||
daemon_reload: yes
|
||||
enabled: yes
|
||||
become: True
|
||||
become_user: root
|
35
tasks/setup.yml
Normal file
35
tasks/setup.yml
Normal file
@ -0,0 +1,35 @@
|
||||
---
|
||||
- block:
|
||||
- name: Install required packages for scan2mail
|
||||
package:
|
||||
name: "{{ item }}"
|
||||
state: present
|
||||
loop: "{{ scan2mail_packages_extra + __scan2mail_packages }}"
|
||||
|
||||
- name: Configure incron
|
||||
template:
|
||||
src: etc/incron.conf.j2
|
||||
dest: /etc/incron.conf
|
||||
owner: root
|
||||
group: root
|
||||
mode: 0644
|
||||
notify: __incron_restart
|
||||
|
||||
- name: Create incron job definition
|
||||
template:
|
||||
src: etc/incron.d/scan2mail.conf.j2
|
||||
dest: "{{ scan2mail_incron_system_table_dir }}/scan2mail.conf"
|
||||
owner: root
|
||||
group: root
|
||||
mode: 0644
|
||||
when: scan2mail_incron_jobs
|
||||
|
||||
- name: Create scan2mail script
|
||||
template:
|
||||
src: usr/local/bin/scan2mail.j2
|
||||
dest: /usr/local/bin/scan2mail
|
||||
owner: root
|
||||
group: root
|
||||
mode: 0644
|
||||
become: True
|
||||
become_user: root
|
19
templates/etc/incron.conf.j2
Normal file
19
templates/etc/incron.conf.j2
Normal file
@ -0,0 +1,19 @@
|
||||
#jinja2: lstrip_blocks: True
|
||||
{{ ansible_managed | comment }}
|
||||
|
||||
# This directory is examined by incrond for system table files.
|
||||
system_table_dir = {{ scan2mail_incron_system_table_dir }}
|
||||
# This directory is examined by incrond for user table files.
|
||||
user_table_dir = /var/spool/incron
|
||||
# This file contains users allowed to use incron.
|
||||
allowed_users = /etc/incron.allow
|
||||
# This file contains users denied to use incron.
|
||||
denied_users = /etc/incron.deny
|
||||
# This directory is used for creating a lock avoiding to run
|
||||
lockfile_dir = /var/run
|
||||
# This name (appended by '.pid') is used for creating a lock
|
||||
# avoiding to run multiple instances of incrond.
|
||||
lockfile_name = incrond
|
||||
# This name or path is used to run as an editor for editing
|
||||
# user tables.
|
||||
editor = vim
|
5
templates/etc/incron.d/scan2mail.conf.j2
Normal file
5
templates/etc/incron.d/scan2mail.conf.j2
Normal file
@ -0,0 +1,5 @@
|
||||
#jinja2: lstrip_blocks: True
|
||||
{{ ansible_managed | comment }}
|
||||
{% for item in scan2mail_incron_jobs %}
|
||||
{{ item.path }} {{ item.mask }} {{ item.action }} {{ item.options | join(" ") }}
|
||||
{% endfor %}
|
14
templates/usr/local/bin/scan2mail.j2
Normal file
14
templates/usr/local/bin/scan2mail.j2
Normal file
@ -0,0 +1,14 @@
|
||||
#jinja2: lstrip_blocks: True
|
||||
#!/bin/bash
|
||||
{{ ansible_managed | comment }}
|
||||
|
||||
FILEPATH=$1
|
||||
FILENAME=$2
|
||||
|
||||
FROM_ADDR="{{ scan2mail_from_address }}"
|
||||
TO_ADDRESS="`basename \"$FILEPATH\"`@{{ scan2mail_mail_domain }}"
|
||||
ATTACHMENT="$1/$2"
|
||||
|
||||
sleep 10
|
||||
|
||||
echo "{{ scan2mail_message | default('') }}" | mail -s "{{ scan2mail_subject }}" -a "From: ${FROM_ADDR}" -A ${ATTACHMENT} ${TO_ADDRESS}
|
2
vars/debian.yml
Normal file
2
vars/debian.yml
Normal file
@ -0,0 +1,2 @@
|
||||
---
|
||||
__scan2mail_incron_service: incron
|
2
vars/default.yml
Normal file
2
vars/default.yml
Normal file
@ -0,0 +1,2 @@
|
||||
---
|
||||
__scan2mail_incron_service: incrond
|
3
vars/main.yml
Normal file
3
vars/main.yml
Normal file
@ -0,0 +1,3 @@
|
||||
---
|
||||
__scan2mail_packages:
|
||||
- incron
|
Loading…
Reference in New Issue
Block a user