A simple example of creating a list from a dictionary var using Jinja filters in an ansible play task. see article: http://www.blue-bag.com/blog/ansible-filters-taming-lists-part-1
Last active
August 29, 2015 14:17
-
-
Save iAugur/aced8b964392519654c9 to your computer and use it in GitHub Desktop.
Ansible Filters and variables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- hosts: localhost | |
gather_facts: false | |
vars: | |
images_to_update: | |
- { name: "oranges-001.jpg", type: "Citrus"} | |
- { name: "oranges-002.jpg", type: "Citrus"} | |
- { name: "apples-001.jpg", type: "Non-citrus"} | |
- { name: "bananas-001.jpg", type: "Non-citrus"} | |
- { name: "grapes-001.jpg", type: "Non-citrus"} | |
- { name: "grapefruit-001.jpg", type: "Citrus"} | |
- { name: "pears-001.jpg", type: "Non-citrus"} | |
tasks: | |
- debug: msg="images_to_update {{ item.name }}" | |
with_items: images_to_update | |
- name: Setfact for archives to drop | |
set_fact: | |
fruit_archives_to_drop: "{{images_to_update|map(attribute='type')|list|unique }}" | |
- debug: msg="{{ fruit_archives_to_drop }}" | |
- debug: msg="{{ item }}" | |
with_items: fruit_archives_to_drop |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PLAY [localhost] ************************************************************** | |
TASK: [debug msg="images_to_update {{ item.name }}"] ************************** | |
ok: [localhost] => (item={'type': 'Citrus', 'name': 'oranges-001.jpg'}) => { | |
"item": { | |
"name": "oranges-001.jpg", | |
"type": "Citrus" | |
}, | |
"msg": "images_to_update oranges-001.jpg" | |
} | |
ok: [localhost] => (item={'type': 'Citrus', 'name': 'oranges-002.jpg'}) => { | |
"item": { | |
"name": "oranges-002.jpg", | |
"type": "Citrus" | |
}, | |
"msg": "images_to_update oranges-002.jpg" | |
} | |
ok: [localhost] => (item={'type': 'Non-citrus', 'name': 'apples-001.jpg'}) => { | |
"item": { | |
"name": "apples-001.jpg", | |
"type": "Non-citrus" | |
}, | |
"msg": "images_to_update apples-001.jpg" | |
} | |
ok: [localhost] => (item={'type': 'Non-citrus', 'name': 'bananas-001.jpg'}) => { | |
"item": { | |
"name": "bananas-001.jpg", | |
"type": "Non-citrus" | |
}, | |
"msg": "images_to_update bananas-001.jpg" | |
} | |
ok: [localhost] => (item={'type': 'Non-citrus', 'name': 'grapes-001.jpg'}) => { | |
"item": { | |
"name": "grapes-001.jpg", | |
"type": "Non-citrus" | |
}, | |
"msg": "images_to_update grapes-001.jpg" | |
} | |
ok: [localhost] => (item={'type': 'Citrus', 'name': 'grapefruit-001.jpg'}) => { | |
"item": { | |
"name": "grapefruit-001.jpg", | |
"type": "Citrus" | |
}, | |
"msg": "images_to_update grapefruit-001.jpg" | |
} | |
ok: [localhost] => (item={'type': 'Non-citrus', 'name': 'pears-001.jpg'}) => { | |
"item": { | |
"name": "pears-001.jpg", | |
"type": "Non-citrus" | |
}, | |
"msg": "images_to_update pears-001.jpg" | |
} | |
TASK: [Setfact for archives to drop] ****************************************** | |
ok: [localhost] | |
TASK: [debug msg="{{ fruit_archives_to_drop }}"] ************************************ | |
ok: [localhost] => { | |
"msg": "['Citrus', 'Non-citrus']" | |
} | |
TASK: [debug msg="{{ item }}"] ************************************************ | |
ok: [localhost] => (item=Citrus) => { | |
"item": "Citrus", | |
"msg": "Citrus" | |
} | |
ok: [localhost] => (item=Non-citrus) => { | |
"item": "Non-citrus", | |
"msg": "Non-citrus" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment