在 ansible 中运行 shell 模块时权限被拒绝

问题描述 投票:0回答:1

我需要以 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

我尝试了以下方法:

  • 使用“来源”而不是 . - 没有帮助
  • 在 env 文件的顶部添加了一个 shebang - 没有帮助

为什么说permission denied?

shell ansible permission-denied
1个回答
0
投票

您可能有权限问题,获取文件不需要执行权限(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
© www.soinside.com 2019 - 2024. All rights reserved.