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)