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 5 years have passed since last update.

【備忘録】jinja2のselectフィルターを使ってAnsibleの変数を出力する方法

Last updated at Posted at 2018-07-17

selectフィルターを使ってリストから指定の文字列だけ出力する方法。
jinja2やansibleのドキュメントだと調べるのが大変に感じたので備忘録として残しておきます。

test01.yml

- name: practice filter
  hosts: localhost  
  gather_facts: False  

  vars:
    test_servers:      
      - server1
      - server2
      - server3
      - server4  

  tasks:    
    - name: extract only server1
      vars:
        v: "{{ test_servers|select('equalto', 'server1')|list }}"
     debug: var=v

出力結果

ok: [localhost] => {
    "v": [
        "server1"
    ]
}

selectフィルターは'equalto'などの変数 + listとセットで使ってあげないと、ジェネレーターを返してきてしまいます。

セットで使ってないPlaybook

``` test01.yml
  • name: practice filter
    hosts: localhost
    gather_facts: False

    vars:
    test_servers:
    - server1
    - server2
    - server3
    - server4

    tasks:

    • name: extract only server1
      vars:
      v: "{{ test_servers|select('server1') }}"
       debug: var=v

出力結果

ok: [localhost] => {
"v": ""
}



後日リスト全体をマッチさせたい場合の書き方について編集予定。

coming soon.

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?