Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make decision based on arch in Ansible playbooks?

Tags:

ansible

I have been trying to write playbooks where I can run different tasks based on the arch (i.e amd64, arm, ppc64le) that the playbook is running on. I am not able to figure out how do I get the arch of the system I am running it on.

Can you please help me with figuring out the arch of the system in Ansible playbook.

like image 625
Pensu Avatar asked Jun 23 '17 05:06

Pensu


2 Answers

To get the architecture of the system

At the command line:

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

For an x86 architecture host, this would return:

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

Here’s a sample playbook that will print out the architecture of all hosts in your inventory:

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

To run tasks based off the architecture

Use a when clause:

- name: task to run for x86 architecture
  shell: echo "x86 arch found here"
  when: ansible_architecture == "x86_64"
like image 148
infosecDaemon Avatar answered Sep 21 '22 07:09

infosecDaemon


@Pensu it's maybe what you're looking for:

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

- name: Print DEB architecture
  debug:
    msg: "deb_architecture.stdout: {{ deb_architecture.stdout }}"
like image 38
Romain DEQUIDT Avatar answered Sep 23 '22 07:09

Romain DEQUIDT