注册输出 ansible playbook

路德

我有一本剧本可以在服务器列表上运行命令并将结果放入文件中。
但是,有时,由于登录名/密码不正确或无法访问,我无法连接到这些服务器。

当它无法访问时,我在控制台上有这个:

fatal: [fqdn]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname fqdn: Name or service not known", "unreachable": true}

当我的登录名或密码不正确时,我有这个:

fatal: [fqdn]: UNREACHABLE! => {"changed": false, "msg": "Invalid/incorrect password: Permission denied, please try again.", "unreachable": true}

我想在输出控制台中捕获此消息以写入模板 Jinja。
为了有样的报告。

我在剧本中做了这个

- name: Listing des packages
  hosts: all
  gather_facts: false

  tasks:
    - name: Collect only selected facts
      setup:
        gather_subset: min

    - name: Total number of updates
      shell: yum check-update | wc -l
      register: nbupdates

    - name: Output to html file
      template:
        src: ./jinja/src/tpl_dashboard_update.j2
        dest: ./jinja/dst/dashboard_update.html
        force: yes
      delegate_to: localhost
      run_once: true

和 Jinja 模板

{% for host in ansible_play_hosts_all %}
        <tr>
            <td>{{ loop.index }}</td>
            <td>{{ host | upper }}</td>
            {% if hostvars[host]['ansible_distribution'] is not defined %}
                <td colspan="4">Injoignable</td>
            {% else %}
                <td>{{ hostvars[host]['ansible_distribution'] }} {{ hostvars[host]['ansible_distribution_version'] }}</td>
                <td> {% if hostvars[host].nbupdates.stdout_lines.0 is defined %} {{ hostvars[host].nbupdates.stdout_lines.0 }} {% else %} --- {% endif %} </td>
            {% endif %}
        </tr>
{% endfor %}

它无法正常工作,我只有在文件中成功时才有结果,而在失败时没有结果。

当无法访问或出现登录名/密码错误时,如何“捕获”错误消息?

β.εηοιτ.βε

您必须使用ignore_unreachable: yes无法访问的主机来收集结果。
您还必须在setup.
而且,为了克服所有主机都无法访问的可能性,您可能希望在localhost, all不需要时定位并跳过 localhost。

另外两个旁注:

  • 如果存在专用模块,则更喜欢使用它。shell获得所有可用更新的任务会更好地适应yum任务。
  • 这是一个相当沉重的结构:
    {% if hostvars[host].nbupdates.stdout_lines.0 is defined %} 
      {{ hostvars[host].nbupdates.stdout_lines.0 }} 
    {% else %} 
      --- 
    {% endif %}
    
    并且可以使用default过滤器轻松缩短:
    {{ hostvars[host].nbupdates.stdout_lines.0 | default('---') }}
    
    但是,这不是我们应该在这里使用的东西,因为我们应该依赖于yum模块返回的列表的长度

这是一个将所有这些结合在一起的剧本示例:

- hosts: localhost, all
  gather_facts: no

  tasks:
    - setup:
        gather_subset: min
      register: setup
      ignore_unreachable: yes
      when: inventory_hostname != 'localhost'

    - yum:
        list: updates
      register: yum
      when:
        - setup is reachable
        - inventory_hostname != 'localhost'

    - template:
        src: ./jinja/src/tpl_dashboard_update.j2
        dest: ./jinja/dst/dashboard_update.html
        force: yes
      delegate_to: localhost
      run_once: true

这是改编的模板tpl_dashboard_update.j2

<table>
  {% for host in ansible_play_hosts_all if host != 'localhost' %}
    <tr>
      <td>{{ loop.index }}</td>
      <td>{{ host | upper }}</td>
      {% if hostvars[host].setup is unreachable %}
        <td>Injoignable</td>
        <td>{{ hostvars[host].setup.msg }}</td>
      {% else %}
        <td>
          {{ hostvars[host].ansible_distribution }}
          {{ hostvars[host].ansible_distribution_version }}
        </td>
        <td>
          {% if hostvars[host].yum.results is defined %}
            {{ hostvars[host].yum.results | length }}
          {% else %}
            ---
          {% endif %}
        </td>
      {% endif %}
    </tr>
  {% endfor %}
</table>

这一切都会产生一个类似的表格(为便于阅读而添加的样式):

table, th, td {
  border: 1px solid black;
  border-collapse: collapse;
  padding: 4px;
}
<table>
  <tr>
    <td>1</td>
    <td>TEST</td>
    <td>Injoignable</td>
    <td>Failed to connect to the host via ssh: ssh: Could not resolve hostname test: Name does not resolve</td>
  </tr>
  <tr>
    <td>2</td>
    <td>NODE1</td>
    <td>
      Fedora 35
    </td>
    <td>
      29
    </td>
  </tr>
  <tr>
    <td>3</td>
    <td>NODE2</td>
    <td>Injoignable</td>
    <td>Invalid/incorrect password: Permission denied, please try again.</td>
  </tr>
</table>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章