我已阅读 Ansible 文档,但对 ansible playbook 中的以下三个参数仍然有点困惑:user、sudo、sudo_user。
我尝试了以下具有不同参数组合的剧本:
user:deploy => 有效
user:deploy 和 sudo: True => 挂起 git 任务
user:deploy、sudo: True 和 sudo_user:deploy => 有效
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
谢谢
最佳答案
user
是您进行 ssh 操作的用户。根据您的配置,您可以 ssh 作为deploy
.sudo_user
是当sudo: yes
时您在主机上 sudo 的用户已设置。
所以我认为在你的情况下没有 sudo
和sudo_user
如果您可以 ssh 作为 deploy
,则这是必要的.
但是,如果您以 root 身份进行 ssh,则需要设置
sudo_user: deploy
和sudo: yes
.
如果您要求“sudo”但未指定任何用户,Ansible 将使用 ~/.ansible.cfg
中的默认设置( sudo_user
),默认为 root
.
请注意user
已被弃用(因为它令人困惑)。您应该使用remote_user
相反。
编辑:情况 #2 可能因 ssh 确认问题而挂起:您可能在 ~deploy/.ssh/known_hosts
中有 bitbucket.org 主机 key 但不在 ~root/.ssh/known_hosts
更新:从 Ansible 2.x 开始,使用 become
和become_user
而不是已弃用的 sudo
和sudo_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
关于sudo - ansible playbooks 中的 user vs sudo vs sudo_user,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20176957/