I'm trying to match conditions based on the third octet of an IP address since Windows minions don't report the gateway. I can get the grain info for the IP and match the full IP like below but I only want to match to the third octet:
{% set subnet = salt['grains.get']('ipv4:0') %}
user_coinfig:
cmd.run:
{% if subnet == '10.1.244.146' %}
- name: |
cmd.exe net localgroup administrators user /add
{% elif subnet == '10.1.245.146' %}
- name: |
cmd.exe net localgroup administrators user /add
{% endif %}
Desired state:
{% set subnet = salt['grains.get']('ipv4:0') %}
user_coinfig:
cmd.run:
{% if subnet == '10.1.244' %}
- name: |
cmd.exe net localgroup administrators user /add
{% elif subnet == '10.1.245' %}
- name: |
cmd.exe net localgroup administrators user /add
{% endif %}
Since Jinja is a templating language based on Python, you do have access to all of the string
methods of Python.
And for this case, you can use the method startswith()
.
So, you can do:
{% if subnet.startswith('10.1.244.') %}
- name: |
cmd.exe net localgroup administrators user /add
{% endif %}