can roles and tasks exist in the same playbook?

后端 未结 4 1975
囚心锁ツ
囚心锁ツ 2021-02-12 12:48
---
# file: main.yml

- hosts: fotk
  remote_user: fakesudo
  tasks:
  - name: create a developer user
    user: name={{ user }}
          password={{ password }}
               


        
相关标签:
4条回答
  • 2021-02-12 13:04

    https://docs.ansible.com/playbooks_roles.html#roles

    If the play still has a ‘tasks’ section, those tasks are executed after roles are applied.

    If you wish to run tasks before or after roles are executed, you need to list these under pre_tasks and post_tasks. Thus, there is no way to run "loose" tasks between two roles. You might want to create a dedicated role for these tasks.

    0 讨论(0)
  • 2021-02-12 13:04

    Short follow up to the already mentioned options by quoting the latest Ansible docs docs.ansible.com/latest/playbooks_reuse_roles:

    As of Ansible 2.4, you can now use roles inline with any other tasks using import_role or include_role:

    ---    
    - hosts: webservers
      tasks:
        - debug:
          msg: "before we run our role"
        - import_role:
          name: example
        - include_role:
          name: example
        - debug:
          msg: "after we ran our role"`
    

    Code snippet is also from Ansible docs.

    Be aware of the difference between static (import*) & dynamic (include*) usage.

    0 讨论(0)
  • 2021-02-12 13:05

    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 }}" }
    
    0 讨论(0)
  • 2021-02-12 13:13

    You can also do pre_tasks: and post_tasks: if you need to do things before or after. From the Docs https://docs.ansible.com/playbooks_roles.html

    - hosts: localhost
    
      pre_tasks:
        - shell: echo 'hello in pre'
    
      roles:
        - { role: some_role }
    
      tasks:
        - shell: echo 'in tasks'
    
      post_tasks:
        - shell: echo 'goodbye in post'
    

    >

    Gives the output: PLAY [localhost]


    GATHERING FACTS *************************************************************** ok: [localhost]

    TASK: [shell echo 'hello in pre'] ********************************************* changed: [localhost]

    TASK: [some_role | shell echo 'hello from the role'] ************************** changed: [localhost]

    TASK: [shell echo 'in tasks'] ************************************************* changed: [localhost]

    TASK: [shell echo 'goodbye in post'] ****************************************** changed: [localhost]

    PLAY RECAP ******************************************************************** localhost : ok=5 changed=4 unreachable=0
    failed=0

    This is with ansible 1.9.1

    0 讨论(0)
提交回复
热议问题