Introduction

Ansible, the powerful automation and orchestration tool, is continually evolving to enhance its capabilities and streamline automation processes. To maintain efficient and up-to-date playbooks, it’s crucial to adhere to best practices and follow recommended guidelines. Ansible Lint Rule 504, known as “deprecated-local-action,” addresses a practice that is no longer considered best practice in the Ansible ecosystem.

The Local Action Dilemma

In the realm of Ansible playbooks, local actions refer to tasks executed on the control node where Ansible is run. While there are scenarios where local actions are necessary, the Ansible community has shifted towards using a more structured and robust method for handling these tasks.

The problematic code that Ansible Rule 504 targets typically looks like this:

---
- name: Example of deprecated-local-action rule
  hosts: all
  tasks:
    - name: Task example
      local_action: # <-- this is deprecated
        module: ansible.builtin.debug

In this code snippet, the local_action key is used to specify a task to run on the local control node. While this approach can work, it is considered deprecated, and newer Ansible practices recommend a more explicit and structured approach.

Join 50+ hours of courses in our exclusive community

To address the deprecation of local actions, Ansible Lint Rule 504 recommends using the delegate_to: localhost option. This alternative approach is more explicit, aligning with the current best practices in Ansible playbook development. Here is the correct way to structure the code:

---
- name: Example of deprecated-local-action rule
  hosts: all
  tasks:
    - name: Task example
      ansible.builtin.debug:
      delegate_to: localhost # <-- recommended way to run on localhost

Output:

WARNING  Listing 1 violation(s) that are fatal
deprecated-local-action: Do not use 'local_action', use 'delegate_to: localhost'.
504.yml:5 Task/Handler: Task example

Read documentation for instructions on how to ignore specific rule violations.

                   Rule Violation Summary                   
 count tag                     profile rule associated tags 
     1 deprecated-local-action basic   deprecations         

Failed: 1 failure(s), 0 warning(s) on 1 files. Last profile that met the validation criteria was 'min'.

By using delegate_to: localhost, you ensure that the task is explicitly executed on the control node. This approach is not only more in line with modern Ansible standards but also enhances the clarity and maintainability of your playbooks.

Benefits of Using delegate_to: localhost

There are several compelling reasons for adopting the delegate_to: localhost approach:

  1. Alignment with Best Practices: Following recommended practices in Ansible playbook development ensures that your code remains relevant and maintainable as Ansible evolves.

  2. Improved Readability: Using delegate_to: localhost provides greater clarity in your playbooks, making it easier for you and your team to understand the intended execution of tasks.

  3. Maintainable Code: Code that adheres to current best practices is more likely to be maintained effectively over time, even as the Ansible ecosystem evolves.

Conclusion

In the ever-evolving landscape of Ansible automation, it’s essential to stay updated with the latest best practices. Ansible Lint Rule 504, “deprecated-local-action,” underscores the importance of adopting the delegate_to: localhost approach over deprecated local actions. By following this recommendation, you ensure that your Ansible playbooks remain aligned with the latest standards and maintainable for the long term. Your automation processes will benefit from improved clarity, readability, and adherence to best practices, resulting in more efficient and robust playbook execution.

Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my Udemy 300+ Lessons Video Course.

BUY the Complete Udemy 300+ Lessons Video Course

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Patreon Buy me a Pizza