0
votes

I'm having trouble making the following work

Pillar:


    rabbitmq:
      version: '3.5.6'
      users:
        tmpchq_user:
          password: RvdeXurQ
      vhosts:
        tmpchq:
          - user: tmpchq_user
            permission: '".*" ".*" ".*"'
          - user: default_user
            permissions: '".*" ".*" ".*"'
        default:
          - user: temp
            permission: '".*" ".*" ".*"'

State:


    {% for vhost in salt['pillar.get']('rabbitmq:vhosts') %}
      {% for items in salt['pillar.get']('rabbitmq:vhosts:{{ vhost }}') %}
    config_rabbitmq:
      cmd.run:
        - name: |
            rabbitmqctl add_vhost {{ vhost }} user {{ items['user'] }} permissions {{ items['permissions'] }}
        - cwd: /
        - shell: /bin/bash
      {% endfor %}
    {% endfor %}

Now if I work this out in python it works fine:


    >>> x = {'rabbitmq': {'users': {'tmpchq_user': {'password': 'RvdeXurQ'}},
    ...               'version': '3.5.6',
    ...               'vhosts': {'default': [{'permission': '".*" ".*" ".*"',
    ...                                       'user': 'temp'}],
    ...                          'tmpchq': [{'permission': '".*" ".*" ".*"',
    ...                                      'user': 'tmpchq_user'},
    ...                                     {'permission': '".*" ".*" ".*"',
    ...                                      'user': 'default_user'}]}}}
    >>> for vhost in x['rabbitmq']['vhosts']:
    ...     for items in x['rabbitmq']['vhosts'][vhost]:
    ...         print(vhost, items['user'], items['permission'])
    ...         
    ...     
    ... 
    default temp ".*" ".*" ".*"
    tmpchq tmpchq_user ".*" ".*" ".*"
    tmpchq default_user ".*" ".*" ".*"

And the funky part is I don't get a failed or succeed by salt. I get nothing at all:


    [root@salt-master srv]# salt 'client01p' state.sls rabbitmq.install_exp
    client01p:

    Summary for client01p
    -----------
    Succeeded: 0
    Failed:   0
    -----------
    Total states run:    0
    Total run time:  0.000 ms

any ideas?? Also ignore that this command uses just made up directives, I'm simply trying to get the nesting to work, this is just a fictitious example.

1

1 Answers

0
votes
{% for items in salt['pillar.get']('rabbitmq:vhosts:{{ vhost }}') %}

This is wrong. You don't need {{ }} inside {% %} blocks. Use jinja's ~ string concatenation operator instead:

{% for items in salt['pillar.get']('rabbitmq:vhosts:'~vhost) %}

Furthermore, you should be aware that if 'rabbitmq:vhosts:'~vhost is empty in the pillar, you'll get an error because you can't iterate over None.

Also, I'd write your nested for loop like this instead:

{% for vhost, items in salt['pillar.get']('rabbitmq:vhosts', {}).items() %}
  {% for item in items %}