Ansible 从命令行获取用户名 [英] Ansible get the username from the command line
问题描述
在我的剧本中,我经常引用用户名(特别是ubuntu").
In my playbooks I reference username (exclusively its "ubuntu") a lot.
是否有内置的方式说从命令行中传递的值中获取它"?
Is there a built in way to say "get it from the value passed in the command line"?
我知道我能做到
ansible-playbook <task> -u <user> -K --extra-vars "user=<user>"
然后我可以在 playbook 中使用 {{user}}
,但是定义用户两次感觉很奇怪.
and then I can use {{user}}
in the playbook, but it feels odd defining the user twice.
推荐答案
正如 Woodham 所说,代表连接用户的 ansible 变量是
As Woodham stated, the ansible variable that represents the connecting user is
{{ ansible_user }} (Ansible < 2.0 was {{ ansible_ssh_user }} )
但您不必在清单文件本身中定义它.
But you don't have to define it in the inventory file per se.
你可以在:
1.你的游戏,如果你使用 ansible-playbook:查看手册手册
- name: Some play
hosts: all
remote_user: ubuntu
2.在清单文件中:查看库存手册
[all]
other1.example.com ansible_user=ubuntu (Ansible < 2.0 was ansible_ssh_user)
3.正如您所说,在命令行上:
ansible-playbook -i inventory -u ubuntu playbook.yml
4.作为 remote_user
指令的 ansible 配置文件.查看配置文件手册
4. An ansible config file as a remote_user
directive.
See the manual on a config file
ansible 配置文件可以放在当前文件夹 ansible.cfg
、你的 homedir .ansible.cfg
或 /etc/ansible/ansbile.cfg代码>.
The ansible config file can be placed in the current folder ansible.cfg
, your homedir .ansible.cfg
or /etc/ansible/ansbile.cfg
.
[defaults]
remote_user=ubuntu
这篇关于Ansible 从命令行获取用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!