Skip to content

Instantly share code, notes, and snippets.

@iAugur
Last active August 29, 2015 14:17
Show Gist options
  • Save iAugur/aced8b964392519654c9 to your computer and use it in GitHub Desktop.
Save iAugur/aced8b964392519654c9 to your computer and use it in GitHub Desktop.
Ansible Filters and variables
---
- 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
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