0

If i declare a variable inside a Group_vars with a value of 1 or 2 or any integer, then if i use that varible inside a playbook it works with all module (or) place, but not with SERIAL: (this place) which does run the playbook in serial manner. the error occurs is the variable is undefined

I had tried By using the declared variable in the playbook in all places like calling it in a command section or given in a task at all places it calls the value which i declared, But it is not calling the value at SERIAL: (this place) with a declared variable at group_vars with a int value, the error occurs is the variable is undefined

In Hosts [server] 192.168.1.71 192.168.1.72

In Group_vars/server.yml

 ---
 Serial_Value: 1
 become_condition: yes
 become_user_as: root

In playbooks/serial.yml

---
- hosts: server
  serial: "{{ Serial_Value }}"
  become: "{{ become_condition }}"
  become_user: "{{ become_user_as }}"
  gather_facts: no
  tasks:
    - name: task_one
      command: echo "{{ Serial_Value }}" >output3.txt

I need the solution that the Serial_Value should call the value i declared in the group_vars OR I need the reason Why it Cant be done.

swashikan
  • 9
  • 1
  • A play as no clue beforehand of the hosts it has to run on and of the variables it should use. The variables you declare in `group_vars` will not be expanded/read until your play goes down to task level. Just imagine you use `hosts: group1:group2:group3` as a host list, which serial_value should the play use if you declared that in all three groups ? Unfortunatelly, you will have to find an other way to define your serial value. – Zeitounator Apr 28 '19 at 10:32
  • @zeitounator Thanks for the comment, but if it will not read group_vars until my play goes for task level means then how it read for become – swashikan Apr 28 '19 at 11:44
  • You are totally right !! I first thought this was because `serial_value` was encountered first and that the next var would error as well. I guess we would have to dig into ansible code to understand why it happens like this. Meanwhile I'm currious to see which value it would use if you target several hosts in several groups where you definined the same var... and I would definitely not rely on this to develop my playbooks. – Zeitounator Apr 28 '19 at 11:59
  • 2
    This is possible because in Ansible, most vars are lazy evaluated: `become` is probably not "used" until the first task is executed - at which point each server has `become_condition` set. `serial` however is evaluated BEFORE the play starts and therefore before any group_vars or similar are read. – mhutter Apr 28 '19 at 14:12
  • 1
    @swashikan It looks like what you're bumping into is filed as a bug for quite a while: https://github.com/ansible/ansible/issues/33382. Same question has been asked a few minutes ago: https://stackoverflow.com/questions/55891201/ansible-cant-set-variable-for-sequential-execution. – Zeitounator Apr 28 '19 at 15:56
  • 1
    @mhutter we can solve this by expressing the group_vars/serial.yml as vars_files before the (serial :) is given, Like: `vars_files: - /etc/ansible/group_vars/server.yml` before the `serial: "{{ Serial_Value }}"` it reads the required yml and works with the serial but without this i cant get into it . – swashikan Apr 28 '19 at 16:40

1 Answers1

0

You will have to use set_fact module or environment module to assign a value to a variable.

set_fact:
  serial: "{{ Serial_Value }}"
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83