manisnesan / til

collection of today i learned scripts
4 stars 0 forks source link

Ansible playbook to download files from s3 #25

Open manisnesan opened 1 year ago

manisnesan commented 1 year ago

Add the encrypted strings for the secrets SHARED_S3_ID and SHARED_S3_SECRET

Playbook role to download files from s3 bucket

---
- name: Recursively remove personalization directory incorrectly added
  file:
    path: /opt/jboss-eap/modules/com/redhat/gss/diagnostics/search/personalization
    state: absent

- name: Create personalization directory
  file:
    state: directory
    path: /opt/jboss-eap/modules/com/redhat/gss/diagnostics/search/main/personalization
    owner: jboss-eap
    group: jboss-eap
    mode: "0755"

- name: Display the value of ansible_python_interpreter
  debug:
    var: ansible_python_interpreter

- name: Check if boto3 and botocore is installed and install if not
  pip:
    name: ['boto3', 'botocore']
    state: present
  register: boto_check

- name: Display result
  debug:
    msg: "boto3 and botocore are installed"
  when: boto_check is succeeded

- name: Download user preferences artifact `user_index_map.json`
  aws_s3:
    bucket: rh-stage-ssa
    prefix: pnt-cee/dxp/cp-search-rex/
    object: pnt-cee/dxp/cp-search-rex/data/processed/prod/user_index_map.json
    dest: /opt/jboss-eap/modules/com/redhat/gss/diagnostics/search/main/personalization/user_index_map.json
    mode: get
    overwrite: different
    aws_access_key: "{{ SHARED_S3_ID }}"
    aws_secret_key: "{{ SHARED_S3_SECRET }}"

- name: Download user preferences artifact `url_index_map.json`
  aws_s3:
    bucket: rh-stage-ssa
    prefix: pnt-cee/dxp/cp-search-rex/
    object: pnt-cee/dxp/cp-search-rex/data/processed/prod/url_index_map.json
    dest: /opt/jboss-eap/modules/com/redhat/gss/diagnostics/search/main/personalization/url_index_map.json
    mode: get
    overwrite: different
    aws_access_key: "{{ SHARED_S3_ID }}"
    aws_secret_key: "{{ SHARED_S3_SECRET }}"

- name: Check user preferences artifact `preds.txt` has changed
  stat:
    path: /opt/jboss-eap/modules/com/redhat/gss/diagnostics/search/main/personalization/preds.txt
  register: preds_stat

- name: Download user preferences artifact `preds.txt`
  aws_s3:
    bucket: rh-stage-ssa
    prefix: pnt-cee/dxp/cp-search-rex/
    object: pnt-cee/dxp/cp-search-rex/models/prod/preds.txt
    dest: /opt/jboss-eap/modules/com/redhat/gss/diagnostics/search/main/personalization/preds.txt
    mode: get
    overwrite: different
    aws_access_key: "{{ SHARED_S3_ID }}"
    aws_secret_key: "{{ SHARED_S3_SECRET }}"

- name: Check if user preferences artifacts are present
  stat:
    path: "{{ item }}"
  register: pref
  loop:
    - /opt/jboss-eap/modules/com/redhat/gss/diagnostics/search/main/personalization/user_index_map.json
    - /opt/jboss-eap/modules/com/redhat/gss/diagnostics/search/main/personalization/url_index_map.json
    - /opt/jboss-eap/modules/com/redhat/gss/diagnostics/search/main/personalization/preds.txt

- name: Recursively change ownership of the contents of personalization directory
  file:
    state: directory
    path: /opt/jboss-eap/modules/com/redhat/gss/diagnostics/search/main/personalization
    recurse: yes
    owner: jboss-eap
    group: jboss-eap
    mode: "0755"

- name: List user preferences artifacts
  debug:
    msg: "{{ pref.results }}"
manisnesan commented 1 year ago

Before adding the role to ansible tower, create a playbook.yml to test it locally.

---
- hosts: 127.0.0.1
  connection: local
  become: true
  gather_facts: true
  roles:
    - ir_search_user_pers_deploy
localhost | FAILED! => {
    "msg": "The module aws_s3 was redirected to amazon.aws.aws_s3, which could not be loaded."
}

This indicates the module is not available since this is a community module. Hence installed the module using ansible-galaxy command

± ansible-galaxy collection install community.aws
Starting galaxy collection install process
Process install dependency map
Starting collection install process
Downloading https://galaxy.ansible.com/download/community-aws-5.2.0.tar.gz to /home/msivanes/.ansible/tmp/ansible-local-13648875smm6jis/tmplbcexm4z/community-aws-5.2.0-rf4j_14s
Installing 'community.aws:5.2.0' to '/home/msivanes/.ansible/collections/ansible_collections/community/aws'
Downloading https://galaxy.ansible.com/download/amazon-aws-5.2.0.tar.gz to /home/msivanes/.ansible/tmp/ansible-local-13648875smm6jis/tmplbcexm4z/amazon-aws-5.2.0-cevnhlnl
community.aws:5.2.0 was installed successfully
Installing 'amazon.aws:5.2.0' to '/home/msivanes/.ansible/collections/ansible_collections/amazon/aws'
amazon.aws:5.2.0 was installed successfully
manisnesan commented 1 year ago

Discovered the jupyter kernel for ansible that allows to run the playbook/tasks from Jupyter https://github.com/ansible/ansible-jupyter-kernel