Thanks Todd.

I will give a try option 1.

*(1) remove the various template/copy tasks from the various places in your
playbook and replace them with a single task at the end that creates the
single file, or* (2) store the output of the various tasks to separate
files and use `assemble` (or a shell script) at then end to put the
disparate pieces into one file.

On Fri, 17 Mar 2023, 21:05 Todd Lewis, <[email protected]> wrote:

> Right, but as you've discovered `copy` and `template` won't update part of
> a file.
> So your two choices are: (1) remove the various template/copy tasks from
> the various places in your playbook and replace them with a single task at
> the end that creates the single file, or (2) store the output of the
> various tasks to separate files and use `assemble` (or a shell script) at
> then end to put the disparate pieces into one file.
> If there are other options, they aren't obvious to me at the moment.
>
> On Friday, March 17, 2023 at 9:43:30 AM UTC-4 Aharonu wrote:
>
>> Greetings for the day!
>>
>> Looks like ansible.builtin.assemble for two different files from
>> directory to another required file.
>>
>> Sorry to raise my query again but..wanted to be clear.
>>
>> My requirement is,  *As i mentioned in previous mail*: playbook should
>> be having only one csv file but two different tasks using that.( not to
>> store 2 diff files and combine). It is to append one file from 2 tasks.
>>
>> *# cat /tmp/facts.csv*
>> somedata1   [coming from 'rescue' task
>> somedata2   [coming from 'rescue' task
>> somedata3   [ coming from 'always' task
>> somedata4   [coming from 'always' task
>>
>> Thank you,
>>
>> On Fri, 17 Mar 2023, 17:36 Todd Lewis, <[email protected]> wrote:
>>
>>> Consider
>>> https://docs.ansible.com/ansible/latest/collections/ansible/builtin/assemble_module.html
>>>
>>> It combines multiple files into one.
>>>
>>> On Friday, March 17, 2023 at 1:40:05 AM UTC-4 Aharonu wrote:
>>>
>>>>
>>>> I am digging into  ansible.builtin.copy and ansible.builtin.template 
>>>> *content *but not get options to append.
>>>>
>>>>  Can we append two different tasks data into one csv file instead of
>>>> over write?
>>>>
>>>> One task i have under '*rescue*' section as example below:
>>>>
>>>> rescue:
>>>>
>>>>
>>>> - name: Create failed_list CSV
>>>>   *ansible.builtin.copy:*
>>>>       *content: |*
>>>>
>>>>         failed
>>>>         {% for host in hostvars | dict2items | map(attribute='value') | 
>>>> map(attribute='failed_list', default=[]) | flatten %}
>>>>         {{ host }}
>>>>         {% endfor %}
>>>>       dest: */tmp/facts.csv*
>>>>     run_once: true
>>>>
>>>> /tmp/facts.csv gives
>>>>
>>>> # cat /tmp/facts.csvsomedata1
>>>> somedata2
>>>>
>>>>  I have another task cvs data under '*always*' section:
>>>>
>>>> always:
>>>> - name: append volume info to CSV
>>>>   *ansible.builtin.copy:*
>>>>       *content: |*
>>>>         failed
>>>>         {% example rule  %}
>>>>         {{ host }}
>>>>         {% endfor %}
>>>>       dest: /tmp/facts.csv
>>>>     run_once: true
>>>> Gives:
>>>>
>>>> # cat /tmp/facts.csvsomedata3
>>>>
>>>> somedata4
>>>>
>>>> It should append data instead of overwrite here. Is it possible?
>>>> We just need end of two files data into one:
>>>>
>>>> # cat /tmp/facts.csv
>>>> somedata1
>>>> somedata2
>>>> somedata3
>>>> somedata4
>>>>
>>>>
>>>> Thank you,
>>>>
>>>> On Thu, 16 Mar 2023, 00:13 Todd Lewis, <[email protected]> wrote:
>>>>
>>>>> Study and play around with these expressions until you understand what
>>>>> each piece does.
>>>>> set_fact sets a host-specific fact, which for convenience can be
>>>>> accessed like any other variable.
>>>>> Any host can see other hosts' facts/variables by looking in
>>>>> hostvars['somehost'].*varname*.
>>>>> The "CSV" is really just a list of failed hosts. With only one column,
>>>>> does CSV really mean anything?
>>>>> The final copy task should be a template task, but I left it in-line
>>>>> for clarity.
>>>>>
>>>>>   - name: Update failed_list fact
>>>>>     ansible.builtin.set_fact:
>>>>>       failed_list: "{{ failed_list | default([]) + [ansible_host] }}"
>>>>>
>>>>>   - name: Debug list
>>>>>     ansible.builtin.debug:
>>>>>      msg:
>>>>>       - "by play_hosts: {{ ansible_play_hosts | map('extract', hostvars) 
>>>>> | map(attribute='failed_list') | flatten }}"
>>>>>       - "by all: {{ hostvars | dict2items | map(attribute='value') | 
>>>>> map(attribute='failed_list', default=[]) | flatten }}"
>>>>>
>>>>>   - name: Create failed_list CSV
>>>>>     ansible.builtin.copy:
>>>>>       content: |
>>>>>         failed
>>>>>         {% for host in hostvars | dict2items | map(attribute='value') | 
>>>>> map(attribute='failed_list', default=[]) | flatten %}
>>>>>         {{ host }}
>>>>>         {% endfor %}
>>>>>       dest: /tmp/failed_list.csv
>>>>>     run_once: true
>>>>>
>>>>> Hope this helps.
>>>>> --
>>>>> Todd
>>>>>
>>>>> On 3/15/23 1:44 PM, Aharonu wrote:
>>>>>
>>>>> Hello Everyone,
>>>>>
>>>>> Greetings!
>>>>>
>>>>> I am working to get failed hosts from 'rescue' section into a CSV
>>>>> file.
>>>>>
>>>>> When i run task for *'inventory_hostname*' from *'rescue'* section:
>>>>>
>>>>> rescue:
>>>>>>         - name: inventory_host name list debug
>>>>>>           debug:
>>>>>>             msg: "{{ inventory_hostname }}"
>>>>>> Output:
>>>>>> TASK [inventory_host name list debug]
>>>>>> *******************************************************************************************************************************************************************************************************
>>>>>> ok: [bogus1] => {}
>>>>>>
>>>>>> MSG:
>>>>>>
>>>>>> bogus1
>>>>>> ok: [bogus2] => {}
>>>>>>
>>>>>> MSG:
>>>>>>
>>>>>> bogus2
>>>>>>
>>>>>> *when  i tried to append data to a list. *
>>>>>
>>>>>>         - set_fact:
>>>>>>             failed_list: "{{ failed_list + [ansible_host] }}"
>>>>>>         - name: failed_list debug
>>>>>>           debug: var=failed_list
>>>>>>
>>>>>
>>>>> Output:
>>>>> TASK [set_fact]
>>>>> *****************************************************************************************************************************************************************************************************************************
>>>>> ok: [bogus1]
>>>>> ok: [bogus2]
>>>>>
>>>>> TASK [failed_list debug]
>>>>> ********************************************************************************************************************************************************************************************************************
>>>>> ok: [bogus1] => {
>>>>>     "failed_list": [
>>>>>         "bogus1"
>>>>>     ]
>>>>> }
>>>>> ok: [bogus2] => {
>>>>>     "failed_list": [
>>>>>         "bogus2"
>>>>>     ]
>>>>> }
>>>>>
>>>>>
>>>>> Here bogus1, bogus2 host names are failed in 'resce' section.
>>>>> We have multiple hosts in our environment. While running playbook we
>>>>> have to capture failed hostname into a file  as mentioned below:
>>>>>
>>>>> *failed_hosts.csv:*
>>>>> *number of failed hots: 2*
>>>>> *hostname:*
>>>>> *bogus1*
>>>>> *bogus2*
>>>>>
>>>>>
>>>>> Thank you for your help.
>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "Ansible Project" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>>> an email to [email protected].
>>>>> To view this discussion on the web visit
>>>>> https://groups.google.com/d/msgid/ansible-project/CANGEjuXCUuCkps8CU9oWnh3XHN7jo6OJnGJQOCQRvay9w1rg2w%40mail.gmail.com
>>>>> <https://groups.google.com/d/msgid/ansible-project/CANGEjuXCUuCkps8CU9oWnh3XHN7jo6OJnGJQOCQRvay9w1rg2w%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>>>> .
>>>>>
>>>>>
>>>>> --
>>>>> Todd
>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "Ansible Project" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>>> an email to [email protected].
>>>>>
>>>> To view this discussion on the web visit
>>>>> https://groups.google.com/d/msgid/ansible-project/9646aced-38a3-b18b-e71f-d7186a8b41fa%40gmail.com
>>>>> <https://groups.google.com/d/msgid/ansible-project/9646aced-38a3-b18b-e71f-d7186a8b41fa%40gmail.com?utm_medium=email&utm_source=footer>
>>>>> .
>>>>>
>>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Ansible Project" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>>
>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/ansible-project/5f6783f9-7d45-41cf-8d7a-1b7a92303f53n%40googlegroups.com
>>> <https://groups.google.com/d/msgid/ansible-project/5f6783f9-7d45-41cf-8d7a-1b7a92303f53n%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/f70dd050-0d71-41fa-b836-d2df25e9555bn%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/f70dd050-0d71-41fa-b836-d2df25e9555bn%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CANGEjuWYVQ9Vzv6tt_COY0p36_X5dGqWZzXbYu5tENUgLKN%2B1A%40mail.gmail.com.

Reply via email to