如何在Ansible中仅定义一次node_hosts变量供所有任务调用?
如何在Ansible Playbook中全局定义
node_hosts变量避免重复定义 我管理着不同架构的微型节点服务器,写了一个安装node_exporter的Ansible Playbook,现在每个用到架构映射的任务里都重复定义了node_hosts变量,想知道怎么只定义一次就让所有任务都能访问。
方案1:在Play的全局vars块中定义(推荐)
把node_hosts变量移到Play最顶层的vars区域,这样整个Play里的所有任务都能直接引用这个变量,不需要在每个任务里重复编写。
修改后的完整Playbook示例:
--- - name: Install and configure node_exporter hosts: your_target_hosts # 替换成你的目标主机组/主机 vars: node_hosts: x86_64: archi: amd64 aarch64: archi: arm64 armv6l: archi: armv6 node_exporter_version: "1.8.2" # 替换成你需要的版本 tasks: - name: download node exporter tags: ["node_exporter"] 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"] 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"] 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 temporary archive and directory tags: ["node_exporter"] 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
方案2:用group_vars/host_vars实现跨Playbook复用
如果这个架构映射变量需要在多个Playbook里使用,可以把它放到Ansible的标准变量目录中:
- 在你的Ansible项目根目录下创建
group_vars/all/文件夹 - 在该文件夹下新建文件(比如
node_exporter_vars.yml),写入变量内容:
node_hosts: x86_64: archi: amd64 aarch64: archi: arm64 armv6l: archi: armv6 node_exporter_version: "1.8.2"
这样所有主机都会自动加载这个变量,任何Playbook的任务都能直接引用,不需要在Play里重复定义。
补充优化:提前生成架构对应文件名变量
还可以用set_fact提前生成完整的文件名变量,进一步简化任务里的模板字符串,减少重复代码:
--- - name: Install and configure node_exporter hosts: your_target_hosts vars: node_hosts: x86_64: archi: amd64 aarch64: archi: arm64 armv6l: archi: armv6 node_exporter_version: "1.8.2" tasks: - name: Set node_exporter filename facts set_fact: node_exporter_archive_name: "node_exporter-{{ node_exporter_version }}.linux-{{ node_hosts[ansible_architecture]['archi'] }}" node_exporter_tar_path: "/tmp/{{ node_exporter_archive_name }}.tar.gz" - name: download node exporter tags: ["node_exporter"] get_url: url: "https://github.com/prometheus/node_exporter/releases/download/v{{ node_exporter_version }}/{{ node_exporter_archive_name }}.tar.gz" dest: "{{ node_exporter_tar_path }}" - name: unarchive node exporter tags: ["node_exporter"] unarchive: remote_src: yes src: "{{ node_exporter_tar_path }}" dest: /tmp # 后续任务直接引用{{ node_exporter_archive_name }}即可
内容的提问来源于stack exchange,提问作者Ciasto piekarz
相关产品推荐
相关产品推荐

