Skip to content

Instantly share code, notes, and snippets.

@krdlab
Created November 9, 2020 10:09
Show Gist options
  • Save krdlab/72a94d198bbef245a49124e5a6a95ca9 to your computer and use it in GitHub Desktop.
Save krdlab/72a94d198bbef245a49124e5a6a95ca9 to your computer and use it in GitHub Desktop.
Ansible の selectattr で正規表現を使う
- hosts: localhost
gather_facts: false
vars:
targets:
- instance_id: 1
instance_type: r4.large
- instance_id: 2
instance_type: r5.large
tasks:
- name: list match
debug:
msg: "{{ targets | selectattr('instance_type', 'match', '^r5\\..+') | list }}"
@krdlab
Copy link
Author

krdlab commented Nov 9, 2020

実行結果

$ ansible-playbook -i localhost, hoge.yml 

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

TASK [list match] *******************************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        {
            "instance_id": 2, 
            "instance_type": "r5.large"
        }
    ]
}

PLAY RECAP **************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0

@krdlab
Copy link
Author

krdlab commented Nov 9, 2020

否定は selectattrrejectattr に変更すれば良い.

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