我正在使用Ansible来启动和配置当前基于(ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-20161020 ( AMI -40d28157)的EC2实例,这些实例从一开始就没有安装。
由于大多数ansible模块需要远程机器上的python才能工作,所以我尝试使用user_data脚本在初始实例上安装python,而不是选择使用不同的AMI。
它有点工作(Python安装正确),但是ansible ec2任务在user_data脚本运行完成之前返回,因此下面的任务(那些实际配置实例的任务)在错误'/bin/sh: 1: /usr/bin/python: not found\r\n'
中失败
由于没有python,我甚至无法在远程主机上运行大多数任务,因此不能简单地检查/usr/bin/python
或标记文件是否存在,或者检查syslog输出。
我考虑在nc -l $SOME_PORT
脚本的末尾添加一个user_data,并使用wait_for
模块检查它,但对我来说,这听起来太麻烦了。
是否有一种方法可以使用ec2模块启动一个实例,以便它等待直到user_data脚本运行完毕?
供参考:
一本与我所用的类似的剧本:
- name: Create the EC2 instance
hosts: localhost
gather_facts: false
vars:
name: myinstance
ami: ami-40d28157
#Other vars ommited for brevity
tasks:
- name: Launch Instance
ec2:
key_name: "{{ keypair }}"
group: "{{ security_group }}"
instance_type: "{{ instance_type }}"
image: "{{ ami }}"
user_data: "{{ lookup('file', 'user_data.sh') }}"
wait: true
region: "{{ region }}"
vpc_subnet_id: "{{ subnet }}"
assign_public_ip: no
zone: "{{ zone }}"
instance_tags: "{{ tag_map }}"
exact_count: 1
count_tag:
Name: "{{ name }}"
register: ec2
- name: Add new Instance to host group
add_host: name={{ item.private_ip }} groups={{ name }}
with_items: "{{ ec2.instances }}"
- name: Wait for SSH to come up
wait_for: host={{ item.private_ip }} port=22 delay=60 timeout=320 state=started
with_items: "{{ ec2.instances }}"
- name: Configure instance(s)
hosts: myinstance
tasks:
- name: Example task
command: touch a_file
user_data.sh脚本:
#!/bin/bash
set -e -x
export DEBIAN_FRONTEND=noninteractive
sudo locale-gen pt_BR.UTF-8
sudo apt-get update && apt-get upgrade -y
sudo apt-get install -y python-minimal
发布于 2020-06-11 03:32:21
因此,您可以使用cloud-init
命令,即:
cloud-init status --wait
因此,您应该将下面的片段添加到您的剧本中:
- name: Wait for cloud-init / user-data to finish
command: cloud-init status --wait
changed_when: false
与第一个答案相比,它看上去更干净一些。
https://serverfault.com/questions/855872
复制