ansible V2 - > " skip_reason":"条件检查失败" with_items

时间:2016-08-19 10:16:46

标签: ansible

我在执行以下操作时遇到了一些错误:

group_vars:

tomcat_servers:
- name: tomcat_1
  shutdown_port: 8005
  connector_port: 8080
  ajp_port: 8009
- name: tomcat_2
  shutdown_port: 8105
  connector_port: 8180
  ajp_port: 8109

主要代码:

  - name: "Check if tomcat is already installed"
    stat: path={{ tomcat_server_dir }}/{{ item.name }}/RELEASE-NOTES
    register: status
    with_items: "{{ tomcat_servers }}"

  - debug: var=status

  - name: "Copy tomcat into folder if it is not installed"
    command: /bin/tar -zxvf /tmp/{{ tomcat_catalina_base }} -C {{ tomcat_server_dir }}/{{ item.name }} --strip 2
when: not status.results[0].stat.exists
with_items:
  - "{{ tomcat_servers }}"
  - "{{ status.results }}"

调试结果:

    ok: [VM1] => {
        "status": {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "changed": false,
                "invocation": {
                    "module_args": {
                        "checksum_algorithm": "sha1",
                        "follow": false,
                        "get_checksum": true,
                        "get_md5": true,
                        "mime": false,
                        "path": "/opt/tomcat_3/RELEASE-NOTES"
                    },
                    "module_name": "stat"
                },
                "item": {
                    "ajp_port": 8009,
                    "connector_port": 8080,
                    "name": "tomcat_1",
                    "shutdown_port": 8005
                },
                "stat": {
                    "exists": false
                },
                {
                    "_ansible_item_result": true,
                    "_ansible_no_log": false,
                    "changed": false,
                    "invocation": {
                        "module_args": {
                            "checksum_algorithm": "sha1",
                            "follow": false,
                            "get_checksum": true,
                            "get_md5": true,
                            "mime": false,
                            "path": "/opt/tomcat_3/RELEASE-NOTES"
                        },
                        "module_name": "stat"
                    },
                    "item": {
                        "ajp_port": 8109,
                        "connector_port": 8180,
                        "name": "tomcat_2",
                        "shutdown_port": 8105
                    },
                    "stat": {
                    "exists": false
                }
        }
    ]
    }
}

现在不幸的是我似乎得到了错误

"skip_reason": "Conditional check failed", "skipped": true

我一直在"你用google搜索它"很多次但似乎无法在这里找到解决方案。 Google ansible check if file exists with_items您可能会看到相同的结果。

任何人都知道如何让这个工作?

1 个答案:

答案 0 :(得分:1)

纠正第二个循环:

---
- hosts: localhost
  vars:
    results:
      - item:
          ajp_port: 8009
          connector_port: 8080
          name: tomcat_1
          shutdown_port: 8005
        stat:
          exists: false
      - item:
          ajp_port: 8109
          connector_port: 8180
          name: tomcat_2
          shutdown_port: 8105
        stat:
          exists: false
      - item:
          name: tomcat_exist
        stat:
          exists: true

  tasks:
    - debug:
        msg: "name: {{ item.item.name }}, exists: {{ item.stat.exists }}"
      when: not item.stat.exists
      with_items: "{{ results }}"

因此,在您的设置中,您需要循环status.results并参考item.item.nameitem.stat.exists