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

https://stackoverflow.com/questions/44713880/how-do-i-make-decision-based-on-arch-in-ansible-playbooks/44903506#44903506

Asked 8 years, 6 months ago Modified 9 months ago Viewed 36k times 29

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 can not figure out how do I get the arch of the system I am running it on.

How to figure out the arch of the system in Ansible playbook?

ansible Share Improve this question Follow edited Apr 6, 2024 at 17:58 peterh’s user avatar peterh 1 asked Jun 23, 2017 at 5:45 Pensu’s user avatar Pensu 3,6891111 gold badges5252 silver badges7474 bronze badges Add a comment 5 Answers Sorted by:

Highest score (default) 26

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_facts.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_facts.ansible_architecture == “x86_64” Share Improve this answer Follow edited Mar 18 at 23:24 Elijah’s user avatar Elijah 2,3182525 silver badges3535 bronze badges answered Jul 4, 2017 at 10:29 infosecDaemon’s user avatar infosecDaemon 66366 silver badges77 bronze badges Sign up to request clarification or add additional context in comments.

Comments

24

@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 }}” Share Improve this answer Follow answered Oct 10, 2018 at 13:59 Romain DEQUIDT’s user avatar Romain DEQUIDT 9241111 silver badges1515 bronze badges 1 Comment

Per Over a year ago Prints “amd64” instead of “x86_64” which “ansible_architecture” would. 17

Ansible gathers suitable information from target hosts and stores it as facts in ansible_facts, for example: ansible_facts.ansible_architecture, ansible_facts.ansible_os_family.

If you are in doubt, you can display all facts with the debug module and choose the ones that suit you best.

You can use ansible_facts.ansible_architecture in when conditions, and use it to include different task-files (an example to customise).

Share Improve this answer Follow edited Mar 18 at 23:26 Elijah’s user avatar Elijah 2,3182525 silver badges3535 bronze badges answered Jun 23, 2017 at 6:10 techraf’s user avatar techraf 69.3k3131 gold badges211211 silver badges215215 bronze badges 4 Comments

Pensu Over a year ago Thanks, ansible_architecture works well, but there is one issue, for amd64, ansible_architecture shows it as x86_64, to make my playbook work, I need amd64, any idea which fact can give me and64?

techraf Over a year ago As I mentioned, you only display all facts and check if the one you want is there. If not, you need to implement the check yourself.

200_success Over a year ago Note that there is also an ansible_machine fact, where ansible_machine might be “i686”, whereas ansible_architecture on the same host would be “i386”.

bloudraak Over a year ago For FreeBSD 13 on POWER, ansible_architecture will always return powerpc regardless of the actual architecture (powerpc64 or powerpc64le). In part uname -m returns the same. Add a comment 6

While I was not able to find ansible_architecture documented anywhere on docs.ansible.com, here is a list of compiled possible value found while grepping ansible source code:

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.* Share Improve this answer Follow edited Oct 2, 2020 at 8:24 answered Oct 2, 2020 at 8:16 sorin’s user avatar sorin 173k194194 gold badges583583 silver badges861861 bronze badges 2 Comments

ku1ik Over a year ago In case it’s useful to anyone Raspberry Pi Zero W reports armv6l.

bitinerant Over a year ago Here is a more complete mapping: { “armv6l”: “armhf”, “armv7l”: “armhf”, “aarch64”: “arm64”, “x86_64”: “amd64”, “i386”: “i386” } (thanks to Cybervitexus - source) 3

I used something like this to make it work for me:

  • name: Set architecture specific variables set_fact: arch: “{{ ‘arm64’ if ansible_facts.architecture == ‘aarch64’ else ‘amd64’ }}” This can be modified depending on the arches you are after. Then you can use arch in your installation task depending on the package you want to install. E.g. https://link-to-file/file-{{arch}}.tar.gz

Share Improve this answer Follow edited Mar 18 at 23:22 Elijah’s user avatar Elijah 2,3182525 silver badges3535 bronze badges answered Jun 5, 2024 at 14:47 Afterlife’s user avatar Afterlife 495

Updated: