Ansible when语句未给出预期结果

时间:2020-09-15 03:05:55

标签: ansible

我有一个剧本,该剧本收集了ec2_info,并且我想根据带when语句的条件检查的结果向用户显示控制台信息。在我的剧本中,when条件不起作用。我该如何执行任务以确保正确的when语句结果?

    - hosts: localhost
      gather_facts: false
    
      tasks:
    
        - name: ec2 instance info
          ec2_instance_info:
            aws_access_key: "{{ AWS_ACCESS_KEY_ID }}"
            aws_secret_key: "{{ AWS_SECRET_ACCESS_KEY }}"
            aws_region: "{{ AWS_REGION }}"
          register: result
    
        - name: display ec2 info
          debug:
            msg: 
              - "{{ item.tags['Name'] }}"
              - "{{ item.placement['availability_zone'] }}"
              - "{{ item.instance_type }}"
              - "{{ item.launch_time }}"
              - "{{ item.state['name'] }}"
          with_items:
            - "{{ result.instances }}"      
          loop_control:
            label: ""
          when: ("item.state['name'] == running")

1 个答案:

答案 0 :(得分:0)

这是when语句中的报价问题。下面的代码解决了我的问题:

    - name: display ec2 info
      debug:
        msg: 
          - "{{ item.tags['Name'] }}"
          - "{{ item.placement['availability_zone'] }}"
          - "{{ item.instance_type }}"
          - "{{ item.launch_time }}"
          - "{{ item.state['name'] }}"
      with_items:
        - "{{ result.instances }}"      
      loop_control:
        label: ""
      when: (item.state['name'] == "running")