Browse Source

implemented pull-files

Grega Bremec 1 năm trước cách đây
mục cha
commit
af3f0de2f3

+ 9 - 0
roles/pull-files/tasks/main.yml

@@ -0,0 +1,9 @@
+---
+# Download all the files needed on workstation. Extract them if needed.
+# Rename the target directory if necessary. Remove after extract if so wanted.
+- name: Do the thing, in a loop.
+  include_tasks: sequence.yml
+  loop: "{{ pull_files}}"
+  loop_control:
+    label: "{{ item.name }}"
+...

+ 96 - 0
roles/pull-files/tasks/sequence.yml

@@ -0,0 +1,96 @@
+---
+- name: Inform what is going on
+  pause:
+    prompt: |-
+      *************************************************************************************
+      Checking if {{ item.name }} needs to be processed...
+      *************************************************************************************
+    seconds: 0
+
+- name: Get download confirmation code from Google Drive
+  uri:
+    url: "https://drive.google.com/uc?export=download&id={{ item.file_id }}"
+    creates: "{{ ansible_facts['user_dir'] }}/Downloads/{{ item.name }}"
+    return_content: yes
+    status_code: 200
+  register: dl_cc
+  when: not (item.direct_download | default(true))
+
+- name: Store conf code as a fact
+  set_fact:
+    src_url: "{{ dl_cc.content | regex_replace('^.*action=\"', '') | regex_replace('\".*$', '') | regex_replace('amp;', '') }}"
+  when: dl_cc.content is defined
+
+- name: Store direct URL as a fact
+  set_fact:
+    src_url: "https://drive.google.com/uc?export=download&id={{ item.file_id }}"
+  when:
+    - dl_cc.content is not defined
+    - item.direct_download | default(true)
+
+- name: Check if the target dir is already there
+  ansible.builtin.stat:
+    path: "{{ item.target_dir }}"
+  register: targetdir
+  when: item.target_dir is defined
+
+- name: Download the file
+  get_url:
+    url: "{{ src_url }}"
+    dest: "{{ ansible_facts['user_dir'] }}/Downloads/{{ item.name }}"
+    mode: 0664
+    timeout: 30
+    checksum: "{{ item.checksum | default('') }}"
+  when:
+    - (src_url | default(None)) != None
+    - item.target_dir is not defined or (targetdir.stat is defined and not targetdir.stat.exists)
+  register: downloaded
+
+- name: Extract if so required
+  become: yes
+  unarchive:
+    src: "{{ ansible_facts['user_dir'] }}/Downloads/{{ item.name }}"
+    remote_src: yes
+    creates: "{{ item.extracted_dir }}"
+    dest: "{{ item.extract_to | default('/') }}"
+  register: extract_action
+  when:
+    - item.extracted_dir is defined
+    - downloaded.changed
+
+- name: Rename wherever the archive extracted to whatever new name is required, if so
+  become: yes
+  command: mv {{ item.extracted_dir }} {{ item.target_dir }}
+  when:
+    - item.target_dir is defined
+    - extract_action is defined
+    - extract_action.changed
+
+- name: Make sure bin is in PATH
+  lineinfile:
+    path: "{{ ansible_facts['user_dir'] }}/.bashrc"
+    line: 'PATH="${PATH}:{{ item.target_dir | default(item.extracted_dir) }}/bin"'
+    regexp: '^PATH=.*{{ item.target_dir | default(item.extracted_dir) }}/bin'
+    insertafter: "^# User specific environment$"
+    state: present
+  when:
+    - item.add_to_path is defined
+    - item.add_to_path
+    - item.extracted_dir is defined
+
+- name: Remove after finished if so required
+  file:
+    path: "{{ ansible_facts['user_dir'] }}/Downloads/{{ item.name }}"
+    state: absent
+  when:
+    - item.remove_after is defined
+    - item.remove_after
+
+- name: reset facts
+  set_fact:
+    dl_cc: !!null
+    src_url: !!null
+    targetdir: !!null
+    downloaded: !!null
+    extract_action: !!null
+...

+ 12 - 0
roles/pull-files/vars/main.yml

@@ -0,0 +1,12 @@
+---
+# Needs a list of files in the pull_files variable:
+# - name: filename.txt
+#   file_id: google-drive-id
+#   checksum: sha256:xxxxxxxxxx
+#   extract_to: /opt
+#   extracted_dir: /opt/foobar
+#   target_dir: /opt/newname
+#   add_to_path: yes
+#   remove_after: yes
+#   direct_download: yes
+...