3

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.

Inventory file

[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

Playbook

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

Error

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

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 Version: ansible 2.7.10

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

rolz
  • 591
  • 2
  • 11
  • 23
  • 1
    AFAIK this is a reported bug in https://github.com/ansible/ansible/issues/33382 All the workarounds out here are pretty ugly. – JGK Apr 28 '19 at 15:50
  • You can pass it in as an extra variable: `-e 'app_stack_serial_value=1'` – Jack Apr 29 '19 at 02:41
  • @jack I know this will work perfectly, But I wanted to configure it somewhere in the variable file. I have to define multiple serial values based on the group name. I cant put all those variables as an extra variables. – rolz Apr 29 '19 at 04:58

1 Answers1

0

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)}}"
aloisio
  • 318
  • 4
  • 11