可能有更好的方法来做到这一点,但到目前为止,我正在使这项工作正常进行,但遇到了问题。我正在尝试执行以下操作:
package_facts并注册包收集包事实这是剧本:
---
- name: Attempt upgrade package
hosts: all
gather_facts: false
tasks:
- name: Get Splunk package
package_facts:
manager: auto
register: package_info
- name: Get SplunkForwarder Package Info
debug:
var: package_info.ansible_facts.packages.splunkforwarder[0].version
register: splunk_current_version
- name: Include variables for new splunk forwarder
set_fact:
splunk_latest_version: "{{ splunkforwarder_latest.split('-')[1] }}"
vars:
splunkforwarder_latest: "splunkforwarder-9.1.1-82c987350fde-linux-2.6-x86_64.rpm"
when: splunk_current_version is defined
- name: Debug latest
debug:
var: splunk_latest_version
- name: Upgrade when latest package is detected
yum:
name: "splunkforwarder-{{ splunk_latest_version }}-linux-2.6-x86_64.rpm"
state: latest
when: splunk_current_version is defined and splunk_current_version is version(splunk_latest_version, '<')
Run Code Online (Sandbox Code Playgroud)
这是我收到的错误消息,指出我正在比较两个字典,这可能是正确的。我对此还很陌生。
致命:[server1]:失败!=> {“msg”:“条件检查‘splunk_current_version > splunk_current_version’失败。错误是:({% if splunk_current_version > splunk_current_version %} True {% else %} False {% endif %}) 上发生意外的模板类型错误: 'dict' 和 'dict' 实例之间不支持 '>'\n\n错误似乎位于 '/home/admin/python-env/ansible/playbooks/uuid.yml':第 33 行,第 7 列,但可能\n位于文件中的其他位置,具体取决于确切的语法问题。\n\n有问题的行似乎是:\n\n\n - name: Upgrade when latest package is detectors.\n ^ here\n"}
我可能可以简单地将包的最新版本传递给变量,方法是包含变量或设置,然后使用最新的包进行 yum 更新。我可能把这个问题复杂化了,但我将不胜感激任何帮助或建议。
调试输出如下:
TASK [Get Splunk package] ******************************************************************************************************************************************************************************************
ok: [server1]
[WARNING]: Platform linux on host server1 is using the discovered Python interpreter at /usr/bin/python3.6, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more information.
ok: [server1]
TASK [Get SplunkForwarder Package Info] ****************************************************************************************************************************************************************************
ok: [server1] => {
"package_info.ansible_facts.packages.splunkforwarder[0].version": "9.0.1"
}
ok: [server1] => {
"package_info.ansible_facts.packages.splunkforwarder[0].version": "9.0.1"
}
TASK [Include variables for new splunk forwarder] ******************************************************************************************************************************************************************
ok: [server1]
ok: [server1]
TASK [Debug latest] ************************************************************************************************************************************************************************************************
ok: [server1] => {
"splunk_latest_version": "9.1.1"
}
ok: [server1] => {
"splunk_latest_version": "9.1.1"
}
TASK [Upgrade when latest package is detected] *********************************************************************************************************************************************************************
fatal: [server1]: FAILED! => {"msg": "The conditional check 'splunk_current_version is defined and splunk_current_version is version(splunk_latest_version, '<')' failed. The error was: Version comparison: '<' not supported between instances of 'str' and 'int'\n\nThe error appears to be in '/home/admin/python-env/ansible/playbooks/uuid.yml': line 36, 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: Upgrade when latest package is detected\n ^ here\n"}
fatal: [server1]: FAILED! => {"msg": "The conditional check 'splunk_current_version is defined and splunk_current_version is version(splunk_latest_version, '<')' failed. The error was: Version comparison: '<' not supported between instances of 'str' and 'int'\n\nThe error appears to be in '/home/admin/python-env/ansible/playbooks/uuid.yml': line 36, 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: Upgrade when latest package is detected\n ^ here\n"}
PLAY RECAP *********************************************************************************************************************************************************************************************************
Run Code Online (Sandbox Code Playgroud)
您要比较的版本不是包事实中给出的原始版本。您需要像提取最新版本一样提取它。
给出以下version_compare.yml测试手册:
---
- name: Compare version demo
hosts: localhost
gather_facts: false
vars:
current_package_version: "{{ ansible_facts.packages[search_package][0].version.split('-') | first }}"
tasks:
- name: This test playbook requires variables search_package and compare_version
ansible.builtin.assert:
that:
- search_package is defined
- compare_version is defined
msg: please define the search_package and compare_version variables
- name: Get package facts
ansible.builtin.package_facts:
- name: Facts for {{ search_package }} must exist to go on
ansible.builtin.assert:
that: ansible_facts.packages[search_package] is defined
msg: Please use in search_package a package name which exist on the sytem
- name: Show raw facts about package {{ search_package }}
ansible.builtin.debug:
var: ansible_facts.packages[search_package]
- name: Show version number we actually want to work with
ansible.builtin.debug:
var: current_package_version
- name: Do something if version is lower than compared one
ansible.builtin.debug:
msg: >-
I would do something since version {{ current_package_version }}
is lower than {{ compare_version }}
when: current_package_version is version(compare_version, '<')
Run Code Online (Sandbox Code Playgroud)
您可以针对系统上安装的任何软件包运行来进行测试。我使用了zenity安装在本地计算机上的软件包。下面运行 2 个示例,针对通过(较低)和失败(大于等于)的情况:
$ ansible-playbook version_compare.yml -e search_package=zenity -e compare_version=4.0.0
PLAY [Compare version demo] *******************************************************************************************************************************************************************************
TASK [This test playbook requires variables search_package and compare_version] ***************************************************************************************************************************
ok: [localhost]
TASK [Get package facts] **********************************************************************************************************************************************************************************
ok: [localhost]
TASK [Facts for zenity must exist to go on] ***************************************************************************************************************************************************************
ok: [localhost]
TASK [Show raw facts about package zenity] ****************************************************************************************************************************************************************
ok: [localhost] => {
"ansible_facts.packages[search_package]": [
{
"arch": "amd64",
"category": "gnome",
"name": "zenity",
"origin": "Ubuntu",
"source": "apt",
"version": "3.42.1-0ubuntu1"
}
]
}
TASK [Show version number we actually want to work with] **************************************************************************************************************************************************
ok: [localhost] => {
"current_package_version": "3.42.1"
}
TASK [Do something if version is lower than compared one] *************************************************************************************************************************************************
ok: [localhost] => {
"msg": "I would do something since version 3.42.1 is lower than 4.0.0"
}
PLAY RECAP ************************************************************************************************************************************************************************************************
localhost : ok=6 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
$ ansible-playbook version_compare.yml -e search_package=zenity -e compare_version=3.42.1
PLAY [Compare version demo] *******************************************************************************************************************************************************************************
TASK [This test playbook requires variables search_package and compare_version] ***************************************************************************************************************************
ok: [localhost]
TASK [Get package facts] **********************************************************************************************************************************************************************************
ok: [localhost]
TASK [Facts for zenity must exist to go on] ***************************************************************************************************************************************************************
ok: [localhost]
TASK [Show raw facts about package zenity] ****************************************************************************************************************************************************************
ok: [localhost] => {
"ansible_facts.packages[search_package]": [
{
"arch": "amd64",
"category": "gnome",
"name": "zenity",
"origin": "Ubuntu",
"source": "apt",
"version": "3.42.1-0ubuntu1"
}
]
}
TASK [Show version number we actually want to work with] **************************************************************************************************************************************************
ok: [localhost] => {
"current_package_version": "3.42.1"
}
TASK [Do something if version is lower than compared one] *************************************************************************************************************************************************
skipping: [localhost]
PLAY RECAP ************************************************************************************************************************************************************************************************
localhost : ok=5 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1339 次 |
| 最近记录: |