剧本(Ansible)语法错误

Playbook (Ansible) syntax error

我是 Ansible(或与此相关的任何脚本语言)的完全 n00b,希望能为我的 syntax/indentation (?) 问题提供一些帮助。

这是我的简单剧本,目标是在 AWS 中创建一个实例并将其添加到安全组。

我对问题出在哪里感到很困惑,因为我从不同的 yaml 检查器收到 2 个不同的错误:

1) Yamllint 告诉我:“():在第 2 行第 3 列解析块映射时没有找到预期的键”

2) Swagger 告诉我:"YAML Syntax Error. Bad indentation of a sequence entry at line 6, column 3: - name: Create a security group ^"(指向第二个“- name”)

有帮助就好了~

干杯~

---
- name: Provision an EC2 Instance and assign an SG
  hosts: local
  connection: local
  gather_facts: False
  - name: Create a security group
      local_action:
          module: ec2_group
          name: test_sg
          region: us-west-2
          rules:
            - proto: tcp
              type: ssh
              from_port: 22
              to_port: 22
              cidr_ip: 0.0.0.0/0
            - proto: tcp
              from_port: 80
              to_port: 80
              cidr_ip: 0.0.0.0/0
          rules_egress:
            - proto: all
              type: all
              cidr_ip: 0.0.0.0/0
  - name: Create new instance
        local_action: ec2
          group= test_sg
          instance_type= t1.micro
          image= ami-9ff7e8af
          wait= true
          region= us-west-2
          keypair= XXX-keypair
          count= 1
      register: ec2

在第 6 行中,您将引入一个新的列表项(破折号)。但是 yaml 解析器不知道如何处理该项目。有两种方法。

  • Swagger 认为它是主列表的一部分,并且与第 2 行处于同一级别。因此它抱怨第 6 行中的缩进不正确。

  • Yamllint 理解它是一个子列表,但它不知道分配子列表的键。

yamllint 很可能是正确的,您错过了一个名为 tasks 的密钥。即使修复了该位,您也会发现还有其他识别问题。下面我粘贴了我理解的正确文本(从 Yaml 的角度来看)

---
- name: Provision an EC2 Instance and assign an SG
  hosts: local
  connection: local
  gather_facts: False
  tasks: 
    - name: Create a security group
      local_action:
          module: ec2_group
          name: test_sg
          region: us-west-2
          rules:
            - proto: tcp
              type: ssh
              from_port: 22
              to_port: 22
              cidr_ip: 0.0.0.0/0
            - proto: tcp
              from_port: 80
              to_port: 80
              cidr_ip: 0.0.0.0/0
          rules_egress:
            - proto: all
              type: all
              cidr_ip: 0.0.0.0/0
    - name: Create new instance
      local_action: ec2
          group= test_sg
          instance_type= t1.micro
          image= ami-9ff7e8af
          wait= true
          region= us-west-2
          keypair= XXX-keypair
          count= 1
      register: ec2