Home > Mobile >  Got out list of values, How do i select first 10 or first 20 or first 30 only
Got out list of values, How do i select first 10 or first 20 or first 30 only

Time:08-22

[
"dltpglci553i7mxg3vbkml7ns46p57bbnsn",
"zpxtlv6s37frl4645n3pfcbg3pblrfgmjzvd5n7w",
"wfv4mrzqnq2nq44xbsn35iiqnindixct2kspwfdq",
"p2vgqnvwwnwdxmrjk6lzk6x6wfwinjdc4tbgq",
"ttg3i44dxflbnwlbpqvmm22kvbkgcjmzl4fjrznipgkcr",
"qqc7lficx3bcxrfrkt4dnrrb4m7m2iwd73jpfqjsngc",
"jlpclfrzspps7jt5jt2dtvvbg3mtnllq3sdr4q",
"633gpf6dg7mlstbncbxql6jt46sgwlpvj2fvviq",
"cmgfsw527bs2wwjmqmfw6bv4ctb6m3kr3rk5s4dbq",
"5qrxtckg3r5q67ntzjv5tivjkqmfgxibqvfk",
"tsgqxp2kjrjjl5dfm3dp2k62xcgvfjqvccm4b72rdxn",
"qktssclbi6k3wqd7gisgs5gfdgdqnm64b36dsmj55s",
"3flglzwmlv22sgdn3xd3p6cw2qqisln5bzbncgbg7gbr",
"jrz56p7nsbnkclvqzi4slt7bvbjcqnxic4qs",
"rwktr34qcxf76f7nkxim4tzkvtt5g36vr6k67pinznpfq",
"2wqq4i76rqf4gtwffktdvz52t3sgnkd4ggv6n",
"sivpqtv35q33g5qgp4snjcbpgr27jlwx66il2swgcq",
"mi2q5nzk4gnnpxtvsppbg5c5nirggpj7fqqdiv6wnl",
"wrrjcl6rcndd24wwwtl563izrclzjv2x2sxxn44rq",
"g4s2qfxgcntcxftv76c6xfki44ziqvvslfqwgj",
"kp3svkqkpkfti2tsbm2c5ds6vnlfvgmtx6sjkgv4i",
"i57ck7jpqrgi6vgwpqvnkw42sdjcqb7gqlmilll",
"lj35g346rpz3tw73qz4pkfbdisq5pbbrtlv5t",
"gbcxv3dgdkk255llittdgn2irwwtidbzcr67n3jf"]

I got output from running ansible and as a list of values. Now What I want it to slice this list [:10] or something which gives me first 10 values or last values.

 - name: Debug sort
      
       debug: msg="{{ jsondata.data | sort(attribute='time-created') | map(attribute='id') list  }}"
       vars: 
        query1: '"time-created"'
        query2: 'id'    
        query3: 'data[*].id'

I just want to slice the list from start or end, let's say I want 10 values from start or 20 values from last item of the list. Is there is filter I can add in debug here ?

Also, where I can get more filters/or this jinja2 documentation. I am finding all over different places and not able to find at one.

CodePudding user response:

Create the list

ids: "{{ jsondata.data|sort(attribute='time-created')|
                       map(attribute='id')|
                       list }}"

Then slice the list. For example, the first 10 values and last 20 values

ids_first_10: "{{ ids[:10] }}"
ids_last_20: "{{ ids[20:] }}"

Example of a complete playbook for testing

shell> cat pb.yml
- hosts: localhost

  vars:

    jsondata: "{{ out.stdout|from_yaml }}"
    ids: "{{ jsondata.data|sort(attribute='time-created')|
                           map(attribute='id')|
                           list }}"
    ids_first_10: "{{ ids[:10] }}"
    ids_last_20: "{{ ids[20:] }}"

  tasks:

    - name: Create file data.json
      copy:
        dest: /tmp/data.json
        content: "{{ content|from_yaml|to_json }}"
      vars:
        content: |
          data:
          {% for id in query('sequence', 'count=50') %}
            - time-created: today
              id: {{ id }}
          {% endfor %}
      when: create_data|d(false)|bool

    - command: cat /tmp/data.json
      register: out

    - debug:
        var: ids_first_10|to_yaml

Create the file for the first time

shell> ansible-playbook pb.yml -e create_data=true

Then, test the playbook

shell> ansible-playbook pb.yml

PLAY [localhost] *****************************************************************************

TASK [Create file data.json] *****************************************************************
skipping: [localhost]

TASK [command] *******************************************************************************
changed: [localhost]

TASK [debug] *********************************************************************************
ok: [localhost] => 
  ids_first_10|to_yaml: |-
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

PLAY RECAP ***********************************************************************************
localhost: ok=2    changed=1    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

Example by @Harry from the comment below will display all but the first 5 values. The suggestions below would improve the code:

  • The variables query* are redundant. Delete them.

  • Putting ids into the task's vars is not very practical. The such variable will be defined in this task only. You'll have to calculate it again if needed. Instead, put it into the play's vars or any other place as convenient.

    - name: Debug sort
      debug:
        msg: "{{ ids[5:] }}"
      vars:
        query1: '"time-created"'
        query2: 'id'
        query3: 'data[*].id'
        ids: "{{ jsondata.data|sort(attribute='time-created')|map(attribute='id')|list }}"

CodePudding user response:

- hosts: localhost

  vars:

    jsondata: "{{ out.stdout|from_yaml }}"
    ids: "{{ JSON data.data|sort(attribute='time-created')|
                           map(attribute='id')|
                           list }}"
    ids_first_10: "{{ ids[:10] }}"
    ids_last_20: "{{ ids[20:] }}"

  tasks:

    - name: Create file data.json
      copy:
        dest: /tmp/data.json
        content: "{{ content|from_yaml|to_json }}"
      vars:
        content: |
          data:
          {% for id in query('sequence', 'count=50') %}
            - time-created: today
              id: {{ id }}
          {% endfor %}
      when: create_data|d(false)|bool

    - command: cat /tmp/data.json
      register: out

    - debug:
        var: ids_first_10|to_yaml

I tried but its returning error, I see tmp/data.json exist only it failed in debug

fatal: [localhost]: FAILED! => {"msg": "An unhandled exception occurred while templating '{{ ids[:10] }}'. Error was a <class 'ansible.errors.AnsibleError'>, original message: An unhandled exception occurred while templating '{{ JSON data.data|sort(attribute='time-created')| map(attribute='id')| list }}'. Error was a <class 'ansible.errors.AnsibleError'>, original message: template error while templating string: expected token 'end of print statement', got 'data'. String: {{ JSON data.data|sort(attribute='time-created')| map(attribute='id')| list }}"}

  • Related