Legal Ansible Syntax/Structure?

Hey All,

Is the following structure ansible-legal? If not, what structure should I use to make it ansible-legal?

- block:
  - name: Create The Base SQL Mount Point
    ansible.builtin.file:
      path: "/database"
      owner: root
      group: database_accounts
      mode: "0770"
      state: directory

  - name: Create The {{item}} Mount Point
    ansible.builtin.file:
      path: "/database/{{ansible_hostname}}_{{item}}"
      owner: root
      group: database_accounts
      mode: "0770"
      state: directory

  - name: Copy The {{item}} Mount Point File
    ansible.builtin.template:
      src: etc.systemd.system.database.mount.j2
      dest: /etc/systemd/system/database-{{ansible_hostname}}_{{item}}.mount
      owner: root
      group: root
      mode: "0644"
  when: ansible_hostname == groups.database_{{item}}
  notify: Reload The SystemD Daemon
  with_items:
    - firebird
    - mariadb
    - postgresql

What I am trying to achieve is to have a single “task/template set” which will create the appropriate mount point (FTR: on a shared Ceph file system share) depending upon which Inventory Group the server belongs to (eg):

Inventory File:

[database_firebird]
host1
testhost
[database_mariadb]
host3
testhost
[database_postgresql]
host2
testhost

Results:

host1:/database/host1_firebird
host2:/database/host2_postgresql
host3:/database/host3_mariadb
testhost:/database/testhost_firebird
testhost:/database/testhost_mariadb
testhost:/database/testhost_postgresql

This should (I hope) be clear from the above code, but… if its not, then I’m probbaly doing something wrong (which I am anyway because by block doesn’t work) :frowning:

Thanks in advance for the help

Cheers

Dulux_Oz

You cannot loop (with_items) over a block. Put the tasks from within the block into a separate file and loop over include_tasks including that file.

2 Likes

Thanks @mkrizek