AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1054110
Accepted
VorX
VorX
Asked: 2021-02-19 04:01:51 +0800 CST2021-02-19 04:01:51 +0800 CST 2021-02-19 04:01:51 +0800 CST

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

  • 772

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

---
- 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. 当 playbook 开始运行时,将执行以下代码:

     - 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"}
    

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

ansible
  • 1 1 个回答
  • 1818 Views

1 个回答

  • Voted
  1. Best Answer
    John Mahowald
    2021-02-19T07:48:40+08:002021-02-19T07:48:40+08:00

    查看建议的更改和代码审查评论。

    ---
    # 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 }}
    
    • 0

相关问题

  • 重复的 Ansible 任务

  • 无法形成站点中的文件的链接,该链接可用于使用 ansible 在远程服务器中启用的目录站点?

  • 如何执行 ansible 的特定角色?

  • Ansible 和 rbash

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve