Ansible 检查 - 操作系统版本和复制模块

Ansible 检查 - 操作系统版本和复制模块

我编写了这个剧本是为了检查一些功能:

---
- name: install packages
  hosts: web
  remote_user: ansibleuser
  become: yes
  vars_files: vars/packages2

  tasks:
  - name: install packages
    yum:
      name: "{{ item.name }}"
      state: "{{ item.state }}"
    with_items: "{{ packages }}"
    when: (ansible_facts['os_family'] == "RedHat" and ansible_facts['distribution_major_version'] == 8)

  - name: start and enable services
    service:
      name: "{{ item.name }}"
      state: "{{ item.state }}"
      enabled: "{{ item.enabled }}"
    loop: "{{ services }}"

  - name: check os
    debug:
      msg: >
        Host {{ ansible_hostname }} does not meet minimal reqs
      when: ansible_distribution.RedHat is not defined

  - name: write to a file
    copy:
      content: This is a TEST FILE
      dest: index.html
  - name: copy to webserver
    copy:
      src: index.html
      dest: /var/www/html/index.html
      register: results
  - name: report an error on file copy
    fail:
      msg: "The html page is not copied!"
    when: results.rc !=0

我有两个问题:

  1. 当剧本开始运行时,将执行以下代码:

     - name: check os
     debug:
       msg: >
         Host {{ ansible_hostname }} does not meet minimal reqs
       when: ansible_distribution.RedHat is not defined
    

和消息“主机客户端 2 不满足最低要求”被回显,但是由于管理节点是 RedHat,因此不应该被回显。

  1. 它会产生以下错误:

        TASK [report an error on file copy] 
       fatal: [client2]: FAILED! => {"msg": "The conditional check 
       'results.rc !=0' failed. The error was: 
       error while evaluating conditional (results.rc !=0): 'results' is 
       undefined\n\nThe error appears to 
        be in '/home/ansibleuser/base/play9loop.yml': line 40, column 5, but 
          may\nbe elsewhere in the file 
       depending on the exact syntax problem.\n\nThe offending line appears 
    to be:\n\n      register: 
      results\n  - name: report an error on file copy\n    ^ here\n"}
    

与变量相关:结果。 有任何想法吗 ?

答案1

查看建议的更改和代码审查意见。

---
# Play names should reflect what it really does, not just one generic task name
- name: web server
  hosts: web
  remote_user: ansibleuser
  become: yes
  vars_files: vars/packages2

  tasks:
  - name: Check os is EL8
    assert:
      that:
      # Careful with os_family, Fedora is also "RedHat" but a much larger major version number
      - ansible_facts['os_family'] == "RedHat"
      - ansible_facts['distribution_major_version'] == 8
      fail_msg: Host {{ ansible_hostname }} does not meet minimal reqs
  
  - name: install packages
    # Using the generic package module wrapper allows for other OSes to be used
    #   although it does not translate package names
    package:
      # package transactions are more efficient when a list is passed to name
      # Rewrote loop to filter remove and install into two transactions
      #   TODO: Simplify.  Currently this requires every package to have "name" and "state" attributes 
      name: "{{ packages | selectattr('state', 'equalto', item) | map(attribute='name')  }}"
      state: "{{ item }}"
    loop:
    - absent
    - present
    # Deleted the when on package install:
    #  1. unnecessary with earlier assert
    #  2. RedHat is not defined is not the correct test

  - name: start and enable services
    service:
      name: "{{ item.name }}"
      state: "{{ item.state }}"
      enabled: "{{ item.enabled }}"
    loop: "{{ services }}"

 
  - name: copy to webserver
    copy:
      # A temporary file task is not needed for copy: content
      #  indeed, that is the entire point of the weird content parameter
      content: This is a TEST FILE
      dest: /var/www/html/index.html
      register: results
  
  # Extra reporting of file copy error is not necessary
  # Module failure will automatically be reported with a verbose message
  # For checking failure, use the test:
  # {{ results is failed }}

相关内容