--
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 ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CANGEjuWprqkT90LdW3%2BVzUfs5YD4pwDZZnmiUMFHWdkVmqzjOQ%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/127203C1-7A98-46C0-9BCF-72065591FECB%40nist.gov.
---
- name: test loop with JSON dict list
hosts: all
become: false
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: convert csv strings to json dict list
set_fact:
csv_json: "{{ csv_json|default([]) + [ { 'cluster_name': entry[0], 'log_cluster': entry[1], 'vol_name': entry[2], 'status': entry[3], 'work_status': entry[4] } ] }}"
loop: "{{ csv }}"
vars:
entry: "{{ item | split(',') }}"
- name: show reference
debug:
msg:
- "{{ csv_json }}"
"msg": [
[
{
"cluster_name": "cluster1",
"log_cluster": "virtual_clu1",
"status": "online",
"vol_name": "log_vol1",
"work_status": "used"
},
{
"cluster_name": "cluster1",
"log_cluster": "virtual_clu1",
"status": "offline",
"vol_name": "log_vol2",
"work_status": "not_used"
},
{
"cluster_name": "cluster1",
"log_cluster": "virtual_clu1",
"status": "online",
"vol_name": "log_vol3_Test",
"work_status": "not_used"
},
{
"cluster_name": "cluster1",
"log_cluster": "virtual_clu1",
"status": "offline",
"vol_name": "log_vol4",
"work_status": "not_used"
},
{
"cluster_name": "cluster2",
"log_cluster": "virtual_clu2",
"status": "online",
"vol_name": "log_vol6",
"work_status": "used"
},
{
"cluster_name": "cluster2",
"log_cluster": "virtual_clu2",
"status": "offline",
"vol_name": "log_vol1",
"work_status": "not_used"
},
{
"cluster_name": "cluster2",
"log_cluster": "virtual_clu2",
"status": "online",
"vol_name": "log_vol3",
"work_status": "not_used"
},
{
"cluster_name": "cluster2",
"log_cluster": "virtual_clu2",
"status": "online",
"vol_name": "log_vol4",
"work_status": "used"
}
]
]
- name: not working
debug: var=item
loop: "{{ csv_json }}"
when: item.status == 'online' and item.work_status == 'not_used'
- name: exclude
debug: var=item
loop: "{{ csv_json }}"
when: (item.status == 'offline') and ('Test' in item.vol_name)
- name: working
debug: var=item
loop: "{{ csv_json }}"
when: (item.status == 'online') and (item.work_status == '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 ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CANGEjuUEowFAMC1ae6TRFeUcT_vT%3DrrzUSTwff%3DEWJZCW%3Dpi5g%40mail.gmail.com.
---
- 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 asansible-playbook aharonu-csv.yml -vv && grep , ./aharonu*.csv
--
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 ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CANGEjuUjv761HrBWPatHrFCqrCt7-cRLtSda0eA2%3D7o26-Rjtw%40mail.gmail.com.
-- Todd
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/21aa0394-92f5-82c8-bcc4-f9b988619984%40gmail.com.