简体   繁体   English

Ansible 设置失败的事实

[英]Ansible set fact for a failure

I'm trying to create a custom fact in Ansible for when hosts are unreachable.当主机无法访问时,我正在尝试在 Ansible 中创建一个自定义事实。 If a host is unreachable or has another failure type I'd like to set a fact with a custom dict.如果主机无法访问或有其他故障类型,我想用自定义字典设置一个事实。 I am able to assign the fact for the reachable hosts but not for the unreachable hosts using when statement.我可以使用 when 语句为可访问的主机分配事实,但不能为无法访问的主机分配事实。 Is there way I can set a custom fact on failure?有没有办法在失败时设置自定义事实?

Example Playbook:示例剧本:

---
  - hosts: myhosts
    gather_facts: False

    tasks:
    - name: Get Dict
      shell: "cat /path/dict_file"
      register: result
      ignore_errors: True

    - set_fact:
        result_dict={"cause": "connection timed out"}
      when: result is failed

    - set_fact:
       result_dict="{{ result.stdout }}"
      when: result is success

When I attempt set a fact and assign the result_dict var to a custom dict value I get the below syntax error.当我尝试设置事实并将 result_dict var 分配给自定义字典值时,出现以下语法错误。

Error:错误:

ERROR! Syntax Error while loading YAML.
  expected <block end>, but found '}'

Desired Output for result_dict on unreachable host:无法访问的主机上 result_dict 的期望输出:

ok: [host-b] => {
    "result_dict": {"cause": "connection timed out"}
}

It's possible to use Blocks error handling "to handle errors in a way similar to exceptions in most programming languages" .可以使用块错误处理“以类似于大多数编程语言中的异常的方式处理错误” For example例如

- hosts: myhosts
  gather_facts: false
  tasks:

    - block:
        - command: "head -1 /etc/motd"
          register: result
      rescue:
        - meta: clear_host_errors

    - set_fact:
        result_dict: "{{ result.failed|
                         ternary('cause: connection timed out',
                         result.stdout) }}"
    - debug:
        var: result_dict

gives

ok: [test_01] => {
    "result_dict": "FreeBSD 12.0-RELEASE r341666 GENERIC "
}
ok: [test_02] => {
    "result_dict": "FreeBSD 12.0-RELEASE r341666 GENERIC "
}
ok: [test_03] => {
    "result_dict": "FreeBSD 12.0-RELEASE r341666 GENERIC "
}
ok: [test_04] => {
    "result_dict": "cause: connection timed out"
}

I was able to accomplish this by setting playbook keyword ignore_unreachable to true to allow tasks to continue for unreachable hosts and then using a when statement to assign the timeout dict for unreachable hosts only.我能够通过将 playbook 关键字ignore_unreachable设置为true以允许任务继续无法访问的主机然后使用 when 语句仅为无法访问的主机分配超时字典来完成此操作。 I also needed to correct the format of the timeout dict.我还需要更正超时指令的格式。

When Statements:当语句:

- name: FAILED
  set_fact:
    result_dict:
      cause: 'Timeout'
  when: "'unreachable' in result"

- name: PASSED
  set_fact:
   result_dict="{{ result.stdout | from_json }}"
  when: "'unreachable' not in result"
- debug: var=result_dict

Output:输出:

ok: [localhost] => {
    "my_final_map": {
        "cause": "Timeout", 
        "host_a": [
            {
                "ip-1": {
                    "port": "22", 
                    "service": "ssh"
                }
            }, 
            {
                "ip-2": {
                    "port": "21", 
                    "service": "ftp"
                }
            }
        ]
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM