authorizedible和Ansible中的with_items

我试图创build新的用户,并使用Ansible填充他们的〜/ .ssh / authorized_keys文件。 这是我的任务:

- name: Create user account user: name="{{ item.username }}-ns" comment="{{ item.realname }}" groups=adm,sudo, append=yes password="{{ item.password }}" with_items: "{{ ssh_users }}" - name: copy ssh keys to authorized_keys authorized_key: user="{{ item.username }}-ns" key="{{ sshkey_path }}/{{ item.username }}.pub" with_item: "{{ ssh_users }}" 

和我的variables文件看起来像这样:

 ssh_users: - username: "jdoe" realname: "jrow" password: "$6$FWhXrnkizCqwKJcM$y55ETlvthHA49NuzwYgKAmOTnsBDRzfXE1OiOuJ.HHwVuI4P/BQrR/eKgYOioevIrgYYw.HpeP/sxCR3M38SW/" - username: "jroe" realname: "Jane Roe" password: "$6$wQhvxq3C.egKzrGi$na0M4jn3bi1lM2dz2YvdbAvvJBvbg4iGH1K6j7sHnZZt7mZggexHPvxOT799pfaDKmU6xDrbtbrLsxviGyABA0" - username: "testuser" realname: "Test User" password: "$6$U24oz4dsfdYD/LZ$fuziBEkc2q/POHSEvfcuTaD6wFTF.49RbU8z8JLQk3oki/He87cYqpSZtL16A11EBaG6VdemXdy6\V/" 

我已经将各种用户的公共ssh密钥设置到了一个名为“sshkey_path”的variables中的publickeys目录。 每个用户都有一个公钥文件(例如jdoe.pub)。

当我运行的剧本,用户帐户创build进展良好,但authorized_keys部分说:

 ERROR! 'with_item' is not a valid attribute for a Task The error appears to have been in 'user-add.yaml': line 29, column 7, but may be elsewhere in the file depending on the exact syntax problem. The offending line appears to be: - name: copy ssh keys to authorized_keys ^ here 

任何想法可能会出错? 原则上,它应该像在网上find类似的例子一样工作。 我已经玩过这个格式,但是无法运行。 感谢您的指点。

它失败的原因是因为实际的插件被称为with_items而不是with_item。 你忘记了。

为了后代:

除了“linuxdynasty”中关于with_items指出的错误with_items ,我在引用密钥文件的方式还有一个错误。

我试图使用下面的语法:

 key="{{ sshkey_path }}/{{ item.username }}.pub" 

但在这种情况下,这是authroized_key因为authroized_key需要一个文件。 一个必须使用“with_files”或查找来获得这个。

为了使它在我的情况下工作,我不得不连接多个variables,这与我在这个StackOverflow后的帮助。 我提出的最终语法是:

 key="{{ lookup('file', sshkey_path+item.username+'.pub') }}" 

它工作正常。