1

I'm trying to concatenate strings in a state, and I'm not having much luck. I've seen the posts that suggest using (|join), but all my strings are not in a single dictionary. Here's my code:

sshd_content:
  file.line:
{% set admin_groups = '' %}
{% for app in grains['application_groups'] %}
{% for group in pillar['admin_users'][app]['members'] %}
{% set admin_groups = admin_groups ~ ' ' ~ group ~ '@mydomain.com' %}
{% endfor %}
{% endfor %}
    - name: /etc/ssh/sshd_config
    - match: AllowGroups wheel fred
    - mode: replace
    - content: AllowGroups wheel fred bob {{ admin_groups }}

I've tried using + instead of ~ without luck, too.

What am I doing wrong?

This state works fine:

sudoers_asmgroups_content:
  file.append:
    - name: /etc/sudoers.d/mygroups
    - text:
{% for app in grains['application_groups'] %}
  {% for group in pillar['admin_users'][app]['members'] %}
      - '%{{ group }}@mydomain.com ALL=(ALL) ALL'
  {% endfor %}
{% endfor %}
dahrens
  • 3,879
  • 1
  • 20
  • 38
michmill
  • 73
  • 1
  • 2
  • 6
  • How does the result look like? Any errors in the master/minion logs? – dahrens Jan 22 '18 at 20:01
  • There aren't any errors in /var/log/salt/minion on the target, nor in /var/log/salt/master on the master. {{ admin_groups }} is empty. – michmill Jan 22 '18 at 21:18
  • What's interesting, though, is the state immediately above this one works fine. ` sudoers_asmgroups_content: file.append: - name: /etc/sudoers.d/mygroups - text: {% for app in grains['application_groups'] %} {% for group in pillar['admin_users'][app]['members'] %} - '%{{ group }}@mydomain.com ALL=(ALL) ALL' {% endfor %} {% endfor %} ` – michmill Jan 22 '18 at 21:19
  • I've added it to your question, is the formatting of the added snippet the same as in your state? – dahrens Jan 22 '18 at 21:38

1 Answers1

1

I found a viable solution by modifying the solution here.

It appears to be a scoping issue with the admin_groups variable. Not sure why append works, but I'm not going to argue.

For the example in the OP above, here is the code:

sshd_content:
  file.line:
{% set admin_groups = [] %}
{% for app in grains['application_groups'] %}
{% for group in pillar['admin_users'][app]['members'] %}
{% do admin_groups.append(group) %}
{% endfor %}
{% endfor %}
    - name: /etc/ssh/sshd_config
    - match: AllowGroups wheel myadmin
    - mode: replace
    - content: AllowGroups wheel fred bob {{ admin_groups|join('@mydomain.com ') }}@mydomain.com 
{% endif %}

Need to add the second @domain.com since the items are AD group names, and join only adds the separator when there is another value.

michmill
  • 73
  • 1
  • 2
  • 6
  • Can't you just skip whole the loop in this case and just do `{{ pillar['admin_users'][app]['members']|join('@mydomain.com ') }}@mydomain.com` – dahrens Jan 22 '18 at 22:18