问题
How can I implement a condition bases on the fact that the remote-host a group called "foo" or not.
My use case: If there is a group called "foo" on the remote-host, then I need to add a user to it. If the group does not exist, then nothing needs to be done.
Is this possible with SaltStack?
回答1:
You need Jinja renderers in your salt states file and mix with salt.states.user.present. (Update) . you can use an salt override modules pw_group
{% if salt['group.info']("foo") %}
add new user if foo group found:
user.present:
- name: foouser
{% endif %}
There is more override modules that you may use.
回答2:
If you want to create the user on the system and add the user to certain groups only if the groups exist, the user.present
state has an optional_groups
option that will do that.
https://docs.saltstack.com/en/latest/ref/states/all/salt.states.user.html#salt.states.user.present
来源:https://stackoverflow.com/questions/37298827/saltstack-do-if-group-foo-exists-on-remote-host