如何使用ansible中的动态库存在EC2实例中运行角色?

时间:2017-10-31 13:20:08

标签: amazon-web-services amazon-ec2 ansible ansible-inventory

我创建了一个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

我使用过动态库存。任何人都可以帮助或建议一些事情。 感谢。

1 个答案:

答案 0 :(得分:2)

以下是我用来创建ec2实例然后使用Object.keys(globals).forEach((key) => I.click(globals[key].xpath)); 动态广告资源在其上运行我的角色的简单示例:

Object.keys(globals).forEach((key) => {
    if(key != "u5boys" && key != "u6boys")
        return;

    console.log(globals[key].xpath);
})

我确定您在具有以下文件的playbook所在的目录中有一个目录名ec2.py

- 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
  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
- 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文件的内容很简单:

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

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

hosts

希望可以帮到你