要在Ansible中以不同用户身份运行delegate_to任务,您可以使用become
和become_user
选项来指定要切换到的用户。下面是一个示例代码:
- name: Run task on remote machine as different user
hosts: your_host
gather_facts: false
tasks:
- name: Delegate task to remote machine
command: whoami
delegate_to: your_remote_machine
become: true
become_user: your_user
在上面的示例中,通过设置delegate_to
选项指定了要在远程机器上运行任务的主机。然后,become
和become_user
选项分别用于切换到指定的用户。
您可以将your_host
替换为您要运行任务的主机的名称或组名,将your_remote_machine
替换为远程机器的名称或IP地址,将your_user
替换为要切换到的用户的名称。
注意:在运行此示例之前,请确保您具有足够的权限以切换到指定的用户。