Skip to content

Instantly share code, notes, and snippets.

@OlegGorj
Created January 13, 2018 18:51
Show Gist options
  • Save OlegGorj/5358b811a54b8c21dc660b6c01b38585 to your computer and use it in GitHub Desktop.
Save OlegGorj/5358b811a54b8c21dc660b6c01b38585 to your computer and use it in GitHub Desktop.
Clarification around user vs sudo vs sudo_user in ansible playbooks

Given the follwoing playbook

- 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:someuser/sometutorial.git dest=/home/deploy/code

Issue occurse when use user:deploy and sudo:true it hangs on the git task

Solution

  • user is the user you're ssh'ing as. With your config, you're ssh'ing as deploy.

  • sudo_user is the user you're sudo'ing on the host when sudo: yes is set.

So I think in this case none of sudo and sudo_user are necessary if ssh as deploy.

However, if ssh as root, we need to set sudo_user: deploy and sudo: yes .

If we ask for 'sudo' but don't specify any user, Ansible will use the default set in your ~/.ansible.cfg (sudo_user), and will default to root.

Note that user is deprecated (because it's confusing). we should use remote_user instead.

Git step probably hangs because of ssh confirmation issues : we have bitbucket.org host key in ~deploy/.ssh/known_hosts but NOT in ~root/.ssh/known_hosts

UPDATE: As of Ansible 2.x, use become and become_user instead of the deprecated sudo and sudo_user.

Example usage:

- 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:someuser/sometutorial.git dest=/home/deploy/code
        
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment