【问题标题】:Becoming non root user in ansible fails在ansible中成为非root用户失败
【发布时间】:2021-07-08 20:26:10
【问题描述】:

我正在尝试使用以下剧本在 ansible 中成为用户“oracle”:

- hosts: "myhost"
  tasks:         
        - name: install oracle client
          become: yes
          become_user: oracle
          become_method: su
          shell: |
                whoami
          args:
            chdir: /tmp/client
          environment:
            DISTRIB: /tmp/client

我收到一个错误:

"msg": "Failed to set permissions on the temporary files Ansible needs to create when becoming an unprivileged user (rc: 1, err: chown: changing ownership of `/tmp/ansible-tmp-1513617986.78-246171259298529/': Operation not permitted\nchown: changing ownership of `/tmp/ansible-tmp-1513617986.78-246171259298529/command.py': Operation not permitted\n}). For information on working around this, see https://docs.ansible.com/ansible/become.html#becoming-an-unprivileged-user"

我红了文章“https://docs.ansible.com/ansible/become.html#becoming-an-unprivileged-user

并将以下内容添加到 /etc/ansible/ansible.cfg 中,但没有任何效果。

allow_world_readable_tmpfiles = True

我的 Ansible 版本:

ansible 2.4.2.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]

问题: 有没有办法将我的主机配置为接受 ansible 成为 oracle 用户?

【问题讨论】:

    标签: ansible


    【解决方案1】:

    要允许成为非特权用户,必须在/etc/ansible/ansible.cfg 中将两件事设置为True

    重要: 必须在 ansible.cfg 的正确位置取消对给定设置的注释。 将这些设置附加到 ansible.cfg 是不够的。

    allow_world_readable_tmpfiles = True
    pipelining = True
    

    要以编程方式取消注释它们:

    sed -i 's/.*pipelining.*/pipelining = True/' /etc/ansible/ansible.cfg
    sed -i 's/.*allow_world_readable_tmpfiles.*/allow_world_readable_tmpfiles = True/' /etc/ansible/ansible.cfg
    

    这是一个示例剧本,它展示了如何成为用户oracle

    # Setup the infrastructure for Faktura
    - hosts: "myhost"
      become: yes
      become_method: sudo
      become_user: oracle
      vars:
        allow_world_readable_tmpfiles: true
      tasks:         
    
    
            # an error is thorwn when becoming unpriviledged user. Hence use sudo
            - name: install oracle client
              shell: |
                    whoami
              args:
                chdir: /tmp/client
              environment:
                DISTRIB: /tmp/client
    

    【讨论】:

    • @VladimirBotka 在大多数情况下可能是这样,but“流水线不适用于涉及文件传输的 python 模块(例如:复制、获取、模板)或非 python 模块。 "
    • (@leopold.talirz 你说得对。需要更新。) 引用自 2.10 Risks of becoming an unprivileged user: "Use pipelining. When pipelining is enabled, Ansible does not save the module to a temporary file on the client. Instead it pipes the module to the remote python interpreter’s stdin. Pipelining does not work for python modules involving file transfer (for example: copy, fetch, template), or for non-python modules."
    • 引用 2.10 [成为非特权用户的风险]:"... the file needs to be readable by the user Ansible is set to become. In this case, Ansible makes the module file world-readable for the duration of the Ansible module execution. ... If any of the parameters passed to the module are sensitive in nature, and you do not trust the client machines, then this is a potential danger." 结果是:如果流水线不起作用,请避免在向远程主机发送机密时成为非特权用户。
    【解决方案2】:

    从 ansible 2.10 开始,对临时文件的可读性进行了更细粒度的控制(并且不推荐使用全局 allow_world_readable_tmpfiles 变量)。

    例如为了使shell 模块具有世界可读性,您现在可以在主机级别设置变量ansible_shell_allow_world_readable_temp: true(使用ansible 2.10.5 为我工作)。

    截至 2021 年 2 月,文档似乎仍然有些缺乏;见https://github.com/ansible/ansible/issues/72264

    【讨论】:

    • 我一直在手头的任务之前将其设置为set_fact,然后立即取消设置。似乎是在低风险环境中尽量减少接触的最佳方式。
    【解决方案3】:

    如果您使用的是 Ubuntu 20.04 或更高版本,则需要安装 acl 软件包。

    来源:https://github.com/georchestra/ansible/issues/55#issuecomment-651043423

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 2021-05-28
      • 2020-01-26
      • 2017-02-21
      • 1970-01-01
      • 1970-01-01
      • 2020-07-19
      相关资源
      最近更新 更多