From efca7d06ee5efa7bd9d1df6241ec9edf296323ff Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Sun, 5 May 2024 19:41:56 -0700 Subject: [PATCH] freebsd: add ezjail * Some tests like service, service_facts will require jail add a role setup_ezjail to install jails Signed-off-by: Abhijeet Kasurde --- .../targets/setup_ezjail/tasks/main.yml | 99 +++++++++++++++++++ .../integration/targets/test_ezjail/README.md | 38 +++++++ test/integration/targets/test_ezjail/aliases | 9 ++ .../targets/test_ezjail/defaults/main.yml | 2 + .../targets/test_ezjail/handlers/main.yml | 2 + .../targets/test_ezjail/meta/main.yml | 2 + .../targets/test_ezjail/tasks/main.yml | 11 +++ .../targets/test_ezjail/tests/inventory | 2 + .../targets/test_ezjail/tests/test.yml | 5 + .../targets/test_ezjail/vars/main.yml | 2 + 10 files changed, 172 insertions(+) create mode 100644 test/integration/targets/setup_ezjail/tasks/main.yml create mode 100644 test/integration/targets/test_ezjail/README.md create mode 100644 test/integration/targets/test_ezjail/aliases create mode 100644 test/integration/targets/test_ezjail/defaults/main.yml create mode 100644 test/integration/targets/test_ezjail/handlers/main.yml create mode 100644 test/integration/targets/test_ezjail/meta/main.yml create mode 100644 test/integration/targets/test_ezjail/tasks/main.yml create mode 100644 test/integration/targets/test_ezjail/tests/inventory create mode 100644 test/integration/targets/test_ezjail/tests/test.yml create mode 100644 test/integration/targets/test_ezjail/vars/main.yml diff --git a/test/integration/targets/setup_ezjail/tasks/main.yml b/test/integration/targets/setup_ezjail/tasks/main.yml new file mode 100644 index 00000000000..a6d562a44fc --- /dev/null +++ b/test/integration/targets/setup_ezjail/tasks/main.yml @@ -0,0 +1,99 @@ +# Copyright (c) Ansible Project +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +# +# Instructions for setting up a jail +# https://www.freebsd.org/doc/en_US.ISO8859-1/books/handbook/jails-ezjail.html +# +- name: Setup cloned interfaces + lineinfile: + dest: /etc/rc.conf + regexp: ^cloned_interfaces=lo1 + line: cloned_interfaces=lo1 + +- name: Activate cloned interfaces + command: "service netif cloneup" + changed_when: false + +- name: Add nat rule for cloned interfaces + copy: + dest: /etc/pf.conf + content: | + nat on {{ ansible_default_ipv4.interface }} from 127.0.1.0/24 -> {{ ansible_default_ipv4.interface }}:0 + validate: "pfctl -nf %s" + +- name: Start pf firewall + service: + name: pf + state: started + enabled: true + +- name: Install ezjail + pkgng: + name: ezjail + +- name: Configure ezjail to use http + when: ansible_distribution_version is version('11.01', '>') + lineinfile: + dest: /usr/local/etc/ezjail.conf + regexp: ^ezjail_ftphost + line: ezjail_ftphost=http://ftp.freebsd.org + +- name: Configure ezjail to use archive for old freebsd releases + when: ansible_distribution_version is version('11.01', '<=') + lineinfile: + dest: /usr/local/etc/ezjail.conf + regexp: ^ezjail_ftphost + line: ezjail_ftphost=http://ftp-archive.freebsd.org + +- name: Start ezjail + ignore_errors: true + service: + name: ezjail + state: started + enabled: true + +- name: Redirect logs depending on verbosity + set_fact: + pkgng_jail_log_redirect: "2>&1 | tee -a /tmp/ezjail.log {{ '> /dev/null' if ansible_verbosity < 2 else '' }}" + +- name: Has ezjail + register: ezjail_base_jail + stat: + path: /usr/jails/basejail + +- name: Setup ezjail base + when: not ezjail_base_jail.stat.exists + shell: "ezjail-admin install {{ pkgng_jail_log_redirect }}" + changed_when: false + +- name: Has testjail + register: ezjail_test_jail + stat: + path: /usr/jails/testjail + +- name: Create testjail + when: not ezjail_test_jail.stat.exists + shell: "ezjail-admin create testjail 'lo1|127.0.1.1' {{ pkgng_jail_log_redirect }}" + changed_when: false + +- name: Configure testjail to use Cloudflare DNS + lineinfile: + dest: /usr/jails/testjail/etc/resolv.conf + regexp: "^nameserver[[:blank:]]+{{ item }}$" + line: "nameserver {{ item }}" + create: true + loop: + - "1.1.1.1" + - "1.0.0.1" + +- name: Is testjail running + shell: "jls | grep testjail" + changed_when: false + failed_when: false + register: is_testjail_up + +- name: Start testjail + when: is_testjail_up.rc == 1 + command: "ezjail-admin start testjail" diff --git a/test/integration/targets/test_ezjail/README.md b/test/integration/targets/test_ezjail/README.md new file mode 100644 index 00000000000..225dd44b9fc --- /dev/null +++ b/test/integration/targets/test_ezjail/README.md @@ -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). diff --git a/test/integration/targets/test_ezjail/aliases b/test/integration/targets/test_ezjail/aliases new file mode 100644 index 00000000000..21dd0a5b75e --- /dev/null +++ b/test/integration/targets/test_ezjail/aliases @@ -0,0 +1,9 @@ +destructive +shippable/posix/group1 +skip/macos +skip/rhel +needs/privileged +skip/docker +needs/root +needs/target/setup_ezjail +context/controller \ No newline at end of file diff --git a/test/integration/targets/test_ezjail/defaults/main.yml b/test/integration/targets/test_ezjail/defaults/main.yml new file mode 100644 index 00000000000..3e3adfd4b59 --- /dev/null +++ b/test/integration/targets/test_ezjail/defaults/main.yml @@ -0,0 +1,2 @@ +--- +# defaults file for test_ezjail diff --git a/test/integration/targets/test_ezjail/handlers/main.yml b/test/integration/targets/test_ezjail/handlers/main.yml new file mode 100644 index 00000000000..7d1fab5dd6d --- /dev/null +++ b/test/integration/targets/test_ezjail/handlers/main.yml @@ -0,0 +1,2 @@ +--- +# handlers file for test_ezjail diff --git a/test/integration/targets/test_ezjail/meta/main.yml b/test/integration/targets/test_ezjail/meta/main.yml new file mode 100644 index 00000000000..811cb55c831 --- /dev/null +++ b/test/integration/targets/test_ezjail/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: + - setup_ezjail diff --git a/test/integration/targets/test_ezjail/tasks/main.yml b/test/integration/targets/test_ezjail/tasks/main.yml new file mode 100644 index 00000000000..fe8856b9093 --- /dev/null +++ b/test/integration/targets/test_ezjail/tasks/main.yml @@ -0,0 +1,11 @@ +--- +# tasks file for test_ezjail +- name: Is testjail running + shell: "jls | grep testjail" + changed_when: false + failed_when: false + register: is_testjail_up + +- name: Start testjail + when: is_testjail_up.rc == 1 + command: "ezjail-admin start testjail" diff --git a/test/integration/targets/test_ezjail/tests/inventory b/test/integration/targets/test_ezjail/tests/inventory new file mode 100644 index 00000000000..878877b0776 --- /dev/null +++ b/test/integration/targets/test_ezjail/tests/inventory @@ -0,0 +1,2 @@ +localhost + diff --git a/test/integration/targets/test_ezjail/tests/test.yml b/test/integration/targets/test_ezjail/tests/test.yml new file mode 100644 index 00000000000..45575fef5e6 --- /dev/null +++ b/test/integration/targets/test_ezjail/tests/test.yml @@ -0,0 +1,5 @@ +--- +- hosts: localhost + remote_user: root + roles: + - test_ezjail diff --git a/test/integration/targets/test_ezjail/vars/main.yml b/test/integration/targets/test_ezjail/vars/main.yml new file mode 100644 index 00000000000..7beab2a5b44 --- /dev/null +++ b/test/integration/targets/test_ezjail/vars/main.yml @@ -0,0 +1,2 @@ +--- +# vars file for test_ezjail