I have situation where I have micro node server of different architectures so in ansible I wrote this approach to build the download path based on platform architecture and download and install to disk
---
- name: download node exporter
tags: ["node_exporter"]
vars:
node_hosts:
x86_64:
archi: amd64
aarch64:
archi: arm64
armv6l:
archi: armv6
get_url:
url: https://github.com/prometheus/node_exporter/releases/download/v{{ node_exporter_version }}/node_exporter-{{ node_exporter_version }}.linux-{{node_hosts[ansible_architecture]['archi']}}.tar.gz
dest: /tmp
- name: unarchive node exporter
tags: ["node_exporter"]
vars:
node_hosts:
x86_64:
archi: amd64
aarch64:
archi: arm64
armv6l:
archi: armv6
unarchive:
remote_src: yes
src: /tmp/node_exporter-{{ node_exporter_version }}.linux-{{node_hosts[ansible_architecture]['archi']}}.tar.gz
dest: /tmp
- name: move node exporter to /usr/local/bin
tags: ["node_exporter"]
vars:
node_hosts:
x86_64:
archi: amd64
aarch64:
archi: arm64
armv6l:
archi: armv6
copy:
src: /tmp/node_exporter-{{ node_exporter_version }}.linux-{{node_hosts[ansible_architecture]['archi']}}/node_exporter
dest: /usr/local/bin/node_exporter
remote_src: yes
owner: root
group: root
mode: 0755
- name: remove repo.
tags: ["node_exporter"]
vars:
node_hosts:
x86_64:
archi: amd64
aarch64:
archi: arm64
armv6l:
archi: armv6
file:
state: absent
path: /tmp/node_exporter-{{ node_exporter_version }}.linux-{{node_hosts[ansible_architecture]['archi']}}
- name: install unit file to systemd
tags: ["node_exporter"]
template:
src: templates/node_exporter.service.j2
dest: /etc/systemd/system/node_exporter.service
owner: root
group: root
mode: 0600
- name: configure systemd to use service
tags: ["node_exporter"]
systemd:
daemon_reload: yes
enabled: yes
state: started
name: node_exporter.service
My question here is: how can I create the node_hosts variable only once and access the value each time in all the tasks instead if recreating them in each task.