繁体   English   中英

在 centos 7 到 ansible 剧本上创建一个 aws ec2 实例

[英]create an aws ec2 instance on centos 7 through ansible playbook

我创建了一个剧本aws.yml并希望在我的本地主机上运行它

---
-  hosts: webserver

-  vars:
      region: ap-south-1
      instance_type: t2.micro
      ami: ami-005956c5f0f757d37  # Amazon linux LTS
      keypair: ansible # pem file name

-  tasks:

    -  ec2:
         key_name: "{{ ansible }}"
         group: ansible  # security group name
         instance_type: "{{ t2.micro }}"
         image: "{{ ami-005956c5f0f757d37 }}"
         wait: true
         wait_timeout: 500
         region: "{{ ap-south-1 }}"
         count: 1  # default
         count_tag:
            Name: Prod-Instance
         instance_tags:
            Name: Prod-Instance
         vpc_subnet_id: subnet-00efd068
         assign_public_ip: yes

主机文件/etc/ansible/hosts的内容

[web]
localhost

当我尝试运行我的aws.yml时,它给出了以下错误

[root@localhost ~]# ansible-playbook  aws.yml

PLAY [web] ********************************************************************************************************************************************************************************************************
ERROR! the field 'hosts' is required but was not set
[root@localhost ~]# 

您的剧本和您的主机组名称不匹配。

aws.yml

- hosts: webserver

/etc/ansible/hosts

[web]

您应该更改您的aws.yml剧本以阅读

- hosts: web

您的主机文件/etc/ansible/hosts读取

[webserver]

太多- s。 删除tasksvars前面的那些。

此外,在ec2任务上使用delegate_to: localhost

---
- hosts: webserver
  vars:
      region: ap-south-1
      instance_type: t2.micro
      ami: ami-005956c5f0f757d37  # Amazon linux LTS
      keypair: ansible # pem file name
  tasks:
  - ec2:
      key_name: "{{ ansible }}"
      group: ansible  # security group name
      instance_type: "{{ t2.micro }}"
      image: "{{ ami-005956c5f0f757d37 }}"
      wait: true
      wait_timeout: 500
      region: "{{ ap-south-1 }}"
      count: 1  # default
      count_tag:
        Name: Prod-Instance
      instance_tags:
        Name: Prod-Instance
      vpc_subnet_id: subnet-00efd068
      assign_public_ip: yes
    delegate_to: localhost

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM