字典里面的字典在Ansible

时间:2015-03-24 16:55:19

标签: ansible ansible-playbook

我有以下Ansible playbook文件:

  1 ---                                                                                                                                           
  2 -   name: Provision cluster                                                                                                
  3     connection: local                                                                                                                         
  4     hosts: localhost                                                                                                                          
  5     gather_facts: False                                                                                                                       
  6     vars_files:                                                                                                                               
  7     -   ../vars_files/credentials/foo.yml                                                                                         
  8     -   ../vars_files/credentials/bar.yml                                                                                          
  9                                                                                                                                               
 10     vars:                                                                                                                                     
 11       - ec2_hosts:                                                                                                                            
 12         - node1:                                                                                                                              
 13           - address: 1.1.1.1                                                                                                               
 14         - node2:                                                                                                                              
 15           - address: 1.1.1.2                                                                                                               
 16         - node3:                                                                                                                              
 17           - address: 1.1.1.3                                                                                                              
 18         - node4:                                                                                                                              
 19           - address: 1.1.1.4

我想要做的是暗示在另一本字典中有一本字典。在第二个我要在主机名和它的IP地址之间声明一个字典。但是我收到以下错误:

ERROR: Syntax Error while loading YAML script, provision.yml
Note: The error may actually appear before this position: line 10, column 1


        vars:

1 个答案:

答案 0 :(得分:3)

我个人更喜欢在定义复杂变量时使用JSON(或Python'ish)表达式,因为我发现与YAML语法中定义的复杂变量相比,它们更具可读性。 E.g:

vars:
  ec2_hosts: {
      node1: {
          address: 1.1.1.1,
          hostname: ec2_node1
      },
      node2: {
          address: 1.1.1.2,
          hostname: ec2_node2
      },
    },

如果你必须使用YAML,希望它实际上是你想要的结构,它与前一个结构不同:

vars:
  - ec2_hosts:
    - host1:
      - address: 1.1.1.1
    - host2:
      - address: 1.1.1.2

以下是测试代码,以防出现其他一些语法错误:

- name: Provision cluster
  connection: local
  hosts: localhost
  gather_facts: False
  # vars_files:
  # - ../vars_files/credentials/foo.yml
  # - ../vars_files/credentials/bar.yml
  vars:
    - ec2_hosts:
      - host1:
        - address: 1.1.1.1
      - host2:
        - address: 1.1.1.2
  tasks:
  - debug: msg="{{ec2_hosts}}"

PS:我从你的帖子中复制了大量的尾随空格,希望它们不存在于你的实际剧本YAML中。