0

As a follow up to this question How to read a particular part of file in ansible. I am trying to do the same but by using roles. The variables are stored in vars files

Here is the vars/main.yml file in the role

add:
    commands: []
sub:
    commands: []
multiply: 
    commands: []
div:
    commands: []

Here's the code in tasks/main.yml file

 - name: Getting the Add Commands
   set_fact:
            add.commands: "{{add.commands + [ item ]}}"
   with_lines: "cat {{ {{playbook_dir}}/testing/files/data.txt }}"
   when: item is search('^add')
 - debug:
        var: add.commands
- name: Getting the Sub Commands
   set_fact:
            sub.commands: "{{sub.commands + [ item ]}}"
   with_lines: "cat {{ {{playbook_dir}}/testing/files/data.txt }}"
   when: item is search('^sub')
 - debug:
        var: sub.commands
- name: Getting the Multiply Commands
   set_fact:
            multiply.commands: "{{multiply.commands + [ item ]}}"
   with_lines: "cat {{ {{playbook_dir}}/testing/files/data.txt }}"
   when: item is search('^multiply')
 - debug:
        var: multiply.commands
- name: Getting the Div Commands
   set_fact:
            div.commands: "{{div.commands + [ item ]}}"
   with_lines: "cat {{ {{playbook_dir}}/testing/files/data.txt }}"
   when: item is search('^div')
 - debug:
        var: div.commands

Code to execute the role testing.yml

 - name: Main Program
   hosts: localhost
   roles:
          - testing

I thought that I would get the add commands for add.commands and similarly for others but I am getting the following error

"msg": "The variable name 'add.commands' is not valid. Variables must start with a letter or underscore character, and contain only letters, numbers and underscores."

Can anyone tell me how to troubleshoot this error and why it happened in the first place.

Noobie
  • 55
  • 1
  • 2
  • 7

1 Answers1

-2

This is as expected and the error clearly states that.

Also as per ansible,

Variable names should be letters, numbers, and underscores. Variables should always start with a letter.

So . can't be part of the variable name.

error404
  • 2,684
  • 2
  • 13
  • 21
  • Then Could you tell me how to access the commands variable of the 'add'.I tried using add['commands'] but it is giving me the same error – Noobie Jun 18 '19 at 17:55