Ansible:无法为顺序执行设置变量 [英] Ansible: Can't set variable for sequential execution

查看:30
本文介绍了Ansible:无法为顺序执行设置变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 serial 元素构建一个具有滚动更新用例(顺序执行)的剧本.由于我必须在剧本中的多个地方使用序列值,我想将其用作变量,该变量可用于将某处定义为组变量.

I'm trying to build a playbook with rolling update use case (Sequential Execution) with the serial element. Since I have to use the serial value in multiple places in the playbook, I wanted to use it as a variable which can be used to define somewhere as a group variable.

库存文件

[all]
webserver1  ansible_host=10.1.1.1 ansible_user=root
webserver2  ansible_host=10.1.1.2 ansible_user=root
webserver3  ansible_host=10.1.1.3 ansible_user=root
dbserver1  ansible_host=10.1.2.1 ansible_user=root

[webserver]
webserver1
webserver2
webserver3

[dbserver]
dbserver1

[app-stack:children]
webservers
dbservers

[app-stack:vars]
app_stack_serial_value=1

剧本

- hosts: app-stack
  serial: "{{ app_stack_serial_value }}"
  tasks:
   - name: debug
     debug:
       msg: "Ansible Host - {{ ansible_host }}"

错误

错误!字段serial"具有无效值,其中包括未定义的变量.错误是:'app_stack_serial_value' 未定义

ERROR! The field 'serial' has an invalid value, which includes an undefined variable. The error was: 'app_stack_serial_value' is undefined

如果我在串行字段中指定值,例如 serial: 1,这将起作用.但我想将此值用作变量.有什么想法吗??

This will work if I specify the value in the serial field like serial: 1. But I wanted to use this value as a variable. Any Idea ??

Ansible 版本:ansible 2.7.10

Ansible Version: ansible 2.7.10

python 版本 = 3.7.2(默认,2019 年 1 月 13 日,12:50:01)[Clang 10.0.0 (clang-1000.11.45.5)]

python version = 3.7.2 (default, Jan 13 2019, 12:50:01) [Clang 10.0.0 (clang-1000.11.45.5)]

推荐答案

据我所知,不能在组变量中设置.但是您可以在运行此剧本之前尝试设置 ENV SERIAL_NUMBER=1 并设置串行参数,如:

As far as I know, you cannot set it in the group variables. But you can try set ENV SERIAL_NUMBER=1 before run this playbook and set the serial parameter like:

  serial: "{{lookup('env','SERIAL_NUMBER')|default(3, true)}}"

这篇关于Ansible:无法为顺序执行设置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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