Ansible remote_user vs ansible_user

时间:2016-04-16 19:33:38

标签: ansible ansible-playbook ansible-2.x

问题很简单:Ansible中ansible_user(前ansible_ssh_user)和remote_user之间有什么区别,除了第一个是配置文件,后者是在戏剧/角色中设置?它们如何与-u / --user命令行选项相关?

2 个答案:

答案 0 :(得分:24)

它们似乎都是一样的。看看这里:

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主机文件中指定默认SSH用户时使用ansible_user,其中 playbook 上下文中使用了remote_user

来自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

答案 1 :(得分:1)

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"