首页 文章

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

提问于
浏览
0

我创建了一个ec2实例,现在当我尝试在该playbook下调用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 回答

  • 2

    这是我用来创建ec2实例然后使用 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
    

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

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

    hosts 文件的内容很简单:

    [localhost]
    127.0.0.1
    

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

    ansible-playbook -i inventory/hosts yourplaybook.yml
    

    希望这对你有所帮助

相关问题