** Exponentiation<\/li><\/ul>\n\n\n\nLet’s go through some simple examples of each of the operators. After that, I will show you some more practical examples.<\/p>\n\n\n\n
Addition in Ansible<\/h2>\n\n\n\n Adding two numbers together is done with the + operator. To get Ansible to process the expression, you must wrap it with double curly brackets: {{ expression }}. To demonstrate this, I have created a simple playbook that only prints the result:<\/p>\n\n\n\n
---\n- name: Run some calculations\n hosts: localhost\n tasks:\n - name: Add 2 + 2\n debug:\n msg: The result is {{ 2 + 2 }}<\/pre>\n\n\n\nYou can try running this if you want to see it in action. But as I imagine you can already guess, the result will be 4:<\/p>\n\n\n\n
TASK [Add 2 + 2] *************************************************************************************\nok: [localhost] => {\n \"msg\": \"The result is 4\"\n}<\/pre>\n\n\n\nSubtraction in ansible<\/h2>\n\n\n\n Subtracting two numbers is just as simple. By using the – operator, the right side argument will be subtracted from the left one. Here we will subtract 5 from 10:<\/p>\n\n\n\n
- name: Subtract: 10-5\n debug:\n msg: The result is {{ 10 - 5 }}<\/pre>\n\n\n\nThis will output the following result:<\/p>\n\n\n\n
TASK [Subtract 10-5] *********************************************************************************\nok: [localhost] => {\n \"msg\": \"The result is 5\"\n}\n<\/pre>\n\n\n\nDivision in Ansible<\/h2>\n\n\n\n To divide, we use the \/ operator. This will divide the left argument by the right argument. Here we will divide 10 by 5:<\/p>\n\n\n\n
- name: Divide 10\/5\n debug:\n msg: The result is {{ 10 \/ 5 }}\n<\/pre>\n\n\n\nThe answer is 2, as we can tell from the output below. <\/p>\n\n\n\n
TASK [Divide 10\/5] ***********************************************************************************\nok: [localhost] => {\n \"msg\": \"The result is 2.0\"\n}<\/pre>\n\n\n\nAs you can see, the return value from a division operation will always be a floating point number. 2.0 in this case. This is not always what you want. If you want the return value to be a whole integer, you can use Jinja filters to round the number and then convert it to an integer.<\/p>\n\n\n\n
You can see this in action in the task below. Here I have wrapped the calculation in parenthesis to ensure that the filter is applied to the finished calculation. Then I add the pipe character (|) and call the round filter. The int filter then converts the result to an integer.<\/p>\n\n\n\n
- name: Divide 10\/5\n debug:\n msg: The result is {{ (10 \/ 5)|round|int }}\n<\/pre>\n\n\n\nThis time we will see the same result except without any decimal points.<\/p>\n\n\n\n
TASK [Divide 10\/5] ***********************************************************************************\nok: [localhost] => {\n \"msg\": \"The result is 2\"\n}\n<\/pre>\n\n\n\nMultiplication in Ansible<\/h2>\n\n\n\n The multiplication operator is * (asterisk). Here we will multiply 2 by 2:<\/p>\n\n\n\n
- name: Multiply 2 * 2\n debug:\n msg: The result is {{ 2 * 2 }}\n<\/pre>\n\n\n\nI hope you can guess the result. It is, of course, 4.<\/p>\n\n\n\n
TASK [Multiply 2 * 2] ********************************************************************************\nok: [localhost] => {\n \"msg\": \"The result is 4\"\n}<\/pre>\n\n\n\nFloor division in Ansible<\/h2>\n\n\n\n Floor division works similarly to division. The difference is that instead of returning a floating point number, it will return the highest possible whole integer. A simple example would be 5 \/\/ 2 = 2. In this example, 5 can only be divided by two twice without converting the result to a float.<\/p>\n\n\n\n
Let’s look at two examples in action. In these examples, we try to divide 11 by 5 and 18 by 5.<\/p>\n\n\n\n
- name: Floor division 11 \/\/ 5\n debug:\n msg: The result is {{ 11 \/\/ 5 }}\n\n - name: Floor division 18 \/\/ 5\n debug:\n msg: The result is {{ 18 \/\/ 5 }}\n<\/pre>\n\n\n\nIf we had used division for these two problems, we would have gotten 2.2 and 3.6, respectively. But, floor division will only return the highest whole integer possible. In our case, that would be 2 and 3:<\/p>\n\n\n\n
TASK [Floor division 11 \/\/ 5] ************************************************************************\nok: [localhost] => {\n \"msg\": \"The result is 2\"\n}\n\nTASK [Floor division 18 \/\/ 5] ************************************************************************\nok: [localhost] => {\n \"msg\": \"The result is 3\"\n}\n<\/pre>\n\n\n\nModulo in Ansible<\/h2>\n\n\n\n The modulo operator is %. A modulo operation gives you the remainder after a division. As an example, calculating 7 \/ 3 will not result in a whole number, but 6 \/ 3 will. 7 % 3 will give you the remainder of 1.<\/p>\n\n\n\n
Let’s try a couple of examples:<\/p>\n\n\n\n
- name: Modulo operation 11 % 5\n debug:\n msg: The result is {{ 11 % 5 }}\n - name: Modulo operation 19 % 5\n debug:\n msg: The result is {{ 19 % 5 }}<\/pre>\n\n\n\nIn the first example of 11 % 5, you can only divide 11 by five twice to get a whole number. This would be equal to 10 \/ 5. So you have a remainder of 11-10=1<\/p>\n\n\n\n
In the second example of 19 % 5, you can dive 19 by 5 three times to get a whole number. This would be equal to 15 \/ 5. Now you have a remainder of 19 – 15 = 4.<\/p>\n\n\n\n
TASK [Modulo operation 11 % 5] *************************************************\nok: [localhost] => {\n \"msg\": \"The result is 1\"\n}\n\nTASK [Modulo operation 19 % 5] *************************************************\nok: [localhost] => {\n \"msg\": \"The result is 4\"\n}\n\n<\/pre>\n\n\n\nExponentation in Ansible<\/h2>\n\n\n\n The last operator we should know is the exponent operator **. Also known as the “power of” operator. In mathematics, exponentiation is usually written as Xn<\/sup> . The operation of 5 to the power of 2 would be 52<\/sup>. This is not easy to write in a text editor when programming, so programming languages use simpler operators like ** for this purpose. 5 to the power of 2 can be written as 5**2 in Ansible.<\/p>\n\n\n\nLet’s try a couple of examples:<\/p>\n\n\n\n
- name: Exponentation 5 ** 2\n debug:\n msg: The result is {{ 5 ** 2 }}\n - name: Exponentation 2 ** 3\n debug:\n msg: The result is {{ 2 ** 3 }}\n<\/pre>\n\n\n\nThe first example of 52<\/sup> is written in Ansible as 5 ** 2. This is the equivalent of writing 5*5. This should result in 25.<\/p>\n\n\n\nThe second example of 23<\/sup> is written in Ansible as 2 ** 3. This is the equivalent of writing 2 * 2 * 2. This should result in 8.<\/p>\n\n\n\nHere is the output of these two operations:<\/p>\n\n\n\n
TASK [Exponentation 5 ** 2] ****************************************************\nok: [localhost] => {\n \"msg\": \"The result is 25\"\n}\n\nTASK [Exponentation 2 ** 3] ****************************************************\nok: [localhost] => {\n \"msg\": \"The result is 8\"\n}\n\n<\/pre>\n\n\n\nPractical examples of arithmetic in Ansible<\/h2>\n\n\n\n Now we have gone over simple examples of doing math in Ansible. But, of course, Ansible is not just a simple calculator. Now we should try to put our newfound knowledge to good use and do something useful.<\/p>\n\n\n\n
Count VCPU cores and multiply them to get thread count<\/h2>\n\n\n\n Let’s imagine we have a fictional service we want to run two threads of for every VCPU our host has. Our fictional service has a configuration file that has an argument for how many threads it should run. We start by defining a template for our service and place it in the templates directory (templates\/fictional.conf.j2)<\/p>\n\n\n\n
cores: {{ cores }}\nthreads: {{ threads }}<\/pre>\n\n\n\nWe can now create our playbook. To get the VCPU count, I have added “gather_facts: true” to the playbook. This way, we will be able to get the VCPUs for the processor_vcpus fact.<\/p>\n\n\n\n
---\n- name: Configure fictional service\n hosts: myhost\n remote_user: root\n gather_facts: true\n tasks:\n - name: How many vcpus do we have?\n debug:\n var: ansible_facts.processor_vcpus\n - name: Calculate the number of threads\n set_fact:\n cores: '{{ ansible_facts.processor_vcpus }}'\n threads: '{{ ansible_facts.processor_vcpus * 2 }}'\n - name: Write the configuration file\n template:\n src: fictional.conf.j2\n dest: \/etc\/fictional\/fictional.conf<\/pre>\n\n\n\nThe first task will print the number of VCPUs, so we can verify that it is correct. The next task will set the variables for us. Finally, we use the built-in template module to save our configuration file to the host.<\/p>\n\n\n\n
Here is the output from our playbook.<\/p>\n\n\n\n
PLAY [Configure fictional service] *******************************************************************\n\nTASK [Gathering Facts] *******************************************************************************\nok: [myhost]\n\nTASK [How many vcpus do we have?] ********************************************************************\nok: [myhost] => {\n \"ansible_facts.processor_vcpus\": \"12\"\n}\n\nTASK [Calculate the number of threads] ***************************************************************\nok: [myhost]\n\nTASK [Write the configuration file] ******************************************************************\nchanged: [myhost]\n\nPLAY RECAP *******************************************************************************************\nmyhost : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 <\/pre>\n\n\n\nThe output from the playbook run tells us that we have 12 VCPUs. That should mean that our thread count should be set to 12*2=24. Let’s check the contents of \/etc\/fictional\/fictional.conf:<\/p>\n\n\n\n
:~# cat \/etc\/fictional\/fictional.conf \ncores: 12\nthreads: 24<\/pre>\n\n\n\nYes! The file contains exactly what we expected.<\/p>\n\n\n\n
If we want to simplify our playbook, we can also do the calculations in the template file itself. This way, we can skip the set_fact task and access the processor_vcpu fact directly.<\/p>\n\n\n\n
Here is our new template:<\/p>\n\n\n\n
cores: {{ ansible_facts.processor_vcpus }}\nthreads: {{ ansible_facts.processor_vcpus * 2 }}\n<\/pre>\n\n\n\nSuppose we want one thread for every two VCPUs our host has. Obviously, we would need to do division instead of multiplication.<\/p>\n\n\n\n
Here I have defined a new template where I divide the number of VCPUs by 2 with the division operator, \/. I have also added one small change. At the end of the calculation, I added a filter to ensure we only get a whole integer. The calculation is also wrapped in parentheses to ensure Ansible knows we want the filter to apply to the result, not just the last integer.<\/p>\n\n\n\n
cores: {{ ansible_facts.processor_vcpus }}\nthreads: {{ (ansible_facts.processor_vcpus \/ 2)|int }}<\/pre>\n\n\n\nAs expected, our threads variable now contains 6:<\/p>\n\n\n\n
:~# cat \/etc\/fictional\/fictional.conf \ncores: 12\nthreads: 6<\/pre>\n\n\n\nDoing calculations within conditional arguments<\/h2>\n\n\n\n Let’s say we only want to configure our service when a host has more than 1024MB of memory for every VCPU. This is a totally fictional scenario. But it is not unreasonable to think a similar scenario could come up in the real world.<\/p>\n\n\n\n
To do this, we would add the “when<\/strong>” statement to our task. We also need to calculate how many MB of memory we have for every VCPU by dividing the amount of available memory by our VCPU count.<\/p>\n\n\n\nWe can retrieve the amount of memory the host has with the fact “memtotal_mb”. So the calculation should be memtotal_mb \/ processor_vcpus. After the calculation, we add >= 1024 to see if the result is greater than or equal to 1024.<\/p>\n\n\n\n
Here is the task with the “when<\/strong>” statement added.<\/p>\n\n\n\n - name: Write the configuration file\n template:\n src: fictional.conf.j2\n dest: \/etc\/fictional\/fictional.conf\n when: (ansible_facts.memtotal_mb \/ ansible_facts.processor_vcpus) > 1024\n<\/pre>\n\n\n\nNow we have tried all kinds of calculations and even tried to apply our new knowledge to some semi-practical examples. I hope you have found this tutorial helpful. Now it is up to you to find ways to use these examples in your own playbooks. The beauty of ansible is that there are endless ways you can use arithmetic operators in playbooks. You can customize all kinds of configurations with calculations made on the fly. Good luck!<\/p>\n","protected":false},"excerpt":{"rendered":"
Did you know that you can do mathematic calculations in Ansible? This can be very helpful…<\/p>\n","protected":false},"author":1,"featured_media":2013,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[40],"yoast_head":"\n
Arithmetic Operators in Ansible - Linux Digest<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n \n \n \n\t \n\t \n\t \n