0
0

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

Ansibleで変数で定義されたリストを文字列に変換

Last updated at Posted at 2020-06-04

概要

with_itemsだと同じコマンドをループしてしまうのでリストを文字列結合してtaskで利用したかった。

e.g.)

vars:
  list:
   - foo
   - bar
   - baz

shell: command --option foo bar baz

結論

  • set_factにJinja2的な出力の仕方で変数化してしまう。
  • ここではdebugで出力しているが shell: echo{{ str }} のような形で利用できる。

定義

- name: concat strings
  set_fact:
    str: " {{ names | join(' ') }}"
  vars:
    - names:
        - foo
        - bar
        - baz

- name: view var
  debug:
    var: str

実行結果

TASK [debug : concat strings] *******************************************************************************************************************************************************************************************************************************************
ok: [host]

TASK [debug : view var] *************************************************************************************************************************************************************************************************************************************************
ok: [host] => {
    "str": " foo bar baz"
}


もっと良い方法があれば教えてほしいです。

0
0
3

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?