How to perform bitwise operation inside ansible playbook

290 views
Skip to first unread message

Reshma Pattan

unread,
Aug 12, 2021, 6:14:53 AM8/12/21
to Ansible Project
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?), 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

Brian Coca

unread,
Aug 12, 2021, 10:43:05 AM8/12/21
to Ansible Project
have you tried using python bitwise operators (they should work in any
Jinja2 template)?

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


--
----------
Brian Coca

Reshma Pattan

unread,
Sep 3, 2021, 5:38:50 AM9/3/21
to Ansible Project
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
Reply all
Reply to author
Forward
0 new messages