alexis
alexis

Reputation: 1211

Ansible variable manipulation

I have a variable in my playbook that has a number of values separated by commas. At this point I am not sure if the variable is a string or a list. I believe from the output below the "[]" indicate a list.

Variable populated

-set_fact:
    snap_master_01: "{{ ec2_snapshot_facts.snapshots | 
selectattr(tags.HostName, equalto, ICINGA2_MASTER_1.tag_value) |
 sort(attribute=start_time) | reverse | map(attribute=snapshot_id) | list }}"

- name: Print snapshot ID's
   debug:
     msg:
        - "{{ snap_master_01 }}"`  

Gives the following output:

`TASK [Print snapshot ID's]     ********************************************************************************    ***********************
task path: /home/r_ansible/playbooks/backup_aws.yml:252
ok: [172.16.1.58] => {
    "changed": false,
    "msg": [
        [
            "snap-04c88ef6XXXXXXXXX",
            "snap-0bd5785fXXXXXXXXX",
            "snap-045e0f4bXXXXXXXXX",
            "snap-055fda51XXXXXXXXX",
            "snap-03759206XXXXXXXXX"
        ]
    ]
}`

I would like to delete the last 3 values. What is the best way of achieving this in Ansible?

Upvotes: 1

Views: 2369

Answers (1)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68319

To manipulate lists in Ansible, you can use Python slices.

In your case snap_master_01[:-3] will give you all but last three elements.

Upvotes: 2

Related Questions