我想根据操作系统版本设置补丁。在 Ansible 版本 2.8 中提出了这个剧本。但它The task includes an option with an undefined variable.在调试行中给出错误消息。
---
- hosts: all
gather_facts: yes
vars:
patch_name_8: 'centos8-updates'
patch_name_7: 'centos7-updates'
tasks:
- name: Set fact for CentOS 7
set_fact:
install_patch_name: "{{ patch_name_7 }}"
when: ansible_distribution_major_version == 7
- name: Set fact for CentOS 8
set_fact:
install_patch_name: "{{ patch_name_8 }}"
when: ansible_distribution_major_version == 8
- name: patch name display
debug:
msg: "install {{ install_patch_name }}"
Run Code Online (Sandbox Code Playgroud)
如何install_patch_name根据操作系统版本设置变量值?
这是错误消息:
TASK [patch name display] ************************************************************************************************************
fatal: [host01]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'install_patch_name' is undefined\n\nThe error appears to be in 't.yaml': line 23, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n
- name: patch name display\n ^ here\n"}
Run Code Online (Sandbox Code Playgroud)
使用
when: ansible_distribution_major_version == "8"
Run Code Online (Sandbox Code Playgroud)
或者
when: ansible_distribution_major_version | int == 8
Run Code Online (Sandbox Code Playgroud)
注意:以下所有示例都是针对 docker 镜像进行的centos:8。
您正在寻找的事实以字符串形式返回:
[root@f6408271fc8c ~]# ansible localhost -m setup -a filter=ansible_distribution_major_version
localhost | SUCCESS => {
"ansible_facts": {
"ansible_distribution_major_version": "8"
},
"changed": false
}
Run Code Online (Sandbox Code Playgroud)
使用比较时变量会保留其类型,并且需要时需要正确转换,如以下剧本所示。
---
- hosts: localhost
tasks:
- name: default compare
debug:
msg: Comparison is true
when: ansible_distribution_major_version == 8
- name: compare as strings
debug:
msg: Comparison is true
when: ansible_distribution_major_version == "8"
- name: compare as ints
debug:
msg: Comparison is true
when: ansible_distribution_major_version | int == 8
Run Code Online (Sandbox Code Playgroud)
这使
[root@f6408271fc8c ~]# ansible-playbook play.yml
PLAY [localhost] **************************************************************************************************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [localhost]
TASK [default compare] ********************************************************************************************************************************************************************************************
skipping: [localhost]
TASK [compare as strings] *****************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "Comparison is true"
}
TASK [compare as ints] ********************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "Comparison is true"
}
PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7173 次 |
| 最近记录: |