Difference between become and become_user in Ansible

北城以北 提交于 2019-12-02 15:29:25

become_user defines the user which is being used for privilege escalation.

become simply is a flag to either activate or deactivate the same.

Here are three examples which should make it clear:

  1. This task will be executed as root, because root is the default user for privilege escalation:

    - do: something
      become: yes
    
  2. This task will be executed as user someone, because the user is explciitly set:

    - do: something
      become: yes
      become_user: someone
    
  3. This task will not do anything with become_user, because become is not set and defaults to false/no:

    - do: something
      become_user: someone
    

    ...unless become was set to true on a higher level, e.g. a block, the playbook, group or host-vars etc.

    Here is an example with a block:

    - become: yes
      block:
        - do: something
          become_user: someone
        - do: something
    

    The first 1st is ran as user someone, the 2nd as root.

As I understand it become_user is something similar to su , and become means something like sudo su or "perform all commands as a sudo user".

The default become_method is sudo, so sudo do something or sudo -u <become_user> do something

Fineprint: Of course "do: something" is pseudocode. Put you actual Ansible module there.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!