---
- name: Creating CSVs from subsets of data
  hosts: localhost
  gather_facts: false
  vars:
    csv:
      - cluster1,virtual_clu1,log_vol1,online,used
      - cluster1,virtual_clu1,log_vol2,offline,not_used
      - cluster1,virtual_clu1,log_vol3_Test,online,not_used
      - cluster1,virtual_clu1,log_vol4,offline,not_used
      - cluster2,virtual_clu2,log_vol6,online,used
      - cluster2,virtual_clu2,log_vol1,offline,not_used
      - cluster2,virtual_clu2,log_vol3,online,not_used
      - cluster2,virtual_clu2,log_vol4,online,used
  tasks:

    - name: Copy csv where status == 'online' and work_status == 'not_used'
      ansible.builtin.copy:
        content: |
          cluster_name,log_cluster,vol_name,status,work_status
          {% for line in csv %}
          {%     set (cluster_name,log_cluster,vol_name,status,work_status) = 
line.split(',', maxsplit=4) %}
          {%     if status == 'online' and work_status == 'not_used' %}
          {{ line }}
          {%     endif %}
          {% endfor %}
        dest: aharonu-online-not_used.csv

    - name: Copy csv where status == 'offline' and vol_name.endswith('Test')
      # Note: there are none of these in the test data.
      ansible.builtin.copy:
        content: |
          cluster_name,log_cluster,vol_name,status,work_status
          {% for line in csv %}
          {%     set (cluster_name,log_cluster,vol_name,status,work_status) = 
line.split(',', maxsplit=4) %}
          {%     if status == 'offline' and vol_name.endswith('Test') %}
          {{ line }}
          {%     endif %}
          {% endfor %}
        dest: aharonu-offline-test.csv

    - name: Copy csv where status == 'online' and work_status == 'used'
      ansible.builtin.copy:
        content: |
          cluster_name,log_cluster,vol_name,status,work_status
          {% for line in csv %}
          {%     set (cluster_name,log_cluster,vol_name,status,work_status) = 
line.split(',', maxsplit=4) %}
          {%     if status == 'online' and work_status == 'used' %}
          {{ line }}
          {%     endif %}
          {% endfor %}
        dest: aharonu-online-used.csv

Save the above as aharonu-csv.yml and run as

ansible-playbook aharonu-csv.yml -vv && grep , ./aharonu*.csv


On 3/6/23 2:58 PM, Aharonu wrote:
Thanks Walter.

May I still ask a favor for CVS data structure atleast for any of one condion here so I can refer and work on remaining? Once I got flow it helps to explore more. Thanking you.

The header must be: cluster_name,log_cluster,vol_name,status,work_status

not working:
(it has to give here when status=online and  work_status=not_used )
exclude:
(it has go give here when status=offline and  vol_name=<name contacts 'Test'>)
working:
(it has to give here when status=online and work_status=used
[other then not working & exclude list] )



On Tue, 7 Mar 2023, 01:13 Aharonu, <[email protected]> wrote:

    Hi Walter,

    Getting for the day!

    Actually I have data stored in 'csv' as mentioned below it is
    received from some other task and stories in 'csv' variable.

    I wanted to created CVS file from that data with required data
    structure I requested. Could you please help me with this to
    achieve my requirement? Thank you

    On Tue, 7 Mar 2023, 00:39 Aharonu, <[email protected]> wrote:

        Hi All,

        Could anyone please help for storing data into CSV file from
        variable with required structure ? Thank you

        I have csv variable data from other set_fact as mentioned below

        "csv": [
        "cluster1,virtual_clu1,log_vol1,online,used",
        "cluster1,virtual_clu1,log_vol2,offline,not_used",
        "cluster1,virtual_clu1,log_vol3_Test,online,not_used",
        "cluster1,virtual_clu1,log_vol4,offline,not_used",
        "cluster2,virtual_clu2,log_vol6,online,used",
        "cluster2,virtual_clu2,log_vol1,offline,not_used",
        "cluster2,virtual_clu2,log_vol3,online,not_used"
                "cluster2,virtual_clu2,log_vol4,online,used"
            ]

        *Looking for:*
        The header must be:
        cluster_name,log_cluster,vol_name,status,work_status

        not working:
        (it has to give here when status=online and
         work_status=not_used )
        exclude:
        (it has go give here when status=offline and  vol_name=<name
        contacts 'Test'>)
        working:
        (it has to give here when status=online and work_status=used
        [other then not working & exclude list] )

        *_example: file1.csv_*

        not working:
        cluster_name,log_cluster,vol_name,status,work_status
        cluster2,virtual_clu2,log_vol3,online,not_used

        exclude:
        cluster_name,log_cluster,vol_name,status,work_status
        cluster1,virtual_clu1,log_vol3_Test,online,not_used
        cluster1,virtual_clu1,log_vol2,offline,not_used
        cluster1,virtual_clu1,log_vol4,offline,not_used
        cluster2,virtual_clu2,log_vol1,offline,not_used

        working:
        cluster_name,log_cluster,vol_name,status,work_status
        cluster1,virtual_clu1,log_vol1,online,used
        cluster2,virtual_clu2,log_vol6,online,used
        cluster2,virtual_clu2,log_vol4,online,used







--
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/CANGEjuUjv761HrBWPatHrFCqrCt7-cRLtSda0eA2%3D7o26-Rjtw%40mail.gmail.com <https://groups.google.com/d/msgid/ansible-project/CANGEjuUjv761HrBWPatHrFCqrCt7-cRLtSda0eA2%3D7o26-Rjtw%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/21aa0394-92f5-82c8-bcc4-f9b988619984%40gmail.com.

Reply via email to