我如何指定一个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'
至少这样你就不用写更多指向密码的变量了。
我的黑客自动化这是使用一个环境变量,并通过——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).
如果您正在使用密码管理器,您可以使用模块passwordstore,这使得这非常容易。
假设您将用户的sudo密码保存为pass as
服务器 1/用户
然后您可以像这样使用解密后的值
{{ lookup('community.general.passwordstore', 'Server1/User')}}"
我在我的清单中使用它:
---
servers:
hosts:
server1:
ansible_become_pass: "{{ lookup('community.general.passwordstore', 'Server1/User')}}"
请注意,您应该运行gpg-agent,这样您就不会在每次运行“成为”任务时看到pinentry提示。