1

I am trying to execute a salt state depending on the IP first digit.

{% if grains['fqdn_ip4'].startswith('10') %}
task
{% endif %}

I get :

failed: Jinja variable 'list object' has no attribute 'startswith'

My syntax seems to be good, if I replace the grain item fqdn_ip4 with id, it works :

{% if grains['id'].startswith('10') %}
task
{% endif %}

The format seems different though :

salt-call grains.get id

returns

local:
admin.local

And

salt-call grains.get fqdn_ip4

returns

local:
- 10.25.64.33

So I have a "-" in front of fqdn_ip4 value.

Thanks in advance,

B.

1 Answer 1

3

As per the error and grains.get output, we can see that the fqdn_ip4 grain is a list/array. On machines with more than 1 IP address this grain will contain multiple elements.

Example:

minion:
  - 127.0.0.1
  - 1.2.3.4
  - 5.6.7.8

So if you want to match condition with the first element of the list, you have to use grains['fqdn_ip4'][0]. Like below:

{% if grains['fqdn_ip4'][0].startswith('10') %}
show-ip:
  module.run:
    - name: test.echo
    - text: grains['fqdn_ip4'][0]
{% endif %}
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.