Ansible remote_user vs ansible_user

mad*_*ead 26 ansible ansible-playbook ansible-2.x

问题很简单:(ansible_useransible_ssh_user)和remote_userAnsible 之间的区别是什么,除了第一个是设置文件而后者设置为播放/角色?它们如何与-u/ --user命令行选项相关?

sla*_*fer 32

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

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'),
Run Code Online (Sandbox Code Playgroud)

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

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

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

这是一个ansible_useransible hosts文件中使用的例子:

[targets]

localhost              ansible_connection=local
other1.example.com     ansible_connection=ssh        ansible_user=mpdehaan
other2.example.com     ansible_connection=ssh        ansible_user=mdehaan
Run Code Online (Sandbox Code Playgroud)

  • 很遗憾他们如何为同一个选项使用不同的名称,以及ssh用户登录,这不是唯一的例子.从第一课开始,我们工作中的每个人都学会了永远不会"破坏向后兼容性"并且不自己思考,这样的事情只会堆积起来并且花费社区花费数小时来破解产品使用的浪费. (20认同)

小智 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
Run Code Online (Sandbox Code Playgroud)

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

- name: Add user murphy to wheel group
  user:
    name: murphy
    groups: wheel
    append: yes
  when: ansible_user == "root"
Run Code Online (Sandbox Code Playgroud)

  • 您显示的示例实际上使用不同的用户运行两个不同的游戏,而不是两个不同的角色。就我而言,目前不可能在同一个游戏中以不同的用户身份运行不同的角色。不过,这是一个功能请求:https://github.com/ansible/ansible/issues/5084 (3认同)