On Tue, 12 May 2020 13:42:00 -0700 (PDT)
Laci <
lac...@gmail.com> wrote:
> Thank you vlado, this worked for me:
>
> blockinfile:
> path: /conf.d/jobs.inc
> marker: " "
> block: |
> Job {
> Name = "{{ remote_shorthost }}"
> Client = "{{ remote_shorthost }}-fd"
> JobDefs = "DefaultJob"
> }
> backup: yes
You're welcome, Laci. It's necessary to add that the task is not idempotent.
It will repeatedly add the same block into the file. Quoting from
"blockinfile" parameter "marker":
https://docs.ansible.com/ansible/latest/modules/blockinfile_module.html#parameter-marker
"Using a custom marker without the {mark} variable may result in the block
being repeatedly inserted on subsequent playbook runs."
Best practice is to make the "marker" unique. For example
- blockinfile:
path: jobs.inc
marker: "# {mark} {{ remote_shorthost }}"
block: |
Job {
Name = "{{ remote_shorthost }}"
Client = "{{ remote_shorthost }}-fd"
JobDefs = "DefaultJob"
}
backup: yes
would create (if the variable "remote_shorthost: server3")
shell> cat jobs.inc
Job {
Name = "server1"
Client = "server1-fd"
JobDefs = "DefaultJob"
}
Job {
Name = "server2"
Client = "server2-fd"
JobDefs = "DefaultJob"
}
# BEGIN server3
Job {
Name = "server3"
Client = "server3-fd"
JobDefs = "DefaultJob"
}
# END server3
Such marker makes the task idempotent and also helps to create a loop, if
needed. For example
- blockinfile:
path: jobs.inc
marker: "# {mark} {{ item.remote_shorthost }}"
block: |
Job {
Name = "{{ item.remote_shorthost }}"
Client = "{{ item.remote_shorthost }}-fd"
JobDefs = "{{ item.jobdefs }}"
}
backup: yes
loop: "{{ my_servers }}"
vars:
my_servers:
- remote_shorthost: server3
jobdefs: DefaultJob
- remote_shorthost: server4
jobdefs: DefaultJob
Fit the first character(s) of the marker to the comment syntax of the language
the block shall be included in.
HTH,
-vlado