2014-11-06 1 views
3

문자열 목록이 있습니다. strs = [ 'foo', 'bar'] 및 일부 dicts foo = { 'a': 1, 'b': 2}, bar = { 'a': 3, 'b': 4}. 나는 이름 dicts 가변 문자열 (일명 평가)

- copy 
    src: {{item}}.a 
    dest: {{item}}.b 
    with_items: strs 

에 인덱스 with_items을 사용하고 싶습니다하지만 {{항목}} foo는 이름의 변수를 참조하고 싶은 문자열이 아닌 바. lisp이나 python에서는 eval을 사용했습니다. 무언가가 비슷한가?

답변

3

을 사용하여 사전을 설정하고 반복하지 않는 이유는 무엇입니까?

--- 
- hosts: localhost 
    connection: local 
    vars: 
    strs: 
     foo: 
     a: 1 
     b: 2 
     bar: 
     a: 3 
     b: 4 

    tasks: 
    - copy: src={{ item.value.a }} dest={{ item.value.b }} 
    with_dict: strs 
0

아마도 그는 다음과 같이 더욱 역동적 인 것이 필요할 것입니다.

이것은 2.1에서 가능합니다. 해시 키의 배열을 갖는 해시의 하위 집합을 가져와야했지만,이 게시물에 대해 비틀 거리며 솔루션을 발견했습니다.

나는 that을 사용합니다.

나는 완전히 어리 석고 쓸모 없지만 완전한 예를 제공하기로 선택한다.

--- 
- hosts: localhost 
    vars: 
    filenames: [ file1, file2 ] 
    file1: 
     a: file1.c 
     b: file1.o 
    file2: 
     a: file2.c 
     b: file2.o 
    file3: 
     a: file3.py 
     b: file3.pyc 
    tasks: 
    - debug: msg="gcc -c -o {{item.b}} {{item.a}}" 
     with_items: 
     - "{{ filenames | map('extract', vars) | list}}" 

ansible - 각본의 출력은 이것이다 :

PLAY [localhost] *************************************************************** 

TASK [setup] ******************************************************************* 
ok: [localhost] 

TASK [debug] ******************************************************************* 
ok: [localhost] => (item={u'a': u'file1.c', u'b': u'file1.o'}) => { 
    "item": { 
     "a": "file1.c", 
     "b": "file1.o" 
    }, 
    "msg": "gcc -c -o file1.o file1.c" 
} 
ok: [localhost] => (item={u'a': u'file2.c', u'b': u'file2.o'}) => { 
    "item": { 
     "a": "file2.c", 
     "b": "file2.o" 
    }, 
    "msg": "gcc -c -o file2.o file2.c" 
} 

PLAY RECAP ********************************************************************* 
localhost     : ok=2 changed=0 unreachable=0 failed=0 

나는이 의견에 관심이 있어요.