[英]How to run a role in EC2 instance using dynamic inventory in ansible?
我已经创建了一个 ec2 实例,现在当我尝试在该剧本下调用 ansible 中的角色时,尽管有 ec2 实例,这些角色仍会在我的本地机器上运行。
- name: Provision an EC2 Instance
hosts: localhost
connection: local
gather_facts: False
tags: provisioning
vars:
secret_key: "{{ secret_key }}"
access_key: "{{ access_key }}"
region: us-east-1
- hosts: localhost
roles:
- sdirect
我使用了动态库存。 任何人都可以请帮助或提出建议。 谢谢。
这是我用来创建 ec2 实例然后使用ec2.py
动态清单在其上运行我的角色的简单示例:
- name: Provision an EC2 Instance
hosts: localhost
gather_facts: False
tags: provisioning
vars:
secret_key: "{{ secret_key }}"
access_key: "{{ access_key }}"
region: us-east-1
tasks:
- role: create-ec2-role
- name: Refresh the ec2.py cache
shell: ./inventory/ec2.py --refresh-cache # location of your ec2.py inventory
changed_when: no
- name: Refresh inventory
meta: refresh_inventory
# Let suppose you have assign the Name "my_instance" to your Instance
- name: Run tasks on new ec2 instance
hosts: tag_Name_my_instance
# I assume that you have created the ubuntu ec2 instance and ssh key is in your ssh agent
remote_user: ubuntu
roles:
- myrole
我保证您在具有以下文件的 playbook 的同一目录中有一个目录名称inventory
:
.
|-- ec2.ini
|-- ec2.py
`-- hosts
hosts
文件的内容很简单:
[localhost]
127.0.0.1
要运行剧本,只需使用以下命令:
ansible-playbook -i inventory/hosts yourplaybook.yml
希望可以帮到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.