从剧本内部检查Ansible版本 [英] Check Ansible version from inside of a playbook

查看:87
本文介绍了从剧本内部检查Ansible版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一本在Ansible 1.9.x和2.0中以不同方式运行的剧本.我想检查剧本中当前正在运行的ansible版本,以避免有人使用旧版本运行它.

I have a playbook that is running in different way in Ansible 1.9.x and 2.0. I would like to check currently running ansible version in my playbook to avoid someone running it with old one.

我认为这不是最好的解决方案:

I don't think that this is the best solution:

- local_action: command ansible --version
  register: version

您有什么建议?

推荐答案

Ansible提供了一个称为ansible_version的全局dict,该dict包含以下内容

Ansible provides a global dict called ansible_version, dict contains the following

"ansible_version": {
        "full": "2.7.4", 
        "major": 2, 
        "minor": 7, 
        "revision": 4, 
        "string": "2.7.4"
    }

您可以在创建条件语句时使用以下任何ansible_version.full,ansible_version.major或任何其他组合来检查已安装的ansible版本.

you can use any of the following ansible_version.full, ansible_version.major or any other combination in creating conditional statements to check the version of ansible that's installed.

示例剧本:使用此字典和when语句.

example playbook: using this dict and a when statement.

---
- hosts: localhost
  tasks:

    - name: Print message if ansible version is greater than 2.7.0
      debug:
        msg: "Ansible version is  {{ ansible_version.full }}"
      when: ansible_version.full >= "2.7.4"

这篇关于从剧本内部检查Ansible版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