本文介绍了如何在Ansible中使用动态清单在EC2实例中运行角色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个ec2实例,现在当我试图在该剧本下调用ansible中的角色时,尽管有ec2实例,但这些角色仍在我的本地计算机上运行.

I have created an ec2 instance and now when I am trying to call a role in ansible under that playbook , the roles runs in my local machine inspite of the ec2 instance .

- 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

我使用了动态库存.任何人都可以请帮助或提出建议.谢谢.

I have used dynamic inventories. Can anyone please help or suggest something.Thanks.

推荐答案

以下是我用来创建ec2实例,然后使用 ec2.py 动态清单在其上运行角色的简单示例:

Here is simple example that I am using to create an ec2 instance and then run my role on it using ec2.py dynamic inventory:

- 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

我保证您在包含以下文件的剧本所在的目录中具有目录名称 inventory :

I assue that you have a directory name inventory in the same directory where you have playbook with the following files:

.
|-- ec2.ini
|-- ec2.py
`-- hosts

hosts 文件的内容很简单:

[localhost]
127.0.0.1

要运行该剧本,只需使用以下命令:

To run the playbook, just use this command:

ansible-playbook -i inventory/hosts yourplaybook.yml

希望对您有帮助

这篇关于如何在Ansible中使用动态清单在EC2实例中运行角色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 18:36