/home/zuul/src/opendev.org/openstack/openstack-ansible-openstack_hosts/tasks/openstack_hosts_configure_zypper.yml
---
# Copyright 2017, SUSE LINUX GmbH.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# NOTE(hwoarang) snapper make take significant amount of CPU time
# when executing zypper over and over so it's best to disable the
# background comparison process. Snapper may need further tuning
# for example reducing the number of snapshots to keep, disable daily
# cleanup job etc but these may come later in the future if needed.
- name: Check if Snapper root configuration file exists
  stat:
    path: "/etc/snapper/configs/root"
  register: snapper_root_config

- name: Disable background snapshot comparisons on Snapper
  lineinfile:
    path: "/etc/snapper/configs/root"
    regexp: '^BACKGROUND_COMPARISON=.*'
    line: 'BACKGROUND_COMPARISON="no"'
    state: present
  when:
    - snapper_root_config.stat.exists

# NOTE(nicolasbock) The gettext-runtime-mini package conflicts with
# gettext-runtime. But removing it with zypper will also remove
# grub2. We call `rpm` directly to remove _only_ gettext-runtime-mini.
- name: Remove gettext-runtime-mini without removing grub2
  shell: |
    return_code=0
    if rpm --query --info gettext-runtime-mini; then
      rpm --erase --verbose --nodeps gettext-runtime-mini
      return_code=2
    fi
    exit ${return_code}
  args:
    executable: /bin/bash
    warn: false
  register: _remove_gettext_runtime_mini
  changed_when: _remove_gettext_runtime_mini.rc == 2
  failed_when: _remove_gettext_runtime_mini.rc not in [0, 2]

- name: If a keyfile is provided, copy the gpg keyfile to the key location
  copy:
    src: "{{ item.keyfile }}"
    dest: "{{ item.key }}"
  with_items: "{{ openstack_hosts_package_repos_keys | selectattr('keyfile','defined') | list }}"

- name: Add/Remove repositories gpg keys manually
  rpm_key:
    key: "{{ key.key }}"
    state: "{{ key.state | default('present') }}"
    validate_certs: "{{ key.validate_certs | default(omit) }}"
  with_items: "{{ openstack_hosts_package_repos_keys }}"
  loop_control:
    loop_var: key
  register: _add_rpm_keys
  until: _add_rpm_keys  is success
  retries: 5
  delay: 2

- name: Add/Remove/Update acceptable repository vendors
  template:
    src: osa.vendors.j2
    dest: /etc/zypp/vendors.d/osa.vendors
    owner: root
    group: root
    mode: 0644

- name: Add requirement packages (repositories gpg keys, toolkits...)
  zypper:
    name: "{{ openstack_hosts_package_list | rejectattr('state','equalto','absent') | map(attribute='name') | list }}"
    state: "{{ openstack_hosts_package_state }}"
  when:
    - (openstack_hosts_package_list | rejectattr('state','equalto','absent') | map(attribute='name') | list | length) > 0

- name: Add/Remove/Update standard and user defined repositories
  zypper_repository:
    repo: "{{ repo.repo }}"
    state: "{{ repo.state | default('present') }}"
    name: "{{ repo.name | default(omit) }}"
    enabled: "{{ repo.enabled | default(omit) }}"
    disable_gpg_check: "{{ repo.disable_gpg_check | default(omit) }}"
    description: "{{ repo.description | default(omit) }}"
    autorefresh: "{{ repo.autorefresh | default(omit) }}"
    auto_import_keys: "{{ repo.auto_import_keys | default(omit) }}"
    priority: "{{ repo.priority | default(omit) }}"
  with_items: "{{ openstack_hosts_package_repos }}"
  loop_control:
    loop_var: repo
  register: _adding_repo
  until: _adding_repo  is success
  retries: 5
  delay: 2

- name: Refresh repositories if necessary
  zypper_repository:
    repo: '*'
    runrefresh: yes
  when:
    - _adding_repo  is changed