gpt4 book ai didi

amazon-web-services - 安塞 bool : Execute commands on newly created EC2 instance

转载 作者:行者123 更新时间:2023-12-02 19:58:51 25 4
gpt4 key购买 nike

我有一个 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

但是将它们添加为原始数据不起作用,甚至将它们添加为命令也不起作用。

最佳答案

您发布的代码的第一部分是通过从本地系统调用 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

如果您随后想要配置新系统,可以将其添加到主机组并运行一些配置步骤。例如,在配置 EC2 实例步骤之后添加以下内容,以将新实例的公共(public) 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 key 对。另外,请确保在公有子网中启动 EC2 实例。

请参阅Ansible Amazon Web Service Guide .

关于amazon-web-services - 安塞 bool : Execute commands on newly created EC2 instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56346730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com