I am setting up a new play in my Ansible playbook that does some operations on a group of machines. These operations require to play in serial mode 1 by 1.

My job is working well for the 1st machine but for the others one it seems that the variables are not valued.

  - name: "My play"  
    hosts: my_hosts_group   
    tags: [test]   
    serial: 1  
    become: yes   
    become_user: root   
        - shell: echo {{  MYVARIBLE.MYSOFTWARE.souche_path }} > /home/USER/test.txt
          register: result
        - debug: var=result

Here is my datamodel :

    souche_path: "/mybinpath/mybin"

For the 1st machine it's working well and I am getting this message :

ok: [host1] => {
    "changed": false, 
    "result": {
        "changed": true, 
        "cmd": "echo my_variable_value > /home/USER/test.txt", 
        "delta": "0:00:00.005541", 
        "end": "2019-04-26 16:27:09.415017", 
        "failed": false, 
        "rc": 0, 
        "start": "2019-04-26 16:27:09.409476", 
        "stderr": "", 
        "stderr_lines": [], 
        "stdout": "", 
        "stdout_lines": []

But fort he others one I got this one :

fatal: [host2]: FAILED! => {}


The task includes an option with an undefined variable. The error was: 'MYVARIABLE' is undefined

The error appears to have been in '/my playbook/install.yml': line 85, column 7, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

    - shell: echo {{  MYVARIBLE }} >  /home/USER/test.txt
      ^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes.  Always quote template expression brackets when they
start a value. For instance:

      - {{ foo }}

Should be written as:

      - "{{ foo }}"

exception type: <class 'ansible.errors.AnsibleUndefinedVariable'>
exception: 'MYVARIBLE' is undefined

I have played it in parallel mode and it's working well. So I am sure that the problem is the variable are only valued for the 1st loop.

Am I missing something ?

0 Answers