40

问题很简单:(ansible_useransible_ssh_user)和remote_userAnsible 之间有什么区别,除了第一个是在配置文件中设置的,而后一个是在角色/角色中设置的?它们与-u/--user命令行选项有什么关系?

4

2 回答 2

51

他们似乎都一样。看看这里:

https://github.com/ansible/ansible/blob/c600ab81ee/lib/ansible/playbook/play_context.py#L46-L55

# the magic variable mapping dictionary below is used to translate
# host/inventory variables to fields in the PlayContext
# object. The dictionary values are tuples, to account for aliases
# in variable names.

MAGIC_VARIABLE_MAPPING = dict(
   connection       = ('ansible_connection',),
   remote_addr      = ('ansible_ssh_host', 'ansible_host'),
   remote_user      = ('ansible_ssh_user', 'ansible_user'),
   port             = ('ansible_ssh_port', 'ansible_port'),

此外,ansible_user当我们想在 ansible hosts 文件中指定默认 SSH 用户remote_user时使用,而 as 在playbook上下文中使用。

来自https://github.com/ansible/ansible/blob/c600ab81ee/docsite/rst/intro_inventory.rst

ansible_user 要使用的默认 ssh 用户名。

ansible_user这是在ansible hosts文件中使用的示例:

[targets]

localhost              ansible_connection=local
other1.example.com     ansible_connection=ssh        ansible_user=mpdehaan
other2.example.com     ansible_connection=ssh        ansible_user=mdehaan
于 2016-04-17T14:28:44.650 回答
5

remote_user 和 ansible_user 之间的一个区别:
当您从剧本中运行具有不同用户的角色时,例如:

- name: Apply user configuration to user root 
  hosts: all
  remote_user: root
- name: Apply user configuration to user murphy
  hosts: all
  remote_user: murphy

然后,您可以使用“when:ansible_user == ..”而不是“when:remote_user == ..”为不同的用户执行条件任务。例如:

- name: Add user murphy to wheel group
  user:
    name: murphy
    groups: wheel
    append: yes
  when: ansible_user == "root"
于 2018-11-12T10:18:44.340 回答