代码之家  ›  专栏  ›  技术社区  ›  Chris F

在Ansible中,如何使用数组迭代stdout?

  •  0
  • Chris F  · 技术社区  · 6 年前

    Ansible v2.6.3版

      tasks:
      - command: aws ecs list-container-instances --cluster jenkins
        register: jenkins_ecs_containers
    
      - debug: var=jenkins_ecs_containers.stdout
    

    具有以下输出

    TASK [debug] *******************************************************************
    ok: [localhost] => {
        "jenkins_ecs_containers.stdout": {
            "containerInstanceArns": [
               "arn:aws:ecs:us-east-1:arn0", 
               "arn:aws:ecs:us-east-1:arn1"
            ]
        }
    }
    

    - debug: var=item
      with_items: jenkins_ecs_containers.stdout.containerInstanceArns
    

    TASK [debug] *******************************************************************
    ok: [localhost] => (item=jenkins_ecs_containers.stdout.containerInstanceArns) => {
        "item": "jenkins_ecs_containers.stdout.containerInstanceArns"
    }
    

    - debug: var=item
      with_items: "{{ jenkins_ecs_containers.stdout.containerInstanceArns }}"
    

    TASK [debug] *******************************************************************
    fatal: [localhost]: FAILED! => {"msg": "'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'containerInstanceArns'"}
        to retry, use: --limit @/Users/cfouts/git-repos/ansible/playbooks/loop.retry
    

    谢谢!

    2 回复  |  直到 6 年前
        1
  •  0
  •   Jack    6 年前

    我用你的输出创建了一个文件。所以我用 set_fact . 否则,它只是一个字符串,而不是JSON对象:

      tasks:
    
      - command: cat files/stdout.txt
        register: result
    
      - debug: var=result.stdout
    
      - set_fact:
          jenkins_ecs_containers: "{{ result.stdout }}"
    
      - debug:
          msg: "{{ item }}"
        with_items: "{{ jenkins_ecs_containers.containerInstanceArns }}"
    

    这给了我以下输出:

    PLAY [localhost] ***************************************************************
    
    TASK [Gathering Facts] *********************************************************
    ok: [localhost]
    
    TASK [command] *****************************************************************
    changed: [localhost]
    
    TASK [debug] *******************************************************************
    ok: [localhost] => {
        "result.stdout": {
            "containerInstanceArns": [
                "arn:aws:ecs:us-east-1:arn0", 
                "arn:aws:ecs:us-east-1:arn1"
            ]
        }
    }
    
    TASK [set_fact] ****************************************************************
    ok: [localhost]
    
    TASK [debug] *******************************************************************
    ok: [localhost] => (item=None) => {
        "msg": "arn:aws:ecs:us-east-1:arn0"
    }
    ok: [localhost] => (item=None) => {
        "msg": "arn:aws:ecs:us-east-1:arn1"
    }
    
    PLAY RECAP *********************************************************************
    localhost                  : ok=5    changed=1    unreachable=0    failed=0   
    
        2
  •  0
  •   robinhoodjr    6 年前

    你可以这样迭代:

    - debug:
        msg: "{{ item[1] }}"
      with_subelements:
        - "{{ jenkins_ecs_containers }}"
        - containerInstanceArns
    

    经历这个 link