Juliatzin
Juliatzin

Reputation: 19705

Yaml code repetition

I have several block of code repeated all over my code, like this:

 - name: Retrieve VM information
  os_server_facts:
    validate_certs: False ### From this line
    api_timeout: 300
    timeout: 600
    auth:
      auth_url: "{{ cloudstack_auth_url }}"
      username: "{{ cloudstack_login }}"
      password: "{{ cloudstack_password }}"
      project_name: "{{ cloudstack_project }}"
    auth_type: v2password ### To this line
    server: "{{ vm_hostname }}" 

Inside the same file, I could use anchors, but I don't know how to do to factorize this piece of code in differents files, any ideas ?

Upvotes: 2

Views: 361

Answers (1)

Tj Kellie
Tj Kellie

Reputation: 6476

If you have some tasks that are used in many places you can always include these in your playbook from a common .yml file using an include.

- include: ../common/tasks/mytasks.yml

However! Ansible really want's you to use roles for this type of common task use, I would consider putting these into a very simple role and using it in your plays with include_role. It's really a better and more salable way to do this.

- name: Include my tasks as a role
  include_role:
    name: reusedTasks
    tasks_from: simple_role

Upvotes: 1

Related Questions