为有条件的模块设置参数

Setting Parameters for Modules With Conditions

假设您有一个服务器来宾用户列表,并且您希望确保他们只出现在不重要的主机组 playground

天真地我会那样做,但这是行不通的:

- name: adding guest users to playground servers
  user: >
      name="{{item.key}}"
      shell="{{item.value.shell}}"
      groups="{{item.value.groups}}"
      state="{{'present' if host in playground else 'absent'}}" #tricky part
  with_dict: guests

如何成功实现该目标?

列表group_names包含当前主机所属的所有组。 所以你的条件应该是这样的:

{{'present' if 'playground' in group_names else 'absent'}}