我写这个剧本是为了检查一些功能:
---
- 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
我有两个问题:
当 playbook 开始运行时,将执行以下代码:
- name: check os debug: msg: > Host {{ ansible_hostname }} does not meet minimal reqs when: ansible_distribution.RedHat is not defined
并且回显消息“主机客户端 2 不满足最低要求”,尽管它不应该因为托管节点是 RedHat。
它会产生以下错误:
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"}
这与变量有关:结果。有任何想法吗 ?