You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Ansible Playbook创建EC2实例时遇语法错误求助

问题解决步骤

1. 修复核心语法错误

Ansible的tasks是列表类型,每个任务必须以-开头。你的代码里ec2_instance:前面缺少了-,这是触发语法报错的直接原因。

2. 修正模块名称(推荐)

注释掉的amazon.aws.ec2_instance是AWS官方维护的模块,建议启用它(去掉注释),避免旧版ec2_instance模块的兼容性问题。

3. 修复参数拼写错误

secutiry_group是拼写错误,正确参数名应为security_group。

修正后的完整Playbook

- name: EC2 Creation
  hosts: localhost
  tasks:
    - name: Launch EC2 instance
      amazon.aws.ec2_instance:
        region: ap-south-1
        instance_type: t2.micro
        image_id: ami-022ce6f32988af5fa
        count: 1
        wait: yes
        tags:
          name: Test
        vpc_subnet_id: subnet-078e0bc5c6d3d2d66
        network:
          assign_public_ip: true
        key_name: linux
        security_group: ALL_TCP

额外说明

  • 关于Found both group and host with same name: localhost警告:这是因为你的inventory中同时存在名为localhost的组和主机,不影响功能。若要消除警告,可在inventory中调整名称,或在playbook里添加connection: local。
  • 若未安装amazon.aws集合,执行命令安装:ansible-galaxy collection install amazon.aws

内容的提问来源于stack exchange,提问作者user2842413

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 01:03:14