问题原文
原帖链接 · errata · CC BY-SA 3.0
I am trying to run a specific Ansible task as a different user than the one who is running the playbook. My .yml file looks like this:
---
- hosts: staging_servers
tasks:
- name: check user
remote_user: someusername
shell: whoami
Running this task shows me that whoami command returns a different user than I defined in the task (precisely, returns the user which is defined in hosts file called ubuntu).
I also tried to define the task like this:
---
- hosts: staging_servers
tasks:
- name: check user
sudo: yes
sudo_user: someusername
shell: whoami
but then I get 'Missing sudo password' error, although there is a line in sudoers file which says someusername ALL=(ALL) NOPASSWD:ALL and issuing commands with sudo on remote machine as someusername doesn't ask me for a password.
So, how can I run the specific task as a different user which is not the user defined in hosts file or root himself?
社区高分解答
原帖链接 · ex-nerd · CC BY-SA 4.0
Note that after Ansible 1.9, the sudo wording was replaced with become, thus
sudo: yes
sudo_user: some_user
becomes (pun intended):
become: yes
become_user: some_user
See more specifics here: https://stackoverflow.com/a/22749788/402727
Also write this before the actual module (e.g. command or shell) you want to execute for it to take effect. At least in my experience it didn't work correctly if I have written become and become_user after the shell module.
- name: Example user change
become: true
become_user: '{{ user }}'
shell: |
...
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
