diff --git a/.ansible-lint b/.ansible-lint new file mode 100644 index 0000000..1dd6555 --- /dev/null +++ b/.ansible-lint @@ -0,0 +1,12 @@ +--- +exclude_paths: + - .cache/ + - ./molecule + - ./.travis.yml + - ./.github +parseable: true +skip_list: + - '204' + - 'no-handler' +use_default_rules: true +verbosity: 1 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..d08e2bf --- /dev/null +++ b/.gitattributes @@ -0,0 +1,3 @@ +*.yml linguist-detectable=true +*.yaml linguist-detectable=true +*.html linguist-detectable=false diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2100e33 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ + +tests/playbook.retry +tests/.cache +__pycache__ +.pytest_cache +.molecule +.cache +.venv + +*.iml +.idea +.project + +*.pyc +**/.vscode \ No newline at end of file diff --git a/.yammlint b/.yammlint new file mode 100644 index 0000000..80f524b --- /dev/null +++ b/.yammlint @@ -0,0 +1,33 @@ +--- +# Based on ansible-lint config +extends: default + +ignore: | + molecule/**/tests/ + .venv + .github + .travis.yml + +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 + empty-lines: + max: 3 + level: error + hyphens: + level: error + key-duplicates: enable + line-length: disable + new-lines: + type: unix + truthy: disable diff --git a/LICENSE.md b/LICENSE.md index e69de29..1936a8d 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -0,0 +1,10 @@ +The MIT License (MIT) +===================== + +Copyright © 2023 Laur IVAN + +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. \ No newline at end of file diff --git a/meta/main.yml b/meta/main.yml index c572acc..7b2915b 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -1,52 +1,25 @@ +--- galaxy_info: - author: your name - description: your role description - company: your company (optional) + author: Laur Ivan (laur.ivan@gmail.com) + namespace: laurivan - # If the issue tracker for your role is not on github, uncomment the - # next line and provide a value - # issue_tracker_url: http://example.com/issue/tracker + role_name: macvlan + description: Role to create a macvlan network in docker + min_ansible_version: "2.10" + min_ansible_container_version: "2.10" + license: MIT + galaxy_tags: + - docker + - network + - macvlan + platforms: + - name: Debian + versions: + - bullseye + - buster - # Choose a valid license ID from https://spdx.org - some suggested licenses: - # - BSD-3-Clause (default) - # - MIT - # - GPL-2.0-or-later - # - GPL-3.0-only - # - Apache-2.0 - # - CC-BY-4.0 - license: license (GPL-2.0-or-later, MIT, etc) - - min_ansible_version: 2.1 - - # If this a Container Enabled role, provide the minimum Ansible Container version. - # min_ansible_container_version: - - # - # Provide a list of supported platforms, and for each platform a list of versions. - # If you don't wish to enumerate all versions for a particular platform, use 'all'. - # To view available platforms and versions (or releases), visit: - # https://galaxy.ansible.com/api/v1/platforms/ - # - # platforms: - # - name: Fedora - # versions: - # - all - # - 25 - # - name: SomePlatform - # versions: - # - all - # - 1.0 - # - 7 - # - 99.99 - - galaxy_tags: [] - # List tags for your role here, one per line. A tag is a keyword that describes - # and categorizes the role. Users find roles by searching for tags. Be sure to - # remove the '[]' above, if you add tags to this list. - # - # NOTE: A tag is limited to a single word comprised of alphanumeric characters. - # Maximum 20 tags per role. - -dependencies: [] - # List your role dependencies here, one per line. Be sure to remove the '[]' above, - # if you add dependencies to this list. + - name: Ubuntu + versions: + - bionic + - focal + - jammy diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml index e600756..eef4cb5 100644 --- a/molecule/default/converge.yml +++ b/molecule/default/converge.yml @@ -2,7 +2,5 @@ - name: Converge hosts: all gather_facts: false - tasks: - - name: Replace this task with one that validates your content - ansible.builtin.debug: - msg: "This is the effective test" + roles: + - role: "laurivan.macvlan" diff --git a/molecule/default/create.yml b/molecule/default/create.yml deleted file mode 100644 index 371214e..0000000 --- a/molecule/default/create.yml +++ /dev/null @@ -1,36 +0,0 @@ ---- -- name: Create - hosts: localhost - connection: local - gather_facts: false - # no_log: "{{ molecule_no_log }}" - tasks: - - # TODO: Developer must implement and populate 'server' variable - - - name: Create instance config - when: server.changed | default(false) | bool # noqa no-handler - block: - - name: Populate instance config dict # noqa jinja - ansible.builtin.set_fact: - instance_conf_dict: {} - # instance': "{{ }}", - # address': "{{ }}", - # user': "{{ }}", - # port': "{{ }}", - # 'identity_file': "{{ }}", } - with_items: "{{ server.results }}" - register: instance_config_dict - - - name: Convert instance config dict to a list - ansible.builtin.set_fact: - instance_conf: "{{ instance_config_dict.results | map(attribute='ansible_facts.instance_conf_dict') | list }}" - - - name: Dump instance config - ansible.builtin.copy: - content: | - # Molecule managed - - {{ instance_conf | to_json | from_json | to_yaml }} - dest: "{{ molecule_instance_config }}" - mode: 0600 diff --git a/molecule/default/destroy.yml b/molecule/default/destroy.yml index 5ba2d23..631979b 100644 --- a/molecule/default/destroy.yml +++ b/molecule/default/destroy.yml @@ -1,24 +1,28 @@ --- -- name: Destroy - hosts: localhost - connection: local +- name: "MACVLAN | Destroy" + hosts: "localhost" + connection: "local" gather_facts: false - # no_log: "{{ molecule_no_log }}" + no_log: "{{ molecule_no_log }}" tasks: # Developer must implement. + - name: "MACVLAN | Remove the docker image" + community.docker.docker_container: + name: "MACVLAN" + state: "absent" # Mandatory configuration for Molecule to function. - - name: Populate instance config + - name: "MACVLAN | Populate instance config" ansible.builtin.set_fact: instance_conf: {} - - name: Dump instance config + - name: "MACVLAN | Dump instance config" ansible.builtin.copy: content: | # Molecule managed - {{ instance_conf | to_json | from_json | to_yaml }} dest: "{{ molecule_instance_config }}" mode: 0600 - when: server.changed | default(false) | bool # noqa no-handler + when: "server.changed | default(false) | bool" # noqa no-handler + \ No newline at end of file diff --git a/molecule/default/molecule.yml b/molecule/default/molecule.yml index b524ee7..a302e0b 100644 --- a/molecule/default/molecule.yml +++ b/molecule/default/molecule.yml @@ -1,6 +1,34 @@ --- +dependency: + name: galaxy + options: + ignore-certs: true + ignore-errors: true + role-file: molecule/requirements.yml + requirements-file: molecule/requirements.yml +driver: + name: docker platforms: - - name: instance - # you might want to add your own variables here based on what provisioning - # you are doing like: - # image: quay.io/centos/centos:stream8 + - name: macvlan + groups: + - unifi_group + image: "geerlingguy/docker-${MOLECULE_DISTRO:-ubuntu2204}-ansible:latest" + privileged: true + pre_build_image: true + capabilities: + - SYS_ADMIN + tmpfs: + - /tmp + - /run + - /run/lock + volumes: + - '/sys/fs/cgroup:/sys/fs/cgroup:ro' + - '/var/run/docker.sock:/tmp/docker_mounted.sock' + command: '/lib/systemd/systemd' + stop_signal: 'RTMIN+3' +provisioner: + name: ansible + playbooks: + converge: ${MOLECULE_PLAYBOOK:-converge.yml} +verifier: + name: ansible diff --git a/molecule/default/prepare.yml b/molecule/default/prepare.yml new file mode 100644 index 0000000..0bd05ea --- /dev/null +++ b/molecule/default/prepare.yml @@ -0,0 +1,39 @@ +--- +- name: "MACVLAN | Setup the test machine" + hosts: "macvlan" + tasks: + - name: "MACVLAN | Check if /var/run/docker.sock already exists" + ansible.builtin.stat: + path: "/var/run/docker.sock" + register: "docker_sock_stat" + + - name: "MACVLAN | Create docker.sock" + ansible.builtin.raw: touch /var/run/docker.sock + become: true + changed_when: false + when: "not docker_sock_stat.stat.exists" + + - name: "MACVLAN | Move docker.sock from tmp" + ansible.builtin.raw: > + mount --move /tmp/docker_mounted.sock /var/run/docker.sock + become: true + changed_when: false + when: "not docker_sock_stat.stat.exists" + + - name: "MACVLAN | Update apt cache." + ansible.builtin.apt: + update_cache: yes + cache_valid_time: 600 + when: "ansible_os_family == 'Debian'" + + - name: "MACVLAN | Install python requests" + ansible.builtin.pip: + name: + - "requests" + - "docker" + + - name: "MACVLAN | Install docker" + vars: + docker_service_manage: false + ansible.builtin.include_role: + name: "geerlingguy.docker" diff --git a/molecule/default/requirements.yml b/molecule/default/requirements.yml new file mode 100644 index 0000000..a126885 --- /dev/null +++ b/molecule/default/requirements.yml @@ -0,0 +1,4 @@ +--- +roles: + - name: "geerlingguy.docker" +collections: [] \ No newline at end of file diff --git a/test-requirements.txt b/test-requirements.txt index ed82ef9..12da0eb 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,5 +1,6 @@ +urllib3==1.26.13 ansible==7.2.0 -molecule==4.0.4 +molecule==6.0.2 docker==6.1.3 molecule-docker==2.1.0 ansible-lint==6.14.4