我如何指定一个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**

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


当前回答

Ansible vault已经在这里被建议了几次,但我更喜欢git-crypt加密我的剧本中的敏感文件。如果你使用git来保存你的ansible playbook,这是一个snap。我发现ansible vault的问题是,我不可避免地会遇到我想要使用的文件的加密副本,在我工作之前必须解密它。git-crypt提供了一个更好的工作流程。

https://github.com/AGWA/git-crypt

使用这个,你可以把你的密码放在你的剧本的var中,并把你的剧本标记为一个加密文件。gitattributes,像这样:

 my_playbook.yml filter=git-crypt diff=git-crypt

你的剧本将在Github上透明加密。然后,您只需要在用于运行ansible的主机上安装加密密钥,或者按照文档上的说明使用gpg进行设置。

这里有一个关于像SSH -agent转发SSH密钥一样转发gpg密钥的很好的问答:https://superuser.com/questions/161973/how-can-i-forward-a-gpg-key-via-ssh-agent。

其他回答

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

[whatever]
some-host ansible_sudo_pass='foobar'

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

你可以像这样在hosts文件中为你的剧本写sudo密码:

[host-group-name]
host-name:port ansible_sudo_pass='*your-sudo-password*'

我的黑客自动化这是使用一个环境变量,并通过——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).

我不认为ansible会让你在标志中指定密码,因为你希望这样做。 可能在配置的某个地方可以设置这个,但这将使使用ansible整体不太安全,不建议使用。

您可以做的一件事是在目标计算机上创建一个用户,并向其授予所有命令或受限制的命令列表的无密码sudo特权。

如果你运行sudo visudo并输入如下一行,那么用户'privilegedUser'在运行sudo service xxxx start时应该不需要输入密码:

%privilegedUser ALL= NOPASSWD: /usr/bin/service

我在这个问题上撕了我的头发,现在我找到了一个解决方案,这是我想要的:

每台主机1个加密文件,包含sudo密码

/etc/ansible/hosts:

[all:vars]
ansible_ssh_connection=ssh ansible_ssh_user=myuser ansible_ssh_private_key_file=~/.ssh/id_rsa

[some_service_group]
node-0
node-1

然后为每个主机创建一个加密的var文件,如下所示:

ansible-vault create /etc/ansible/host_vars/node-0

与内容

ansible_sudo_pass: "my_sudo_pass_for_host_node-0"

如何组织保险库密码(通过——ask-vault-pass输入)或CFG由您决定

基于此,我怀疑你可以加密整个hosts文件…