首页 > 解决方案 > ansible:以不同的用户身份运行角色

问题描述

我有两个角色在剧本中运行,一个创建用户,另一个运行一系列任务,我想在服务器上作为新创建的用户运行这些任务。

剧本是这样写的:

- hosts: centos
  connection: ssh
  gather_facts: false
  become: true

  roles:
  - ../roles/user_setup
  - ../roles/tasks

假设从user_setup角色创建的用户被称为user1:我基本上希望角色命名tasks为运行user1。最好的方法是什么?谢谢。

标签: ansibledevops

解决方案


这个问题几乎为您提供了解决方案。你可以使用类似的东西:

- hosts: centos
  connection: ssh
  gather_facts: false

  roles:
  - role: ../roles/user_setup
    become: true

  - role: ../roles/tasks
    become: true
    become_user: user1

如果您想直接连接user1(而不是升级到它),您可以将最新的角色调用替换为:

  - role: ../roles/tasks
    become: false
    remote_user: user1

推荐阅读