如何根据 Ansible 剧本中的 arch 做出决定?

Pen*_*nsu 5 ansible

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

你能帮我弄清楚 Ansible playbook 中的系统架构吗?

tec*_*raf 9

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

如果您有疑问,您可以使用debug模块显示所有事实并选择最适合您的事实。

您可以ansible_architecturewhen条件中使用事实,并使用它来包含不同的任务文件(自定义示例)。

  • 谢谢,ansible_architecture工作得很好,但是有一个问题,对于amd64,ansible_architecture将其显示为x86_64,为了使我的剧本工作,我需要amd64,知道哪个事实可以给我and64吗? (5认同)

小智 9

获取系统架构

在命令行:

ansible HOST -m setup -a 'filter=ansible_architecture'
Run Code Online (Sandbox Code Playgroud)

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

HOST | SUCCESS => {
    "ansible_facts": {
        "ansible_architecture": "x86_64"
    }, 
    "changed": false
}
Run Code Online (Sandbox Code Playgroud)

这是一个示例剧本,它将打印出您库存中所有主机的架构:

- name: print out hosts architectures
  hosts: all
  gather_facts: True
  tasks:
  - debug: var= ansible_architecture
Run Code Online (Sandbox Code Playgroud)

运行基于架构的任务

使用when子句:

- name: task to run for x86 architecture
  shell: echo "x86 arch found here"
  when: ansible_architecture == "x86_64"
Run Code Online (Sandbox Code Playgroud)


Rom*_*IDT 8

@Pensu 这可能是您要找的:

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

- name: Print DEB architecture
  debug:
    msg: "deb_architecture.stdout: {{ deb_architecture.stdout }}"
Run Code Online (Sandbox Code Playgroud)