1

I start node_exporter like this

cd /exporter

then

./node_exporter &

I tried running node_exporter with Ansible using shell module but had no success

- name: Run Exporter
  become: yes
  ansible.builtin.shell: " ./node_exporter & "
  args:
    chdir: /home/netmera/exporter/

Any help would be appreciated.

U880D
  • 8,601
  • 6
  • 24
  • 40
İlker Demirci
  • 243
  • 1
  • 3
  • 12
  • 1
    Running in background won't work with ansible as the process will be killed as soon as the task exits. You could run this with `nohup` but that would still be a bad practice. See @U880D answer for a solution that is more in the state of the art. – Zeitounator Jan 10 '22 at 11:35

1 Answers1

5

According your description you may introduce automatic installation like

- name: Download and unpack node exporter binary to /usr/local/bin
  unarchive:
    src: "https://{{ URL}}/node_exporter-{{ NODE_EXPORTER_VERSION }}.linux-amd64.tar.gz"
    dest: "/usr/local/bin/"
    remote_src: yes
    extra_opts: [--strip-components=1]
    owner: "root"
    group: "root" 

create a group and user to run the node_exporter as a service like

- name: Create group 'node_exporter' to run the node exporter service
  group:
    name: "node_exporter"
    state: present

- name: Create user 'node_exporter' to run the node exporter service
  user:
    name: "node_exporter"
    group: "node_exporter"
    state: present
    ...

a systemd node_exporter.service file like

[Unit]
Description=Node Exporter
After=network.target
    
[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter
     
[Install]
WantedBy=multi-user.target

and roll it out via copy or template_module depending on your infrastructre, environment and needs.

After that you can use the systemd_module to manage the service.

- name: Make sure 'node_exporter' service is started and enabled
  systemd:
    name: node_exporter
    state: started
    enabled: yes
    daemon_reload: yes

Furthermore it might be necessary to manage the local firewalld and open ports like

- name: Do permit traffic in default zone for 'node_exporter' on port 9001/tcp
  firewalld:
    port: 9001/tcp
    permanent: yes
    state: enabled
Zeitounator
  • 38,476
  • 7
  • 53
  • 66
U880D
  • 8,601
  • 6
  • 24
  • 40
  • Hello, thanks for the reply. Actually what i want to achive is just running the node_exporter process. When i run the "./node_exporter &" on the shell, the process starts. So i thought with the shell module i can manage to run it like so, but it seem i need a module like systemd and for it i need a .service file beforehand. I will run this playbook for the new machines, so i try to avoid creating file etc.. Is there another module to run it directly? – İlker Demirci Jan 10 '22 at 14:58
  • @İlkerDemirci, regarding your question "_Is there another module to run it directly?_", this was already answered in a comment by @Zeitounator, "_Running in background won't work with Ansible as the process will be killed as soon as the task exits. You could run this with `nohup` but that would still be a bad practice._". So in short the answer is no. – U880D Jan 11 '22 at 08:52