4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Ansible】json_queryの活用~1回目:contain/starts_with/ends_with/and/or~

Last updated at Posted at 2020-12-22

はじめに

<バージョン>
ansible 2.9.1
jinja2 2.11
jmespath 0.9.3

Ansibleのjson_queryフィルタは便利なのですが、意外に記事が少なくて調べるのに苦労したので
まとめておきます。特に、contain/starts_with/ends_withはクセが強めで
使い方を理解するのに2時間くらいかかった気がします。

Playbook紹介

今回はよくある7パターンを紹介します

パターン 内容 playbookでの抽出例
1 特定の文字列に一致する(query_stringに変数なし) "aaa_bbb.txt"に一致する
2 特定の文字列に一致する(query_stringに変数あり) "aaa_bbb.txt"に一致する
3 特定の文字列を含む(contain) "aaa"を含む
4 特定の文字列から始まる(starts_with) "bbb"から始まる
5 特定の文字列で終わる(ends_with) ".txt"で終わる
6 and条件 "aaa"から始まる and ".txt"で終わる
7 or条件 "aaa"から始まる or ".txt"で終わる

ポイント
(1)パターン2のようにquery_stringに変数を入れる場合、{{ search_name }}のように変数を囲む
  参考:属性がAnsibleの変数と等しいjson配列から値を取得する
(2)containやstarts_withを使う場合は、to_json | from_json とフィルタを通す

jmes_path.yml
---
- name: "json_query test"
  hosts: localhost
  connection: local
  gather_facts: no
  vars:
    search_name: "aaa_bbb"
    sample:
      - id: 1
        name: "aaa_bbb.txt"
      - id: 2
        name: "bbb_ccc.log"
      - id: 3
        name: "aaa_bbb_txt"

  tasks:
    - name: "pattern1 - {{ query_string }}"
      debug:
        var: sample | json_query(query_string)
      vars:
        query_string: "[?name=='aaa_bbb.txt'].name"

    - name: "pattern2 - {{ query_string }}"
      debug:
        var: sample | json_query(query_string)
      vars:
        query_string: "[?name=='{{ search_name }}.txt'].name"    # ポイント(1)

    - name: "pattern3 - {{ query_string }}"
      debug:
        var: sample | to_json | from_json | json_query(query_string)    # ポイント(2)
      vars:
        query_string: "[?contains(name, 'aaa')].name"

    - name: "pattern4 - {{ query_string }}"
      debug:
        var: sample | to_json | from_json | json_query(query_string)
      vars:
        query_string: "[?starts_with(name, 'bbb')].name"

    - name: "pattern5 - {{ query_string }}"
      debug:
        var: sample | to_json | from_json | json_query(query_string)
      vars:
        query_string: "[?ends_with(name, '.txt')].name"

    - name: "pattern6 - {{ query_string }}"
      debug:
        var: sample | to_json | from_json | json_query(query_string)
      vars:
        query_string: "[?starts_with(name, 'aaa') && ends_with(name, '.txt')].name"

    - name: "pattern7 - {{ query_string }}"
      debug:
        var: sample | to_json | from_json | json_query(query_string)
      vars:
        query_string: "[?starts_with(name, 'aaa') || ends_with(name, '.txt')].name"

実行結果

想定通りの結果を得ることが出来ました

出力
[ec2-user@ip-<ip addr> ansible]$ ansible-playbook jmes_path.yml

PLAY [json_query test] *************************************************************************************************

TASK [pattern1 - [?name=='aaa_bbb.txt'].name] **************************************************************************
ok: [localhost] =>
  sample | json_query(query_string):
  - aaa_bbb.txt

TASK [pattern2 - [?name=='aaa_bbb.txt'].name] **************************************************************************
ok: [localhost] =>
  sample | json_query(query_string):
  - aaa_bbb.txt

TASK [pattern3 - [?contains(name, 'aaa')].name] ************************************************************************
ok: [localhost] =>
  sample | to_json | from_json | json_query(query_string):
  - aaa_bbb.txt
  - aaa_bbb_txt

TASK [pattern4 - [?starts_with(name, 'bbb')].name] *********************************************************************
ok: [localhost] =>
  sample | to_json | from_json | json_query(query_string):
  - bbb_ccc.log

TASK [pattern5 - [?ends_with(name, '.txt')].name] **********************************************************************
ok: [localhost] =>
  sample | to_json | from_json | json_query(query_string):
  - aaa_bbb.txt

TASK [pattern6 - [?starts_with(name, 'aaa') && ends_with(name, '.txt')].name] ******************************************
ok: [localhost] =>
  sample | to_json | from_json | json_query(query_string):
  - aaa_bbb.txt

TASK [pattern7 - [?starts_with(name, 'aaa') || ends_with(name, '.txt')].name] ******************************************
ok: [localhost] =>
  sample | to_json | from_json | json_query(query_string):
  - aaa_bbb.txt
  - aaa_bbb_txt

PLAY RECAP *************************************************************************************************************
localhost                  : ok=7    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

参考資料

JMESPath Specification
Ansible Documentation
属性がAnsibleの変数と等しいjson配列から値を取得する

関連記事

【Ansible】json_queryの活用~2回目:to_string/max_by/min_by/length/type~

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?