gpt4 book ai didi

Ansible 日期变量

转载 作者:行者123 更新时间:2023-12-03 06:21:36 37 4
gpt4 key购买 nike

我正在尝试学习如何使用 Ansible 事实作为变量,但我不明白。当我运行时...

$ ansible localhost -m setup

...它列出了我的系统的所有事实。我随机选择了一个来尝试使用它,ansible_facts.ansible_date_time.date,但我不知道如何使用它。当我运行时...

$ 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
}

所以,它显然就在那里。但是当我运行时...

$ 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

我在这里没有得到什么?如何使用事实作为变量?

最佳答案

命令 ansible localhost -m setup 基本上表示“针对 localhost 运行安装模块”,安装模块会收集您在输出中看到的事实。

当您运行 echo 命令时,这些事实不存在,因为安装模块未运行。测试此类事情的更好方法是使用 ansible-playbook 来运行如下所示的 playbook:

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

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

因为这是作为剧本运行的,所以在运行任务之前会收集本地主机的事实。上述剧本的输出将是这样的:

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

关于Ansible 日期变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31323604/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com