From 5d463eff4c068f81946dac42601cb49ef8b520fc Mon Sep 17 00:00:00 2001 From: kenorb Date: Sat, 24 Feb 2024 16:09:54 +0000 Subject: [PATCH] Improves fact logic --- tasks/main.yml | 82 ++++++++++++++++++++++++++++++++++++------------ tasks/verify.yml | 13 -------- 2 files changed, 62 insertions(+), 33 deletions(-) diff --git a/tasks/main.yml b/tasks/main.yml index d054651..dc170fe 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,31 +1,40 @@ --- -- name: Validates variables - ansible.builtin.assert: - that: - - metatrader_setup_url | length > 0 -- name: Checks if platform is already installed - register: file_mt_installed_exists +- name: Collects local facts + ansible.builtin.setup: + filter: + - ansible_local + when: false +- name: Checks if platform is already exist ansible.builtin.stat: - path: ~/.wine/.installed-mt{{ metatrader_version }} - when: - - ansible_os_family != "Windows" -- name: Ensures Curl is present - ansible.builtin.package: - name: curl - state: present - when: - - ansible_os_family != "Windows" - - not file_mt_installed_exists.stat.exists -- name: Installs MetaTrader (via Winetricks) - when: - - ansible_os_family != "Windows" - - not file_mt_installed_exists.stat.exists + path: "{{ ansible_local['metatrader'][metatrader_version | string].path }}" + register: stat_mt + when: ansible_local['metatrader'] is defined +- name: Ensures platform is installed block: + - name: Validates variables + ansible.builtin.assert: + that: + - metatrader_setup_url | length > 0 + - name: Checks if platform is already installed + register: file_mt_installed_exists + ansible.builtin.stat: + path: ~/.wine/.installed-mt{{ metatrader_version }} + when: + - ansible_os_family != "Windows" + - name: Ensures Curl is present + ansible.builtin.package: + name: curl + state: present + when: + - ansible_os_family != "Windows" + - not file_mt_installed_exists.stat.exists - name: Ensures verb installation file is present ansible.builtin.template: src: mt{{ metatrader_version }}_install.verb.j2 dest: /tmp/mt{{ metatrader_version }}_install.verb mode: "0640" + when: + - not file_mt_installed_exists.stat.exists - name: Ensures MetaTrader is installed (via verb file) ansible.builtin.shell: # noqa command-instead-of-shell @@ -37,11 +46,44 @@ creates: ~/.wine/.installed-mt{{ metatrader_version }} args: executable: /bin/bash + when: + - not file_mt_installed_exists.stat.exists - name: Ensures verb installation file is absent ansible.builtin.file: path: /tmp/mt{{ metatrader_version }}_install.verb state: absent + when: + - ansible_local['metatrader'] is not defined + - ansible_os_family != "Windows" # gather_facts is required. + - not stat_mt.stat.exists - name: Verifies ansible.builtin.include_tasks: verify.yml tags: - metatrader_verify +- name: Stores platform paths as facts + block: + - name: Sets fact + ansible.builtin.set_fact: + cacheable: true + metatrader: >- + { + '{{ metatrader_version | int }}': + { + 'path': '{{ find_mt_res.files[0].path | dirname }}' + } + } + when: + - find_mt_res is defined + - find_mt_res.files | length > 0 + - metatrader_version is defined + - name: Ensures that Ansible local facts directory exists + ansible.builtin.file: + path: /etc/ansible/facts.d + state: directory + mode: '0755' + - name: Saves role's facts + ansible.builtin.copy: + content: "{{ ansible_facts['metatrader'] }}" + dest: /etc/ansible/facts.d/metatrader.fact + when: + - ansible_local['metatrader'] is not defined diff --git a/tasks/verify.yml b/tasks/verify.yml index 45e3150..58bf970 100644 --- a/tasks/verify.yml +++ b/tasks/verify.yml @@ -22,16 +22,3 @@ recurse: true register: find_mte_res when: ansible_os_family != "Windows" -- name: Stores platform paths as facts - block: - - name: Sets fact - ansible.builtin.set_fact: - metatrader: >- - { '{{ metatrader_version | int }}': {'path': '{{ find_mt_res.files[0].path | dirname }}'}} - when: - - find_mt_res is defined - - find_mt_res.files | length > 0 - - metatrader_version is defined - - name: Prints current facts - ansible.builtin.debug: - var: hostvars[inventory_hostname]['metatrader']