【问题标题】:Create Ec2 instance and install Python package using Ansible Playbook使用 Ansible Playbook 创建 Ec2 实例并安装 Python 包
【发布时间】:2021-10-16 20:38:34
【问题描述】:

我已经创建了一个用于创建 Ec2 实例并安装 python 以通过 ssh 连接服务器的 ansible playbook。

Playbook 成功创建了一个 EC2 实例,但它没有在新创建的 Ec2 实例上安装 python,而是在我的主计算机上安装了 python。

谁能帮我解决这个问题。

我的代码:

- hosts: localhost
  remote_user: ubuntu
  become: yes
  tasks:
- name: I'm going to create a Ec2 instance
  ec2:
    key_name: yahoo
    instance_type: t2.micro
    region: "ap-south-1"
    image: ami-0860c9429baba6ad2
    count: 1
    vpc_subnet_id: subnet-aa84fbe6
    assign_public_ip: yes
  tags:
    - creation

- name: Going to Install Python
  apt:
    name: python
    state: present
  tags:
    - Web

- name: Start the service
  service:
    name: python
    state: started
  tags:
    - start

【问题讨论】:

    标签: ansible


    【解决方案1】:

    the fine manual 中所示,ec2: 操作应与add_host: 结合使用,以便从本地主机“旋转”到新配置的实例,不能将其添加到清单文件中,因为它没有还不存在

    - hosts: localhost
      gather_facts: no
      tasks:
        - name: I'm going to create a Ec2 instance
          ec2:
            ...etc etc
          register: run_instances
        - name: Add new instance to host group
          add_host:
            hostname: "{{ item.public_ip }}"
            groupname: just_launched
          loop: "{{ run_instances.instances }}"
    
        - name: Wait for SSH to come up
          delegate_to: "{{ item.public_ip }}"
          wait_for_connection:
            delay: 60
            timeout: 320
          loop: "{{ run_instances.instances }}"
    
    - hosts: just_launched
      # and now you can do fun things to those ec2 instances
    

    我怀疑你关于“安装 python”的问题是无关的,但以防万一:如果你真的需要将 python 添加到那些 ec2 实例中,你不能使用大多数 ansible 模块来做到这一点,因为它们是用 python 编写的.但这就是 raw: module 旨在解决的问题

    - hosts: just_launched
      # you cannot gather facts without python, either
      gather_facts: no
      tasks:
      - raw: |
          echo watch out for idempotency issues with your playbook using raw
          yum install python
    
      - name: NOW gather the facts
        setup:
    
      - command: echo and now you have working ansible modules again
    

    我还发现您对 service: { name: python, state: started } 的调用很可疑,但我猜由于您的问题,它还没有运行

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多