Skip to content

Commit

Permalink
oc_client_install: added mirror option
Browse files Browse the repository at this point in the history
  • Loading branch information
kononovn committed Mar 3, 2025
1 parent af28755 commit 3b02092
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 36 deletions.
1 change: 1 addition & 0 deletions playbooks/deploy-ocp-hybrid-multinode.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
vars:
oc_client_install_url: "{{ ocp_version_facts_oc_client_pull_link }}"
oc_client_install_archive_dest_dir: "{{ dest_iso_dir }}"
oc_clinet_install_version: "{{ ocp_version_facts_parsed_release }}"

- name: Download and extract OCP installer
ansible.builtin.import_role:
Expand Down
19 changes: 16 additions & 3 deletions playbooks/roles/oc_client_install/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This Ansible role automates the installation and management of the OpenShift Cli
- Verifies if the `oc_client_install_url` variable is provided.
- Checks if `oc` is already installed.
- Removes existing `oc` binary if found.
- Downloads and installs the `oc` client from the specified source.
- Downloads and installs the `oc` client either from mirror or from the specified source.
- Ensures proper directory structure for the `oc` binary.
- Moves both `oc` and `kubectl` binaries to the user's `.local/bin` directory.
- Verifies the installation by running `oc version`.
Expand All @@ -31,6 +31,7 @@ This Ansible role automates the installation and management of the OpenShift Cli
| `oc_client_install_url` | URL to download the OpenShift client archive (Required) |yes|
| `oc_client_install_archive_dest_dir` | Directory where the archive will be stored |no|
| `oc_client_install_archive_name` | Name of the downloaded archive file |no|
| `oc_clinet_install_version` | Specifies the OC client version used for retrieving the archive from the mirror link |no|

### Usage
Include this role in your playbook as follows:
Expand Down Expand Up @@ -67,7 +68,7 @@ Include this role in your playbook as follows:
Ensures any previously downloaded archive is removed before downloading.

3. **Download OpenShift Client Archive**
Fetches the `openshift-client-linux.tar.gz` file from the given URL.
Fetches the `openshift-client-linux.tar.gz` file either from the mirror or from the given URL.

