prometheus-community / ansible

Ansible Collection for Prometheus
https://prometheus-community.github.io/ansible/
Apache License 2.0
397 stars 134 forks source link

fix: make sure binary install dir exists #423

Closed namsic closed 1 month ago

namsic commented 1 month ago

I'm trying to install node_exporter on a remote host with a custom node_exporter_binary_install_dir.

- hosts: all
  roles:
    - prometheus.prometheus.node_exporter
  vars:
    node_exporter_binary_local_dir: /home/namsic/downloads/node_exporter-1.6.0.linux-amd64
    node_exporter_binary_install_dir: /home/namsic/tmp

But when I ran the above playbook, I got the following result:

TASK [prometheus.prometheus.node_exporter : Propagate locally distributed node_exporter binary] ****************************************************************************************************************************************************************************************************************************************************************************
fatal: [remote001]: FAILED! => {
    "changed": false,
    "checksum": "894db4cbc1bda8d686d73f33f22c57c861836c9d",
    "msg": "Destination directory /home/namsic/tmp does not exist"
}

If there are multiple target hosts, it would be nice to automatically create the directory if it does not exist.

If these changes are not appropriate, please let me know.

Thanks.

namsic commented 1 month ago

Can I just add the same change to every roles?

fix: ensure binary_install_dir before copy binary

Or should I do something else?

gardar commented 1 month ago

Can I just add the same change to every roles?

fix: ensure binary_install_dir before copy binary

Or should I do something else?

You can add a task to the install tasks file of the _common role which all the roles use.

Adding this task before the last task, the one named "Propagate binaries", in _common/tasks/install.yml would probably do the trick.

- name: "Make sure binary install dir exists"
  ansible.builtin.file:
    path: "{{ _common_binary_install_dir }}"
    mode: 0755
    owner: root
    group: root
  become: true
  tags:
    - "{{ ansible_parent_role_names | first | regex_replace(ansible_collection_name ~ '.', '') }}"
    - install
    - "{{ ansible_parent_role_names | first | regex_replace(ansible_collection_name ~ '.', '') }}_install"
namsic commented 1 month ago

Thanks for the detailed explanation. I changed it according to your comment.

SuperQ commented 1 month ago

I think we should use ansible.bultin.stat as a preflight check, rather than actually try and create the target directory.

namsic commented 1 month ago

Can someone help me?

I can't figure out how my ansible.builtin.stat changes relate to the error below.

INFO     Running alternative > destroy
INFO     Sanity checks: 'docker'
[WARNING]: Collection community.docker does not support Ansible version
2.11.12.post0

PLAY [Destroy] *****************************************************************

TASK [Destroy molecule instance(s)] ********************************************
fatal: [localhost]: FAILED! => {
   "msg": "Could not find imported module support code for ansible_collections.community.docker.plugins.modules.docker_container.
           Looked for (['ansible.module_utils.compat.version.LooseVersion', 'ansible.module_utils.compat.version'])"
}

PLAY RECAP *********************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

WARNING  Retrying execution failure 2 of: ansible-playbook --inventory /root/.cache/molecule/alertmanager/alternative/inventory --skip-tags molecule-notest,notest /usr/local/lib/python3.6/dist-packages/molecule_docker/playbooks/destroy.yml
CRITICAL Ansible return code was 2, command was: ['ansible-playbook', '--inventory', '/root/.cache/molecule/alertmanager/alternative/inventory', '--skip-tags', 'molecule-notest,notest', '/usr/local/lib/python3.6/dist-packages/molecule_docker/playbooks/destroy.yml']
gardar commented 1 month ago

Can someone help me?

I can't figure out how my ansible.builtin.stat changes relate to the error below.

INFO     Running alternative > destroy
INFO     Sanity checks: 'docker'
[WARNING]: Collection community.docker does not support Ansible version
2.11.12.post0

PLAY [Destroy] *****************************************************************

TASK [Destroy molecule instance(s)] ********************************************
fatal: [localhost]: FAILED! => {
   "msg": "Could not find imported module support code for ansible_collections.community.docker.plugins.modules.docker_container.
           Looked for (['ansible.module_utils.compat.version.LooseVersion', 'ansible.module_utils.compat.version'])"
}

PLAY RECAP *********************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

WARNING  Retrying execution failure 2 of: ansible-playbook --inventory /root/.cache/molecule/alertmanager/alternative/inventory --skip-tags molecule-notest,notest /usr/local/lib/python3.6/dist-packages/molecule_docker/playbooks/destroy.yml
CRITICAL Ansible return code was 2, command was: ['ansible-playbook', '--inventory', '/root/.cache/molecule/alertmanager/alternative/inventory', '--skip-tags', 'molecule-notest,notest', '/usr/local/lib/python3.6/dist-packages/molecule_docker/playbooks/destroy.yml']

That issue is unrelated to your change, the docker ansible collection which is used for the tests was updated recently and that broke compatibility with old ansible versions. I fixed the issue in #440

github-actions[bot] commented 1 month ago

Docs Build 📝

Thank you for contribution!✨

This PR has been merged and the docs are now incorporated into main: https://prometheus-community.github.io/ansible/branch/main