如何在 Ansible 中將使用者新增至多個群組?

如何在 Ansible 中將使用者新增至多個群組?

我無法將使用者新增至多個群組,我不斷收到訊息:此模組需要 key=value 參數。

這是我正在嘗試的程式碼片段:

- name: make a new user
  user: name=user
        state=present
        groups="group1", "group2", "group3"
        comment="comment"

文件說: Groups= 將使用者放入這個以逗號分隔的群組清單中。當設定為空字串 ('groups=') 時,將從除主要群組之外的所有群組中刪除使用者。

我嘗試過使用“group”、“group”並且不使用冒號,但仍然遇到相同的錯誤。

http://docs.ansible.com/user_module.html

答案1

正確的語法是:

- name: make a new user
  user: name=user
        state=present
        groups="group1, group2, group3"
        comment="comment"

答案2

您發布的程式碼有兩個問題:

  1. 若要將多個值傳遞給groups,請使用逗號分隔的值,中間不帶空格:groups: group1,group2
  2. 在 YAML 中,當您將每個按鍵放在自己的行上時,請交換=for:

這是工作程式碼的範例:

- name: make a new user
  user: 
    name: johnsmith
    state: present
    groups: group1,group2
    comment: "comment"
    append: no # If yes, will only add groups, not set them to just the list in groups.

答案3

我得到群組“group2”不存在。 (但如果沒有引號,那是為了顯示額外的空間)。

正確的方法是

groups={{ group }},{{ sudo_group }}

答案4

上面的答案都是不正確的。定義變數的正確方法:

groups: group1,group2 

然後使用:

action: user groups={{user.groups}}

相關內容