【问题标题】:How to run a role in EC2 instance using dynamic inventory in ansible?如何在 ansible 中使用动态清单在 EC2 实例中运行角色?
【发布时间】:2020-01-20 20:31:27
【问题描述】:

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

我使用了动态库存。任何人都可以请帮助或建议一些东西。 谢谢。

【问题讨论】:

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


    【解决方案1】:

    这是我用来创建 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
    

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

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

    hosts文件的内容很简单:

    [localhost]
    127.0.0.1
    

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

    ansible-playbook -i inventory/hosts yourplaybook.yml
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2019-01-30
      • 1970-01-01
      • 1970-01-01
      • 2020-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多