2016-12-15 2 views
1

다음 재생을 고려하십시오. 내가 뭘 하려는지 기본적으로 키와 개정판 스크립트 dict의 각 요소에 함께 추가 된 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

좋아요, 적어도 해결책을 찾은 것 같습니다. 적어도이 문제를 해결해 드리겠습니다. 단점은 그것이 내 dict의 구조를 제거하고 또한 모든 필드를 재정의하고 새로운 변수를 사용하는 데 약간의 중복이있는 것 같습니다. 누군가가 더 나은 해결책을 제공 할 수 있다면 대신 받아 들일 것입니다. Using Ansible set_fact to create a dictionary from register results

:

--- 
- 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 }}" 
... 

은 BTW 올바른 방향으로 절 지적 다음과 같은 질문에 신용