Accessing remote_user variable

人走茶凉 提交于 2019-12-04 23:56:05

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

When specifying variables in commands, you have to enclose them in quotes - for example:

  file: dest=/usr/src/foo state=directory owner={{ remote_user }} group={{ remote_user }} recurse=yes

should really be:

  file: dest=/usr/src/foo state=directory owner="{{ remote_user }}" group="{{ remote_user }}" recurse=yes

I'm surprised your first example works at all given the lack of quotes.

Additionally, your 'remote_user' doesn't actually have to be 'someguy' in that example as usually using sudo: yes is sufficient to have the file module set the specified owner and group correctly.

You can fix this by adding a vars: option

---
- hosts: foobar
  vars:
    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

You can try to write playbook like:

---
  - set_fact: remote_user="someguy"
  - hosts: foobar
    tasks:
      - name: configure /usr/src/foo
        file: dest=/usr/src/foo state=directory owner={{ remote_user }} group={{ remote_user }} recurse=yes
        sudo: yes
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!