Ansible 时间戳在整个播放过程中对所有节点都相同 [英] Ansible timestamp which is the same for all nodes during the whole play

查看:18
本文介绍了Ansible 时间戳在整个播放过程中对所有节点都相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个时间戳变量,它在所有主机上都是相同的.(我将其包含在目录名称中:它必须在所有主机上都相同)

我最初的想法是在 group_vars/all.yml 中添加一个声明,因为具有较高的优先级,应该共享.

group_vars/all.yml:

my_timestamp: "{{ ansible_date_time.date }}{{ ansible_date_time.hour }}{{ ansible_date_time.minute }}{{ ansible_date_time.second }}"

尝试时,主机之间的值不同,导致我的目录结构不一致.我怀疑这是因为变量只是懒惰地评估,当引用的 ansible_date_time 对象在每个主机上已经具有不同的值时,这取决于执行到达变量引用并解析该值的确切时刻.>

当我一开始就尝试在我的剧本中 set_fact 时发生同样的问题.

site.yml:

---# 这个剧本执行部署- 名称:准备剧本设置主持人:全部任务:- 名称:生成我的时间戳设置事实:my_timestamp: "{{ ansible_date_time.date }}{{ ansible_date_time.hour }}{{ ansible_date_time.minute }}{{ ansible_date_time.second }}"

问题是:如何设置一个常量、共享的时间戳,在整个剧本执行期间在所有节点上都相同?clean Ansible 生成一致、恒定时间戳的方法是什么?不作为命令行参数传递,打印到文件并读回值等

我使用的是 Ansible 2.9.23.

解决方案

您的基本问题是,无论是否使用 set_fact,您都在解析每个主机收集的事实中填充的值.由于与目标的 ssh 连接永远不会在完全相同的时间发生,因此每个主机将具有不同的值

解决方案是对所有这些主机使用来自一台主机的一个时间戳.一种方法是从控制器 (localhost) 获取它,但您可以决定在其他地方获取它(即在部署过程中使用 set_facts 和 <代码>run_once 例如).

这是使用控制器的想法.适应自己的需求

---- 名称:确保我们从本地主机收集事实主机:本地主机- 名称:进行我的实际部署主持人:全部变量:# 使用本地主机值声明时间戳我的时间戳:>-{{hostvars['localhost'].ansible_date_time.date}}{{hostvars['localhost'].ansible_date_time.hour}}{{hostvars['localhost'].ansible_date_time.minute}}{{hostvars['localhost'].ansible_date_time.second}}任务:- 名称:确保时间戳值在任何地方都相同调试:变量:my_timestamp#... 剩下的任务...

I would like to have a timestamp variable, which is the same across across all hosts. (I include this in the name of directories: it must be the same on all hosts)

My initial idea was to add a declaration into group_vars/all.yml, since that has a relatively high precedence and should be shared.

group_vars/all.yml:

my_timestamp: "{{ ansible_date_time.date }}{{ ansible_date_time.hour }}{{ ansible_date_time.minute }}{{ ansible_date_time.second }}"

When tried, the values were different across hosts, making my directory structure inconsistent. I suspect this is because the variable is only evaluated lazily, when the referred ansible_date_time object already has a different value on each host, depending on the exact moment execution reaches the variable reference and resolves the value.

The same issue happens when I try to set_fact in my playbook, at the very beginning.

site.yml:

---
# This playbook performs the deployment

- name: Prepare playbook settings
  hosts: all
  tasks:
    - name: Generate my timestamp
      set_fact:
        my_timestamp: "{{ ansible_date_time.date }}{{ ansible_date_time.hour }}{{ ansible_date_time.minute }}{{ ansible_date_time.second }}"

The question is: how could I set a constant, shared timestamp, which is the same on all nodes during the whole execution of a playbook? What would be the clean Ansible way of generating a consistent, constant time-stamp? Without passing as a command line argument, printing to a file and the reading back the value etc.

I am on Ansible 2.9.23.

解决方案

Your base problem is that, using set_fact or not, you are parsing a value that is populated in the gathered facts for each host. Since the ssh connection to your targets will never happen at the exact same time, each host will have different values

The solution is to use one single timestamp from one single host for all of them. One way is to get it from the controller (localhost) but you can decide to get it elsewhere (i.e. the first host in your group during your deploy play using set_facts and run_once for example).

Here is the idea using the controller. Adapt to your own requirements

---
- name: Make sure we gather facts from localhost
  hosts: localhost

- name: Make my actual deployment
  hosts: all

  vars:
    # declare timestamp using localhost values
    my_timestamp: >-
      {{
        hostvars['localhost'].ansible_date_time.date
      }}{{
        hostvars['localhost'].ansible_date_time.hour
      }}{{
        hostvars['localhost'].ansible_date_time.minute
      }}{{
        hostvars['localhost'].ansible_date_time.second
      }}      

  tasks:
    - name: Make sure timestamp value is the same everywhere
      debug:
        var: my_timestamp

    #... rest of your tasks...

这篇关于Ansible 时间戳在整个播放过程中对所有节点都相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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