Skip to content

Instantly share code, notes, and snippets.

@hasenbalg
Created January 19, 2020 10:36
Show Gist options
  • Save hasenbalg/c6cb097cd55d648a13814c1ff49bb717 to your computer and use it in GitHub Desktop.
Save hasenbalg/c6cb097cd55d648a13814c1ff49bb717 to your computer and use it in GitHub Desktop.
Ansible blockinline with item list
---
# without a marker for each iteration blockinlist will only write the last list entry in the file
# this example shares the directories huhu and haha in the /tmp dir via samba
- hosts: all
vars:
shared_folders:
- {name: "huhu", path: "/tmp/huhu"}
- {name: "haha", path: "/tmp/haha"}
tasks:
- name: check if shared folders exist
become: yes
file:
path: "{{ item.path }}"
mode: 0777
owner: nobody
group: nogroup
state: directory
with_items: "{{ shared_folders }}"
- name: configure samba shares
become: yes
blockinfile:
path: /etc/samba/smb.conf
# this is the important part
marker: "### config for {{ item.name }}"
block: |
[{{ item.name }}]
comment = About {{ item.name }}
browseable = yes
path = {{ item.path }}
guest ok = yes
read only = no
create mask = 0777
with_items: "{{ shared_folders }}"
@a-mcf
Copy link

a-mcf commented Mar 29, 2021

Thank you for this! I had to tweak it a bit to keep sections from being repeatedly added.

Instead of

marker: "### config for {{ item.name }}"

I had to add the `{mark}' variable like so:

marker: "### {mark} config for {{ item.name }}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment