本文介绍了访问 remote_user 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎有效,但它很脆弱吗?我希望将 files 命令中的所有者和组设置为 someguy.我希望能够使用 {{ remote_user }} 但这不起作用.

这是一个示例剧本,展示了我的意思.

---- 主机:foobarremote_user:某人任务:- 名称:配置/usr/src/foo文件:目标:/usr/src/foo状态:目录所有者:{{ ansible_ssh_user }}组:{{ ansible_ssh_user }}递归:是须藤:是的

这不起作用:

---- 主机:foobarremote_user:某人任务:- 名称:配置/usr/src/foo文件:目标:/usr/src/foo状态:目录所有者:{{ remote_user }}组:{{ remote_user }}递归:是须藤:是的

一个或多个未定义变量:'remote_user' 未定义

解决方案

还有 {{ ansible_ssh_user }},它包含远程用户名并且似乎可以毫无问题地访问.另见此答案.

This seems to work, but is it fragile? I want the owner and group in the files command to be set to someguy. I'd expect to be able to use {{ remote_user }} but that doesn't work.

This is an example playbook showing what I mean.

---
- hosts: foobar
  remote_user: someguy
  tasks:
    - name: configure /usr/src/foo
      file:
        dest: /usr/src/foo
        state: directory
        owner: {{ ansible_ssh_user }}
        group: {{ ansible_ssh_user }}
        recurse: yes
      sudo: yes

This doesn't work:

---
- hosts: foobar
  remote_user: someguy
  tasks:
    - name: configure /usr/src/foo
      file:
        dest: /usr/src/foo
        state: directory
        owner: {{ remote_user }}
        group: {{ remote_user }}
        recurse: yes
      sudo: yes
解决方案

There's also {{ ansible_ssh_user }}, which contains remote user name and seems to be accessible without issues. See also this answer.

这篇关于访问 remote_user 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 14:48