From ec1829200305d60f048a21e91cc3aa7d02327016 Mon Sep 17 00:00:00 2001 From: Sebastien Thebert Date: Sun, 30 Oct 2022 01:27:45 +0200 Subject: [PATCH 1/3] add defaults --- defaults/main.yml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 defaults/main.yml diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..f278320 --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1,3 @@ +--- +debug: false +state: present # possible values defined in meta/argument_specs.yml From 7cc01844f1041e674b626007624f2fa0c605c628 Mon Sep 17 00:00:00 2001 From: Sebastien Thebert Date: Sun, 30 Oct 2022 01:28:06 +0200 Subject: [PATCH 2/3] update argument_specs --- meta/argument_specs.yml | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/meta/argument_specs.yml b/meta/argument_specs.yml index 1427f32..213c565 100644 --- a/meta/argument_specs.yml +++ b/meta/argument_specs.yml @@ -6,6 +6,41 @@ argument_specs: - The main entry point just combines the repositories and packages entry points. options: + debug: + description: Use debug module to display 'pro' command stdout/stderr + type: bool + default: false + disabled_services: + description: List of services to disable + type: list + elements: str + choices: + - 'cc-eal' # Common Criteria EAL2 + - 'esm-infra' # Expanded Security Maintenance for Infrastructure + - 'fips' + - 'fips-updates' + - 'livepatch' # Canonical Kernel Livepatch + - 'usg' + enabled_services: + description: List of services to enable + type: list + elements: str + choices: + - 'cc-eal' # Common Criteria EAL2 + - 'esm-infra' # Expanded Security Maintenance for Infrastructure + - 'fips' + - 'fips-updates' + - 'livepatch' # Canonical Kernel Livepatch + - 'usg' + state: + description: State of Ubuntu Pro installation + type: str + choices: + - absent + - attached + - detached + - present + default: present ubuntu_pro_token: description: - Ubuntu Pro token @@ -18,7 +53,7 @@ argument_specs: required: false ubuntu_pro_config.https_proxy: description: - - HTTPW Proxy + - HTTPS Proxy type: str required: false ubuntu_pro_config.apt_http_proxy: @@ -28,7 +63,7 @@ argument_specs: required: false ubuntu_pro_config.apt_https_proxy: description: - - apt HTTPs Proxy + - apt HTTPS Proxy type: str required: false ubuntu_pro_config.ua_apt_http_proxy: From 6a09818893e92ab8917a1eec727e6957b56ed284 Mon Sep 17 00:00:00 2001 From: Sebastien Thebert Date: Sun, 30 Oct 2022 01:29:02 +0200 Subject: [PATCH 3/3] Handle 'state' (present,attached,absent,detached) --- tasks/main.yml | 20 ++++++++++++++++---- tasks/register.yml | 2 ++ tasks/requirements.yml | 16 ++++++++-------- tasks/uninstall.yml | 6 ++++++ tasks/unregister.yml | 20 ++++++++++++++++++++ 5 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 tasks/uninstall.yml create mode 100644 tasks/unregister.yml diff --git a/tasks/main.yml b/tasks/main.yml index 2d96dec..2b0b29e 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,20 +1,32 @@ --- - name: Include tasks 'validate_ubuntu_lts_version.yml' include_tasks: validate_ubuntu_lts_version.yml + when: state == 'present' or state == 'attached' -# - name: Include tasks 'requirements.yml' -# include_tasks: requirements.yml +- name: Include tasks 'requirements.yml' + include_tasks: requirements.yml + when: state == 'present' or state == 'attached' - name: Include tasks 'configure.yml' include_tasks: configure.yml + when: state == 'present' or state == 'attached' - name: Include tasks 'register.yml' include_tasks: register.yml + when: state == 'present' or state == 'attached' - name: Include tasks 'enable_services.yml' include_tasks: enable_services.yml - when: enabled_services is defined + when: enabled_services is defined and (state == 'present' or state == 'attached') - name: Include tasks 'disable_services.yml' include_tasks: disable_services.yml - when: disabled_services is defined + when: disabled_services is defined and (state == 'present' or state == 'attached') + +- name: Include tasks 'unregister.yml' + include_tasks: unregister.yml + when: state == 'absent' or state == 'detached' + +- name: Include tasks 'uninstall.yml' + include_tasks: uninstall.yml + when: state == 'absent' diff --git a/tasks/register.yml b/tasks/register.yml index 6f71165..20f5869 100644 --- a/tasks/register.yml +++ b/tasks/register.yml @@ -3,8 +3,10 @@ ansible.builtin.command: "pro attach {{ ubuntu_pro_token }}" become: yes register: reg_pro_attach + changed_when: reg_pro_attach.rc == 0 failed_when: (reg_pro_attach.rc != 0) and ('This machine is already attached to' not in reg_pro_attach.stderr_lines[0]) - name: Print 'pro attach' command output ansible.builtin.debug: var: reg_pro_attach + when: debug diff --git a/tasks/requirements.yml b/tasks/requirements.yml index bb5b2ed..2b69265 100644 --- a/tasks/requirements.yml +++ b/tasks/requirements.yml @@ -1,13 +1,13 @@ --- -- name: Launch 'apt update' - ansible.builtin.apt: - update_cache: yes - become: yes +# - name: Launch 'apt update' +# ansible.builtin.apt: +# update_cache: yes +# become: yes -- name: Launch 'apt upgrade' - ansible.builtin.apt: - upgrade: yes - become: yes +# - name: Launch 'apt upgrade' +# ansible.builtin.apt: +# upgrade: yes +# become: yes - name: Install 'ubuntu-advantage-tools' package ansible.builtin.apt: diff --git a/tasks/uninstall.yml b/tasks/uninstall.yml new file mode 100644 index 0000000..da6e11e --- /dev/null +++ b/tasks/uninstall.yml @@ -0,0 +1,6 @@ +--- +- name: Uninstall 'ubuntu-advantage-tools' package + ansible.builtin.apt: + name: ubuntu-advantage-tools + state: absent + become: yes diff --git a/tasks/unregister.yml b/tasks/unregister.yml new file mode 100644 index 0000000..6027e28 --- /dev/null +++ b/tasks/unregister.yml @@ -0,0 +1,20 @@ +--- +- ansible.builtin.shell: "command pro" + register: reg_command_pro + ignore_errors: yes + +- set_fact: + pro_command_exists: "{{ (reg_command_pro.rc == 127) | ternary(false, true) }}" + +- name: Unregister this host on Ubuntu Pro (pro detach) + ansible.builtin.command: "pro detach --assume-yes" + become: yes + register: reg_pro_detach + changed_when: reg_pro_detach.rc == 0 + failed_when: (reg_pro_detach.rc != 0) and ('This machine is not attached to an Ubuntu Pro subscription.' not in reg_pro_detach.stderr_lines[0]) + when: pro_command_exists + +- name: Print 'pro detach' command output + ansible.builtin.debug: + var: reg_pro_detach + when: (debug and pro_command_exists)