日期日期变量 [英] Ansible date variable

查看:176
本文介绍了日期日期变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习如何将Ansible事实用作变量,但我不明白.当我跑步...

I'm trying to learn how to use Ansible facts as variables, and I don't get it. When I run...

$ ansible localhost -m setup

...它列出了我系统的所有事实.我随机选择了一个尝试并使用它,ansible_facts.ansible_date_time.date,但是我不知道如何使用它.当我跑步...

...it lists all of the facts of my system. I selected one at random to try and use it, ansible_facts.ansible_date_time.date, but I can't figure out HOW to use it. When I run...

$ ansible localhost -m setup -a "filter=ansible_date_time"
localhost | success >> {
    "ansible_facts": {
        "ansible_date_time": {
            "date": "2015-07-09",
            "day": "09",
            "epoch": "1436460014",
            "hour": "10",
            "iso8601": "2015-07-09T16:40:14Z",
            "iso8601_micro": "2015-07-09T16:40:14.795637Z",
            "minute": "40",
            "month": "07",
            "second": "14",
            "time": "10:40:14",
            "tz": "MDT",
            "tz_offset": "-0600",
            "weekday": "Thursday",
            "year": "2015"
        }
    },
    "changed": false
}

那么,它就在那附近.但是当我跑步...

So, it's CLEARLY there. But when I run...

$ ansible localhost -a "echo {{ ansible_facts.ansible_date_time.date }}"
localhost | FAILED => One or more undefined variables: 'ansible_facts' is undefined

$ ansible localhost -a "echo {{ ansible_date_time.date }}"
localhost | FAILED => One or more undefined variables: 'ansible_date_time' is undefined

$ ansible localhost -a "echo {{ date }}"
localhost | FAILED => One or more undefined variables: 'date' is undefined

我什么都不来?如何使用事实作为变量?

What am I not getting here? How do I use Facts as variables?

推荐答案

命令ansible localhost -m setup基本上说对本地主机运行安装程序模块",安装程序模块收集您在输出中看到的事实.

The command ansible localhost -m setup basically says "run the setup module against localhost", and the setup module gathers the facts that you see in the output.

当您运行echo命令时,这些事实不存在,因为未运行安装模块.测试此类事情的更好方法是使用ansible-playbook来运行看起来像这样的剧本:

When you run the echo command these facts don't exist since the setup module wasn't run. A better method to testing things like this would be to use ansible-playbook to run a playbook that looks something like this:

- hosts: localhost
  tasks:
      - debug: var=ansible_date_time

      - debug: msg="the current date is {{ ansible_date_time.date }}"

因为这是作为剧本运行的,所以在运行任务之前会先收集localhost的事实.上面的剧本的输出将是这样的:

Because this runs as a playbook facts for localhost are gathered before the tasks are run. The output of the above playbook will be something like this:

PLAY [localhost] **************************************************

GATHERING FACTS ***************************************************************
ok: [localhost]

TASK: [debug var=ansible_date_time] *******************************************
ok: [localhost] => {
    "ansible_date_time": {
        "date": "2015-07-09",
        "day": "09",
        "epoch": "1436461166",
        "hour": "16",
        "iso8601": "2015-07-09T16:59:26Z",
        "iso8601_micro": "2015-07-09T16:59:26.896629Z",
        "minute": "59",
        "month": "07",
        "second": "26",
        "time": "16:59:26",
        "tz": "UTC",
        "tz_offset": "+0000",
        "weekday": "Thursday",
        "year": "2015"
    }
}

TASK: [debug msg="the current date is {{ ansible_date_time.date }}"] **********
ok: [localhost] => {
    "msg": "the current date is 2015-07-09"
}

PLAY RECAP ********************************************************************
localhost      : ok=3    changed=0    unreachable=0    failed=0

这篇关于日期日期变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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