在ansible剧本中用户vs sudo vs sudo_user

时间:2013-11-24 16:32:57

标签: sudo ansible

我已经阅读了Ansible文档,但我仍然对ansible playbooks中的以下三个参数感到困惑:user,sudo,sudo_user。

我尝试了以下不同组合参数的剧本:

  1. user:deploy =>作品

  2. user:deploy and sudo:True => 挂起git任务

  3. user:deploy,sudo:True和sudo_user:deploy =>作品

  4. sudo_user实际上做了什么? 何时以及为什么要使用这些组合?

    - hosts: all
      user: deploy
      sudo: True
      sudo_user: deploy
    
      tasks:
          - name: Ensure code directory
            file: dest=/home/deploy/code state=directory
    
          - name: Deploy app
            git: repo=git@bitbucket.org:YAmikep/djangotutorial.git dest=/home/deploy/code
    

    由于

1 个答案:

答案 0 :(得分:70)

  • user是您正在使用的用户。使用您的配置,您可以使用deploy

  • sudo_user是您在设置sudo: yes时在主机上投放的用户。

所以我认为在你的情况下,如果你可以作为sudo ssh,则sudo_userdeploy都不是必需的。

但是,如果以root身份ssh,则需要设置 sudo_user: deploysudo: yes

如果您要求'sudo'但未指定任何用户,Ansible将使用您~/.ansible.cfgsudo_user)中的默认设置,默认为root。< / p>

请注意,user已弃用(因为它令人困惑)。您应该使用remote_user代替。

编辑:案例#2可能由于ssh确认问题而挂起:您可能在~deploy/.ssh/known_hosts中有bitbucket.org主机密钥,但在~root/.ssh/known_hosts

更新:从Ansible 2.x开始,使用becomebecome_user代替弃用的sudosudo_user。用法示例:

- hosts: all
  user: deploy
  become: true
  become_user: deploy

  tasks:
      - name: Ensure code directory
        file: dest=/home/deploy/code state=directory

      - name: Deploy app
        git: repo=git@bitbucket.org:YAmikep/djangotutorial.git dest=/home/deploy/cod