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

问题描述

我已经创建了一个 ec2 实例,现在当我尝试在该剧本下调用 ansible 中的角色时,尽管有 ec2 实例,角色仍然在我的本地机器上运行.

- name:配置 EC2 实例主机:本地主机连接:本地收集事实:错误标签: 供应变量:secret_key: "{{ secret_key }}"access_key: "{{ access_key }}"地区:us-east-1- 主机:本地主机角色:- 直接

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

解决方案

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

- name:配置 EC2 实例主机:本地主机收集事实:错误标签: 供应变量:secret_key: "{{ secret_key }}"access_key: "{{ access_key }}"地区:us-east-1任务:- 角色:create-ec2-role- 名称:刷新 ec2.py 缓存shell: ./inventory/ec2.py --refresh-cache # ec2.py 库存的位置changed_when:否- 名称:刷新库存元:refresh_inventory# 假设您已将名称my_instance"分配给您的实例- 名称:在新的 ec2 实例上运行任务主机:tag_Name_my_instance# 我假设您已经创建了 ubuntu ec2 实例并且 ssh 密钥在您的 ssh 代理中远程用户:ubuntu角色:- 我的角色

我保证你有一个目录名称 inventory 在你有以下文件的 playbook 的同一目录中:

.|-- ec2.ini|-- ec2.py`-- 主机

hosts 文件的内容很简单:

[本地主机]127.0.0.1

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

ansible-playbook -i inventory/hosts yourplaybook.yml

希望能帮到你

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.

解决方案

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

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

The content of the hosts file is simple:

[localhost]
127.0.0.1

To run the playbook, just use this command:

ansible-playbook -i inventory/hosts yourplaybook.yml

Hope that might help you

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

10-20 18:37