4. **Extract Archive**
Unpacks the downloaded archive.
Expand Down Expand Up @@ -103,7 +104,19 @@ None.
vars:
oc_client_install_url: "https://mirror.openshift.com/pub/openshift-v4/x86_64/clients/ocp/latest/openshift-client-linux.tar.gz"
```

Example of getting client from the mirror first:
```yaml
- hosts: localhost
gather_facts: no
roles:
- name: Deploy/Redeploy OCP client
ansible.builtin.import_role:
name: oc_client_install
vars:
oc_client_install_url: "https://mirror.openshift.com/pub/openshift-v4/x86_64/clients/ocp/latest/"
oc_client_install_archive_dest_dir: "/tmp/client"
oc_clinet_install_version: "4.17.10"
```
### License
Apache

Expand Down
98 changes: 65 additions & 33 deletions playbooks/roles/oc_client_install/tasks/oc_install.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
- name: Make a request to trigger tools extraction
ansible.builtin.uri:
url: "{{ oc_client_install_url.split('/')[:-1] | join('/') }}"
return_content: true

- name: Remove OC client archive and checksum file if exist
ansible.builtin.file:
state: absent
Expand All @@ -11,42 +6,79 @@
- "{{ oc_client_install_archive_dest_dir }}/{{ oc_client_install_archive_name }}"
- "{{ oc_client_install_archive_dest_dir }}/{{ checksum_file_name }}"

- name: Download checksum file
register: download_result
until: download_result is not failed
retries: 60
delay: 10
notify: "Delete checksum file"
ansible.builtin.get_url:
url: "{{ oc_client_install_url.split('/')[:-1] | join('/') }}/{{ checksum_file_name }}"
dest: "{{ oc_client_install_archive_dest_dir }}/{{ checksum_file_name }}"
mode: '0640'
- name: Deploy OC client
block:
- name: Try to download checksum file from mirror
register: download_result
until: download_result is not failed
retries: 3
delay: 5
notify: "Delete checksum file"
ansible.builtin.get_url:
url: "{{ mirror_url }}/{{ oc_clinet_install_version }}/{{ checksum_file_name }}"
dest: "{{ oc_client_install_archive_dest_dir }}/{{ checksum_file_name }}"
mode: '0640'

- name: Read checksum file and store content in data variable
ansible.builtin.command: "cat {{ oc_client_install_archive_dest_dir }}/{{ checksum_file_name }}"
changed_when: false
register: data
- name: Read checksum file and store content in data variable
ansible.builtin.command: "cat {{ oc_client_install_archive_dest_dir }}/{{ checksum_file_name }}"
changed_when: false
register: data

- name: Download openshift-client-linux.tar.gz from mirror
register: download_result
until: download_result is not failed
retries: 3
delay: 5
notify: "Delete openshift-client-linux.tar.gz"
vars:
checksum: "{{ data.stdout_lines | select('match', '.*' ~ oc_client_install_url.split('/')[-1] ~ '$') }}"
ansible.builtin.get_url:
url: "{{ mirror_url }}/{{ oc_clinet_install_version }}/openshift-client-linux-{{ oc_clinet_install_version }}.tar.gz"
dest: "{{ oc_client_install_archive_dest_dir }}/{{ oc_client_install_archive_name }}"
mode: '0640'
checksum: "sha256:{{ checksum[0].split(' ')[0] }}"

rescue:
- name: Make a request to trigger tools extraction
ansible.builtin.uri:
url: "{{ oc_client_install_url.split('/')[:-1] | join('/') }}"

- name: Download checksum file
register: download_result
until: download_result is not failed
retries: 60
delay: 10
notify: "Delete checksum file"
ansible.builtin.get_url:
url: "{{ oc_client_install_url.split('/')[:-1] | join('/') }}/{{ checksum_file_name }}"
dest: "{{ oc_client_install_archive_dest_dir }}/{{ checksum_file_name }}"
mode: '0640'

- name: Read checksum file and store content in data variable
ansible.builtin.command: "cat {{ oc_client_install_archive_dest_dir }}/{{ checksum_file_name }}"
changed_when: false
register: data

- name: Download openshift-client-linux.tar.gz
register: download_result
until: download_result is not failed
retries: 60
delay: 10
notify: "Delete openshift-client-linux.tar.gz"
vars:
checksum: "{{ data.stdout_lines | select('match', '.*' ~ oc_client_install_url.split('/')[-1] ~ '$') }}"
ansible.builtin.get_url:
url: "{{ oc_client_install_url }}"
dest: "{{ oc_client_install_archive_dest_dir }}/{{ oc_client_install_archive_name }}"
mode: '0640'
checksum: "sha256:{{ checksum[0].split(' ')[0] }}"

- name: Print client's archive sha256
vars:
checksum: "{{ data.stdout_lines | select('match', '.*' ~ oc_client_install_url.split('/')[-1] ~ '$') }}"
ansible.builtin.debug:
msg: "sha256:{{ checksum[0].split(' ')[0] }}"

- name: Download openshift-client-linux.tar.gz
register: download_result
until: download_result is not failed
retries: 60
delay: 10
notify: "Delete openshift-client-linux.tar.gz"
vars:
checksum: "{{ data.stdout_lines | select('match', '.*' ~ oc_client_install_url.split('/')[-1] ~ '$') }}"
ansible.builtin.get_url:
url: "{{ oc_client_install_url }}"
dest: "{{ oc_client_install_archive_dest_dir }}/{{ oc_client_install_archive_name }}"
mode: '0640'
checksum: "sha256:{{ checksum[0].split(' ')[0] }}"

- name: Extract archive
ansible.builtin.unarchive:
src: "{{ oc_client_install_archive_dest_dir }}/{{ oc_client_install_archive_name }}"
Expand Down
1 change: 1 addition & 0 deletions playbooks/roles/oc_client_install/vars/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
# vars file for oc_client_install
oc_client_install_archive_name: openshift-client-linux.tar.gz
checksum_file_name: sha256sum.txt
mirror_url: https://mirror.openshift.com/pub/openshift-v4/x86_64/clients/ocp

0 comments on commit 3b02092

Please sign in to comment.