# How to perform bitwise operation inside ansible playbook

**URL:** <https://forum.ansible.com/t/how-to-perform-bitwise-operation-inside-ansible-playbook/35613>\
**Category:** Ansible Project\
**Created:** [August 12, 2021, 10:14am UTC](https://forum.ansible.com/t/how-to-perform-bitwise-operation-inside-ansible-playbook/35613 "2021-08-12T10:14:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Reshma\_Pattan](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@Reshma\_Pattan](https://forum.ansible.com/u/Reshma_Pattan)\
**Post date:** [August 12, 2021, 10:14am UTC](https://forum.ansible.com/t/how-to-perform-bitwise-operation-inside-ansible-playbook/35613/1 "2021-08-12T10:14:52Z")

</div>

Hi,

I am looking to perform “bitwise OR” operation like below inside the Ansible playbook.

0x102030 or 0x908060

I understood from the internet that there is no direct way to do this. So, as sugegsted in this post ([How to perform bitwise operations in Ansible?](https://stackoverflow.com/questions/48550786/how-to-perform-bitwise-operations-in-ansible)), I have created the .py file for **“bitwise or”** and placed under specific paths under filter dir.

1)The ansible **“.cfg”** file is edited to specify the filter plugin path.

**filter\_plugins = /usr/share/ansible/plugins/filter:~/.ansible/plugins/filter**

2)Also I tried setting env variable **“ANSIBLE\_FILTER\_PLUGINS”** with the dir path of filter plugin.

3)Ansible version on controller node is: **3.6.10**

But unfortunately I dont see the playbook is able to find the new defined filter. I am not sure if I am missing anything. Is there anything else i need to be done?

Is there a way i can do bitwise operation using simple way.

Any help with this is appreciate.

Thanks,

Reshma

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [August 12, 2021, 2:42pm UTC](https://forum.ansible.com/t/how-to-perform-bitwise-operation-inside-ansible-playbook/35613/2 "2021-08-12T14:42:06Z")

</div>

have you tried using python bitwise operators (they should work in any  
Jinja2 template)?

\<\<, \>\>, &, |, ~, and ^

---

<div class="post-metadata">

**Author:** ![Reshma\_Pattan](https://avatars.discourse-cdn.com/v4/letter/r/977dab/32.png) [@Reshma\_Pattan](https://forum.ansible.com/u/Reshma_Pattan)\
**Post date:** [September 3, 2021, 9:38am UTC](https://forum.ansible.com/t/how-to-perform-bitwise-operation-inside-ansible-playbook/35613/3 "2021-09-03T09:38:50Z")

</div>

Hi Brian,

I tried using **’ |** ’ operator inside the jinja template , but it was not considered as "btwise or " operator, instead it is considered as pipe for filter operation that follows it.

The filter\_plugin I tried is working now, there was a small coding issue inside the filetr.py file , which was reported by playbook run logs on the console, which i missed to look.

After carefully looking into the logs, i could fix the coding issue and after that filter operation was found from the filter\_plugin path that was specified in the ansible.cfg file.

**filter\_plugins = /usr/share/ansible/plugins/filter**

There was no more setting needed apart from specifying the filter path in ansible.cfg

Thanks,  
Reshma
