我需要以 fooser 的身份运行 command1。 command1 的路径环境在 .some_env 文件中设置。所以我需要先找到它。
- name: Run something
become_user: foouser
become_method: sudo
shell: . /path/to/dir/.some_env; command1
但是这个错误与
/bin/sh: /path/to/dir/.some_env: Permission denied
文件权限看起来不错:
-rwxr-x--- 1 foouser foogroup 2107 Mar 20 07:08 .some_env
我尝试了以下方法:
为什么说permission denied?
您可能有权限问题,获取文件不需要执行权限(x)。查看此剧本:
---
- hosts: localhost
connection: local
gather_facts: false
tasks:
- lineinfile:
path: /tmp/a
state: present
line: export n="hello there"
create: yes
- shell:
executable: /bin/bash
cmd: |
set -e
test -r /tmp
test -r /tmp/a
source /tmp/a
echo $n
register: _t
- debug:
var: _t.stdout
在
shell
任务中我检查:
这个剧本的输出是:
PLAY [localhost] *******************************************************************************************************************************************************************************
TASK [lineinfile] ******************************************************************************************************************************************************************************
changed: [localhost]
TASK [shell] ***********************************************************************************************************************************************************************************
changed: [localhost]
TASK [debug] ***********************************************************************************************************************************************************************************
ok: [localhost] => {
"_t.stdout": "hello there"
}
PLAY RECAP *************************************************************************************************************************************************************************************
localhost : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0