【问题标题】:Ansible : Execute commands on newly created EC2 instanceAnsible : 在新创建的 EC2 实例上执行命令
【发布时间】:2019-05-28 16:41:08
【问题描述】:

我有一个 Ansible 配置,我正在其中创建一个 EC2 实例。实例准备好后,我想禁用定期 apt 更新并等待当前更新过程完成。每当我在 yml 文件中添加配置时,它都会在我的本地系统上执行命令。我做错了什么?

yml 文件:

---
  - name: Provision an EC2 Instance
    hosts: localhost
    connection: local
    gather_facts: False
    tags: provisioning
 tasks:

      - name: Create New security group with below given name
        local_action:
          module: ec2_group
          name: "{{ security_group }}"
          description: Security Group for Newly Created EC2 Instance
          region: "{{ region }}"
          rules:
            - proto: tcp
              from_port: 22
              to_port: 22
              cidr_ip: 0.0.0.0/0
            - proto: tcp
              from_port: 80
              to_port: 80
              cidr_ip: 0.0.0.0/0
          rules_egress:
            - proto: all
              cidr_ip: 0.0.0.0/0


      - name: Launch the new t2 micro EC2 Instance
        local_action: ec2
                      group={{ security_group }}
                      instance_type={{ instance_type}}
                      image={{ image }}
                      wait=true
                      region={{ region }}
                      keypair={{ keypair }}
                      count={{count}}
        register: ec2

现在,在此之后,我等待 ssh 完成并希望在新创建的 Ec2 实例上传递以下命令:

- name: Disable timers for unattended upgrade, so that none will be triggered by the `date -s` call.
  raw: systemctl disable --now {{item}}
  with_items:
    - 'apt-daily.timer'
    - 'apt-daily-upgrade.timer'

- name: Reload systemctl daemon to apply the new changes
  raw: systemctl daemon-reload

- name: Purge autoupdate
  raw: apt -y purge unattended-upgrades    

- name: Update apt cache
  raw: apt -y update

但是将它们添加为 raw 不起作用,甚至将它们添加为命令。

【问题讨论】:

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


    【解决方案1】:

    您发布的第一部分代码是通过从本地系统调用 AWS API 来预置一个新的 EC2 实例:

      - name: Provision an EC2 Instance
        hosts: localhost
        connection: local
        gather_facts: False
    ...
     - name: Create New security group with below given name
       local_action:
         module: ec2_group
    

    注意local_action 部分指定在本地运行操作。另外,您的目标是localhost

    如果您想随后配置新系统,您可以将其添加到主机组并运行一些配置步骤。例如,在Provision an EC2 Instance 步骤之后添加以下内容,以将新实例的公共 IP 添加到名为 ec2hosts 的主机组:

       - name: Add instance public IP to host group
         add_host: hostname={{ item.public_ip }} groups=ec2hosts
         loop: "{{ ec2.instances }}"
    

    现在您可以通过定位主机组来配置主机:

    - hosts: ec2hosts
      name: configuration play
      user: ec2-user
      gather_facts: true
      tasks:
      - name:  Disable timers for unattended upgrade, so that none will be triggered by the `date -s` call.
        raw: systemctl disable --now {{item}}
        with_items:
          - 'apt-daily.timer'
          - 'apt-daily-upgrade.timer'
    
      - name: Reload systemctl daemon to apply the new changes
        raw: systemctl daemon-reload
    
      - name: Purge autoupdate
        raw: apt -y purge unattended-upgrades    
    
      - name: Update apt cache
        raw: apt -y update
    

    总而言之,您首先从本地系统创建实例,等待它启动,将其 IP 地址添加到主机组,然后通过对该主机组运行 ansible 来运行其他配置步骤。为此,请确保使用已将私钥添加到 SSH 代理的 SSH 密钥对。此外,请确保在公有子网中启动 EC2 实例。

    请参阅Ansible Amazon Web Service Guide

    【讨论】:

    • 我会试试这个。谢谢。 :-)
    • 这非常有效。谢谢你。澄清一下,我需要在本地 ssh 代理中添加 ssh 密钥对,对吧?
    • 是的。当新实例的 SSH 服务器提示时,您的本地代理将使用私钥。
    猜你喜欢
    • 2016-12-09
    • 2013-03-08
    • 2021-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-07
    相关资源
    最近更新 更多