Skip to content

Instantly share code, notes, and snippets.

@halberom
Created May 12, 2017 09:33
Show Gist options
  • Save halberom/3659c98073efcabd91ed1dec3ad63fa3 to your computer and use it in GitHub Desktop.
Save halberom/3659c98073efcabd91ed1dec3ad63fa3 to your computer and use it in GitHub Desktop.
ansible - example of using selectattr to filter on attrs that are defined and true
---
- hosts: localhost
connection: local
gather_facts: False
vars:
apache2_vhosts:
- name: "stage.example.org"
docroot: "/var/www/stage1"
- name: "stage2.example.org"
docroot: "/var/www/stage2"
letsencrypt: no
- name: "www.example.org"
docroot: "/var/www/www"
letsencrypt: yes
tasks:
- debug:
msg: "{{ apache2_vhosts | selectattr('letsencrypt', 'defined') | selectattr('letsencrypt', 'equalto', True) | list }}"
PLAY [localhost] **********************************************************************************************************************
TASK [debug] **************************************************************************************************************************
ok: [localhost] => {
"changed": false,
"msg": [
{
"docroot": "/var/www/www",
"letsencrypt": true,
"name": "www.example.org"
}
]
}
PLAY RECAP ****************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0
@BarbzYHOOL
Copy link

fatal: [localhost]: FAILED! =>
msg: Unexpected failure during module execution.

@halberom

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment