且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

角色和任务可以存在于同一个剧本中吗?

更新时间:2023-01-22 11:23:26

实际上这应该是可能的,我记得我在测试期间做过几次.可能与您的版本有关 - 或者顺序很重要,因此任务将在角色之后执行.

Actually this should be possible and I remember I did this a few times during testing. Might be something with your version - or the order does matter, so that the tasks will be executed after the roles.

我会将此作为评论而不是答案发布,但我无法在评论中提供以下示例:

I would have posted this as a comment, rather than an answer, but I wouldn't be able to give the following example in a comment:

无论您的任务未执行的原因是什么,您始终可以将您的剧本分成多个剧本,如下所示:

Whatever might be the reason why your task is not executed, you can always separate your playbook into several plays, like so:

---
# file: main.yml

- hosts: fotk
  remote_user: fakesudo
  tasks:
  - name: create a developer user
    user: name={{ user }}
          password={{ password }}
          shell=/bin/bash
          generate_ssh_key=yes
          state=present

- hosts: fotk
  remote_user: fakesudo
  roles:
  - { role: create_developer_environment, sudo_user: "{{ user }}" }
  - { role: vim, sudo_user: "{{ user }}" }