-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Explain use of pre_tasks #207
Comments
@gaddman - Good point; if you don't know the context (especially how
|
This will be included in the next batch of book updates. |
To be honest I plan to deprecate pre/post tasks in ansible-lint as I see no use for them since include_role was created. I would be very happy if you would avoid using them in your book. Obsolete syntax. If you wonder why, think about: task, role, task, role,... not possible with old style. |
@ssbarnea - pre_ and post_ tasks are extremely useful, and I avoid using include_role as much as possible for two reasons:
|
As an illustration (pseudo-playbook): - hosts: all
pre_tasks:
- name: Update apt cache.
roles:
- accounts
- security
- webserver
- app versus: - hosts: all
tasks:
- name: Update apt cache.
- include_role: accounts
- include_role: security
- include_role: webserver
- include_role: app The playbook is shorter, but the layout is all compacted (which I don't particularly like), and now I have an extra 48 characters that make reading the playbook more annoying. |
I too like the pre_tasks/post_tasks but you can achieve the same "look" using loops: - hosts: all
tasks:
- name: Update apt cache.
- include_role:
name: "{{ item }}"
loop:
- accounts
- security
- webserver
- app This also gives back the less-compact view. You can also use the "name:" field in the include_roles to expand them for readability too: tasks:
- name: Update apt cache.
- include_role:
name: accounts
- include_role:
name: security
- include_role:
name: webserver
- include_role:
name: app Though I'm secretly hoping that @ssbarnea keeps the pre_/post_tasks |
Nobody is removing them, but they do not scale, and for sake of having two ways of writing code, I am inclined to discourage their use. The net is full of users confused about where to stick tasks between roles. Even if I succeed introducing a lint rule for that, it will be optional, so anyone could bypass it. |
@ssbarnea but for a case like mine (I have around 300 different playbooks in different projects that I have set up with ansible-lint), that means any of those playbooks would start failing CI tests and I'd need to add yet another rule exception (once each for all those repos) for something that is not discouraged in Ansible core, at least. And to @dglinder's point, loops increase complexity for someone new to a system a lot more than a list of roles. My book tries to start out slow/easy/approachable (the But still, I use |
Book version: 1.21
Chapter 4 introduces
pre_tasks
(page 63) with a brief explanation that they're run before the main set of tasks. It's not clear (at this point in the book anyway) how that's any different to just adding a task at the beginning of thetasks
section.Assuming the reasoning is covered later in the book, it would be useful to reference that section, or provide a simple explanation at this point of the book.
The text was updated successfully, but these errors were encountered: