2016-12-15 25 views
1

次のプレイを考えてみましょう。私がやろうとしているのは、基本的にキーとリビジョンであるtmp_pathというフィールドをスクリプトのdictの各要素に追加して追加することです。項目を追加するフィールドを追加する

--- 
- hosts: localhost 
    connection: local 
    gather_facts: no 
    vars: 
    scripts: 
     a.pl: 
     revision: 123 
     b.pl: 
     revision: 456 
    tasks: 
    - with_dict: "{{ scripts }}" 
     debug: 
     msg: "{{ item.key }}_{{ item.value.revision }}" 
#  - with_items: "{{ scripts }}" 
#  set_fact: {{item.value.tmp_path}}="{{item.key}}_{{item.value.revision}}" 
#  - with_items: "{{ scripts }}" 
#  debug: 
#   msg: "{{ item.value.tmp_path }}" 
... 

明らかにコメントコードは機能しません。どのように私はこれを動作させることができますか?スクリプトのディクテーションを直接変更することは可能ですか、どういうわけか代わりに新しいディクテーションを作成する必要がありますか?

ところで私がやろうとしていることの用語を修正することを歓迎します。

答えて

0

[OK]を、私は解決策を持っていると思う、少なくとも私はこれで前進してください。欠点は、私の辞書の構造を削除したことであり、また、すべてのフィールドを再定義して新しい変数を使用する必要が少し冗長であるようです。誰かがより良い解決策を提供できるならば、代わりにそれを受け入れます。

--- 
- hosts: localhost 
    connection: local 
    gather_facts: no 
    vars: 
    scripts: 
     a.pl: 
     revision: 123 
     b.pl: 
     revision: 456 
    tasks: 
    - with_dict: "{{ scripts }}" 
     debug: 
     msg: "{{ item.key }}_{{ item.value.revision }}" 
    - with_dict: "{{ scripts }}" 
     set_fact: 
     new_scripts: "{{ (new_scripts | default([])) + [ {'name': item.key, 'revision': item.value.revision, 'tmp_path': item.key ~ '_' ~ item.value.revision}] }}" 
#  - debug: 
#   var: x 
#  - with_dict: "{{ scripts }}" 
    - with_items: "{{ new_scripts }}" 
     debug: 
     msg: "{{ item.tmp_path }}" 
... 

ところで正しい方向に私を指摘し、次の質問にクレジット: Using Ansible set_fact to create a dictionary from register results

関連する問題