问题描述
在我的剧本中,我经常引用用户名(完全是用户名"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>"
然后我可以在剧本中使用 {{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.
您可以在以下位置进行定义:
You can define it in:
1.您的剧本(如果您使用ansible-playbook):请参阅Playbooks手册
- 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配置文件可以放在当前文件夹 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从命令行获取用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!