我正在使用geerlingguy's NTP role在一组主机上设置NTP客户端软件。我想将此演示指向一个主机组,并让Ansible即时确定每台计算机是否为VM (这里需要注意的重要一点是,我们所有的VM都运行在KVM虚拟机管理程序上)。如果主机是VM,则应将ntp_tinker_panic设置为true。否则,应将其设置为false。这是我写的剧本:
- name: Set up NTP or chronyd on Linux hosts
hosts: ntp
tasks:
- name: Print the value of ntp_tinker_panic
debug:
var: ntp_tinker_panic
- name: Ensure time sync software is installed and configured
include_role:
name: geerlingguy.ntp
vars:
ntp_enabled: true
ntp_manage_config: true
ntp_timezone: America/New_York
ntp_servers:
- "time.google.com"
ntp_tinker_panic: "{{ ansible_facts['ansible_product_name'] == 'KVM' }}"
tags:
- ntp但是,即使播放执行时没有错误,调试消息也会指出ntp_tinker_panic未定义:
TASK [Print the value of ntp_tinker_panic] *****************************************************************************
ok: [hostname] => {
"ntp_tinker_panic": "VARIABLE IS NOT DEFINED!"
}是否可以在播放运行时按主机设置该变量的值,而不是为虚拟机和物理主机定义单独的主机组?
发布于 2021-01-22 03:30:12
您似乎希望有条件地设置ntp_tinker_panic的值。而您在vars部分中对变量的定义不会将其设置为true。并且它不会考虑ntp组中的每个主机。
如果ansible_product_name与该主机的KVM匹配,则应使用播放中的set_fact任务将此变量设置为true。
示例:
# ntp_tinker_panic not specified here in vars as it is target specific
vars:
ntp_enabled: true
ntp_manage_config: true
ntp_timezone: America/New_York
ntp_servers:
- "time.google.com"
tasks:
- name: Set ntp_tinker_panic for KVM
set_fact:
ntp_tinker_panic: true
when: ansible_product_name == "KVM"
- name: Print the value of ntp_tinker_panic
debug:
var: ntp_tinker_panichttps://stackoverflow.com/questions/65834101
复制相似问题