Strange failure when trying to delegate_to using a group member

I am trying to use a delegate_to in a play where the delegated-to host is the (only) member of a group, where the groupname is ‘slurm-prictlr’. The play looks like:

  • name: Ensure MUNGE key from Slurm primary controller is on this client
    synchronize:
    src: /etc/munge/munge.key
    dest: /etc/munge/munge.key
    delegate_to: $item
    with_items: “{{ groups.slurm-prictlr[0] }}”
    when: “‘slurm-prictlr’ not in group_names”

However, when I run the playbook with this play on a host, I’m getting:

fatal: [myhost]: FAILED! => {
“msg”: “‘prictlr’ is undefined”
}

Are hyphens in group names the problem? Or what else may be the problem here?

The problem is that you have used a hyphen in your group name. That does not meet the requirements for a valid python name, so you cannot use dot syntax.

In your current form, jinja2, thinks that you want to perform subtraction between variables at groups.slurm and prictlr[0].

You will need to instead use bracket syntax:

with_items: “{{ groups[‘slurm-prictlr’][0] }}”

With that being said, I’d really recommend renaming your groups to use underscores, instead of hyphens.

Advice taken, replaced hypens in group names with underscores, everything works now (after I also fixed the “$item” in the delegate_to to be the correct “{{ item }}” syntax)

Thanks for the quick help!