在角色中访问vars文件中的变量时出错

时间:2019-06-18 17:08:15

标签: ansible

作为对该问题How to read a particular part of file in ansible的跟进。我正在尝试通过使用角色来做同样的事情。变量存储在vars文件中

以下是角色中的vars / main.yml文件

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

这是task / main.yml文件中的代码

 - 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

执行角色的代码 testing.yml

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

我以为我会为add.commands获取添加命令,对其他用户也是如此,但是出现以下错误

"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."

任何人都可以告诉我如何解决此错误以及为什么首先发生这种错误。

1 个答案:

答案 0 :(得分:0)

这与预期的一样,并且错误明确指出了这一点。

另外,

  

变量名称应为字母,数字和下划线。变数   应该始终以字母开头。

因此.不能是变量名的一部分。