我如何根据 Ansible playbook 中的 arch 做出决策?

问题描述 投票:0回答:4

我一直在尝试编写剧本,我可以根据剧本运行的架构(即 amd64、arm、ppc64le)运行不同的任务。我不知道如何获得正在运行的系统的架构。

如何在 Ansible playbook 中找出系统的架构?

ansible
4个回答
24
投票

获取系统架构

在命令行:

ansible HOST -m setup -a 'filter=ansible_architecture'

对于 x86 架构主机,这将返回:

HOST | SUCCESS => {
    "ansible_facts": {
        "ansible_architecture": "x86_64"
    }, 
    "changed": false
}

这是一个示例手册,它将打印出清单中所有主机的架构:

- name: print out hosts architectures
  hosts: all
  gather_facts: True
  tasks:
  - debug: var= ansible_architecture

基于架构运行任务

使用

when
子句:

- name: task to run for x86 architecture
  shell: echo "x86 arch found here"
  when: ansible_architecture == "x86_64"

19
投票

@Pensu 也许这就是您正在寻找的:

- name: Get DEB architecture
  shell: dpkg --print-architecture
  register: deb_architecture

- name: Print DEB architecture
  debug:
    msg: "deb_architecture.stdout: {{ deb_architecture.stdout }}"

17
投票

Ansible 从目标主机收集适当的信息并将其存储为事实,例如:

ansible_architecture
ansible_os_family

如果您有疑问,您可以使用

debug
模块显示所有事实,并选择最适合您的事实。

您可以在

ansible_architecture
条件下使用
when
事实,并使用它来包含不同的任务文件(自定义示例)。


4
投票

虽然我无法在

docs.ansible.com
上找到 ansible_architecture 记录,但这里是在 grepping ansible 源代码时找到的已编译可能值的列表:

x86_64  # <-- guaranteed to work
aarch64  # <-- found on https://git.e1e0.net/ansible-playbooks/file/roles/node-exporter/vars/main.yml.html
ia64
ppc64le
s390x

# found some matches like:
armv.*
aarch.*
ppc.*
© www.soinside.com 2019 - 2024. All rights reserved.