我如何指定一个sudo密码Ansible在非交互的方式?

我是这样运行Ansible剧本的:

$ ansible-playbook playbook.yml -i inventory.ini \
    --user=username --ask-sudo-pass

但我想这样运行它:

$ ansible-playbook playbook.yml -i inventory.ini \
    --user=username` **--sudo-pass=12345**

有办法吗?我希望尽可能地自动化我的项目部署。


当前回答

五年过去了,我可以看出这仍然是一个非常相关的主题。有点像leucos的答案,我发现在我的情况下,只使用ansible工具(没有任何集中的身份验证,令牌或其他东西)。这假设您在所有服务器上都有相同的用户名和相同的公钥。如果你不这样做,当然你需要更具体,并在主机旁边添加相应的变量:

[all:vars]
ansible_ssh_user=ansible
ansible_ssh_private_key_file=home/user/.ssh/mykey
[group]
192.168.0.50 ansible_sudo_pass='{{ myserver_sudo }}'

ansible-vault create mypasswd.yml
ansible-vault edit mypasswd.yml

Add:

myserver_sudo: mysecretpassword

然后:

ansible-playbook -i inv.ini my_role.yml --ask-vault --extra-vars '@passwd.yml'

至少这样你就不用写更多指向密码的变量了。

其他回答

使用ansible 2.4.1.0和以下应该工作:

[all]
17.26.131.10
17.26.131.11
17.26.131.12
17.26.131.13
17.26.131.14

[all:vars]
ansible_connection=ssh
ansible_user=per
ansible_ssh_pass=per
ansible_sudo_pass=per

然后用这个清单运行剧本:

ansible-playbook -i inventory copyTest.yml

只是一个补充,所以没有人会经历我最近遇到的烦恼:

我相信,最好的解决方案是沿着上面的toast38coza的大致路线。如果将密码文件和剧本静态地绑定在一起是有意义的,那么使用vars_files(或include_vars)遵循他的模板。如果你想让它们分开,你可以在命令行上提供仓库内容,如下所示:

ansible-playbook --ask-vault-pass -e@<PATH_TO_VAULT_FILE> <PLAYBOOK_FILE>

回想起来,这是显而易见的,但这里有一些陷阱:

那个该死的@符号。如果省略它,解析将无声地失败,ansible-playbook将继续进行,就好像您从未指定过该文件一样。 您必须显式地导入保险库的内容,可以使用命令行——extra-vars/-e,也可以在YAML代码中导入。——ask-vault-pass标志本身不做任何事情(除了提示您输入一个稍后可能使用或不使用的值)。

愿你把“@”也写进去,节省一个小时。

这对我很管用…… 创建文件/etc/sudoers.d/90-init-users文件

echo "user ALL=(ALL)       NOPASSWD:ALL" > 90-init-users

其中“user”是您的用户id。

查看代码(runner/__init__.py),我认为你可以将其设置在你的库存文件中:

[whatever]
some-host ansible_sudo_pass='foobar'

ansible.cfg配置文件中似乎也有一些规定,但现在还没有实现(constants.py)。

我的黑客自动化这是使用一个环境变量,并通过——extralvars =“ansible_become_pass='{{lookup('env', ' ansible_become_pass ')}}'”访问它。

导出一个env变量,但避免bash/shell历史记录(前面有一个空格或其他方法)。例如:

     export ANSIBLE_BECOME_PASS='<your password>'

查找env变量,同时将额外的ansible_become_pass变量传递到ansible-playbook中,例如:

ansible-playbook playbook.yml -i inventories/dev/hosts.yml -u user --extra-vars="ansible_become_pass='{{ lookup('env', 'ANSIBLE_BECOME_PASS') }}'"

好的替代答案:

@toast38coza: simply use a vaulted value for ansible_become_pass. This is decent. However, for the paranoid teams that need to share ansible vault passwords, and execute ansible plays with induvidual accounts, they coudld use the shared vault password to reverse each others operating system password (identiy theft). Arguably, you need to trust your own team? @slm's bash subshell output generated to temp file descriptor and using the @ prefix to read the ansible variable from the file desriptor. Avoids bash history at least. Not sure, but hopefully subshell echo doesn't get caught and exposed in audit logging (e.g. auditd).