【问题标题】:Ansible and ForwardAgent for sudo_usersudo_user 的 Ansible 和 ForwardAgent
【发布时间】:2019-03-07 06:13:55
【问题描述】:

有人能说我,我做错了什么吗?我正在使用 Amazon EC2 实例并希望将代理转发到用户 rails,但是当我运行下一个任务时:

- acl: name={{ item }} etype=user entity=rails permissions=rwx state=present
  with_items:
    - "{{ ansible_env.SSH_AUTH_SOCK|dirname }}"
    - "{{ ansible_env.SSH_AUTH_SOCK }}"
  sudo: true

我看到失败的结果:

(item=/tmp/ssh-ULvzaZpq2U) => {"failed": true, "item": "/tmp/ssh-ULvzaZpq2U"}
msg: path not found or not accessible!

当我在没有 ansible 的情况下手动尝试时,它看起来不错:

setfacl -m rails:rwx "$SSH_AUTH_SOCK"
setfacl -m rails:x $(dirname "$SSH_AUTH_SOCK")
sudo -u rails ssh -T git@github.com //Hi KELiON! You've successfully authenticated, but GitHub does not provide shell access.

我什至尝试运行新实例并运行 test ansible playbook:

#!/usr/bin/env ansible-playbook
---
- hosts: all
  remote_user: ubuntu
  tasks:
    - user: name=rails
      sudo: true
    - name: Add ssh agent line to sudoers
      lineinfile:
        dest: /etc/sudoers
        state: present
        regexp: SSH_AUTH_SOCK
        line: Defaults env_keep += "SSH_AUTH_SOCK"
      sudo: true
    - acl: name={{ item }} etype=user entity=rails permissions=rwx state=present
      with_items:
        - "{{ ansible_env.SSH_AUTH_SOCK|dirname }}"
        - "{{ ansible_env.SSH_AUTH_SOCK }}"
      sudo: true
    - name: Test that git ssh connection is working.
      command: ssh -T git@github.com
      sudo: true
      sudo_user: rails

ansible.cfg 是:

[ssh_connection]
pipelining=True
ssh_args=-o ForwardAgent=yes -o ControlMaster=auto -o ControlPersist=60s

[defaults]
sudo_flags=-HE
hostfile=staging

但结果相同。有什么想法吗?

【问题讨论】:

    标签: ssh ansible


    【解决方案1】:

    我遇到了同样的问题,并在https://github.com/ansible/ansible/issues/7235#issuecomment-45842303找到了答案

    我的解决方案与他的有点不同,因为 acl 对我不起作用,所以我:

    1. 已更改 ansible.cfg:
    [默认值] sudo_flags=-HE [ssh_connection] # 注释掉:ssh_args = -o ForwardAgent=yes
    1. 添加的任务/ssh_agent_hack.yml 包含:
    - 名称:“(ssh-agent hack:授予对 {{ deploy_user }} 的访问权限)” # SSH-agent socket 只转发给当前用户(0700 文件)。让我们改变它 # 见:https://github.com/ansible/ansible/issues/7235#issuecomment-45842303 # 见:http://serverfault.com/questions/107187/ssh-agent-forwarding-and-sudo-to-another-user 变成:假 文件:组={{deploy_user}} 模式=g+rwx 路径={{item}} with_items: - “{{ ansible_env.SSH_AUTH_SOCK|目录名}}” - “{{ ansible_env.SSH_AUTH_SOCK }}”

    注意 - become: false 设置是因为我以 root 身份登录 - 如果您以其他身份登录,那么您需要成为 root 才能进行修复,然后在下面成为您的 deploy_user(如果它不是用户你正在 ssh'ing as)。

    1. 然后从我的 deploy.yml 剧本中调用它:
    - 主机:应用程序 收集事实:真 变成:真 成为用户:“{{部署用户}}” pre_tasks: - 包括:tasks/ssh_agent_hack.yml 标签: ['部署'] 角色: - {角色:carlosbuenosvinos.ansistrano-deploy,标签:['deploy']}

    旁注 - 在 ~/.ssh/config 中的主机条目中添加 ForwardAgent yes 并不会影响有效的方法(我尝试了所有 8 种组合:- 仅设置 sudo_flags 而不是 ssh_args 有效,但如果您设置则没关系在 ~/.ssh/config 中打开或关闭 opensssh 的转发 - 在 ubuntu trusty 下测试)

    另请注意:我在 ansible.cfg 中有 pipelining=True

    【讨论】:

    • 谢谢,这让我发疯了
    【解决方案2】:

    我知道这个答案迟到了,但是当我将我的解决方案提炼到最低限度时,其他答案似乎有点过于复杂。这是一个克隆 git repo 的示例剧本,需要通过 ssh 进行访问的身份验证:

    - hosts: all
      connection: ssh
      vars:
        # forward agent so access to git via ssh works
        ansible_ssh_extra_args: '-o ForwardAgent=yes'
        utils_repo: "git@git.example.com:devops/utils.git"
        utils_dir: "/opt/utils"
      tasks:
        - name: Install Utils
          git:
            repo: "{{ utils_repo }}"
            dest: "{{ utils_dir }}"
            update: true
            accept_hostkey: yes
          become: true
          become_method: sudo
          # Need this to ensure we have the SSH_AUTH_SOCK environment variable
          become_flags: '-HE'
    

    【讨论】:

      【解决方案3】:

      这在 ansible v2.3.0.0 中对我有用:

      $ vi ansible.cfg

      [defaults]
      roles_path = ./roles
      retry_files_enabled = False
      [ssh_connection]
      ssh_args=-o ControlMaster=auto -o ControlPersist=60s -o ControlPath=/tmp/ansible-ssh-%h-%p-%r -o ForwardAgent=yes
      

      $ vi roles/pull-code/tasks/main.yml

      - name: '(Hack: keep SSH forwarding socket)'
        lineinfile:
            dest: /etc/sudoers
            insertafter: '^#?\s*Defaults\s+env_keep\b'
            line: 'Defaults    env_keep += "SSH_AUTH_SOCK"'
      
      - name: '(Hack: grant access to the socket to {{app_user}})'
        become: false
        acl: name='{{item}}' etype=user entity='{{app_user}}' permissions="rwx" state=present
        with_items:
            - "{{ ansible_env.SSH_AUTH_SOCK|dirname }}"
            - "{{ ansible_env.SSH_AUTH_SOCK }}"
      
      - name: Pull the code
        become: true
        become_user: '{{app_user}}'
        git:
            repo: '{{repository}}'
            dest: '{{code_dest}}'
            accept_hostkey: yes 
      

      【讨论】:

        猜你喜欢
        • 2013-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-03
        • 2011-12-05
        • 2021-03-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多