[英]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.