1
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 1 year has passed since last update.

Ansible: dictionaryデータの一部を書き換える

Last updated at Posted at 2022-12-03

dictionaryデータの一部を書き換える

例えば、以下のようなdictionaryデータのdb2.img部分のみ書き換える。

サンプルdictionaryデータ

vars/main.yml
db2:
  img: v11.5.7_linuxx64_server_dec.tar.gz
  install_path: /opt/ibm/db2/V11.5
  ...

書き換えtask

combineフィルターが使用可能

- set_fact:
    db2: "{{ db2|combine({'img': 'v11.5.8_linuxx64_server_dec.tar.gz'}, recursive=true) }}"

もしくはtask varsを使って書き直すと

- vars:
    db2_img:
      img: v11.5.8_linuxx64_server_dec.tar.gz
  set_fact:
    db2: "{{ db2|combine(db2_img, recursive=true) }}"

蛇足: set_fact使用上の注意

set_factでは、変数などが解釈されてしまうので注意。

たとえば、以下の様な属性の場合を考える。

vars/main.yml
db2:
  img: v11.5.7_linuxx64_server_dec.tar.gz
  install_path: "{{ shared_filesystem|default('') }}/opt/ibm/db2/V11.5"
  ...
  • set_factを用いたdb2.imgの再設定を行なわない場合、
    • db2.install_pathの先頭は、
      • db2.install_path使用時点のshared_filesystemの値となる。

しかし、

  • set_factを用いたdb2.imgの再設定を行う場合、
    • db2.install_pathの先頭は、
      • set_fact使用時点のshared_filesystemの値となり、
      • db2.install_path使用時点のshared_filesystemの値には影響されない。

といった差異が生じることなり、個人的にはこれに引っかかったので、注意。

なお、当然、combineした値をtask varsで使用するケースでは、問題とならない。
block varsならどうだろう?

1
0
1

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
1
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?