我正在使用Ansible来启动和configuration当前没有安装Python的AMI(ubuntu / images / hvm-ssd / ubuntu-xenial-16.04-amd64-server-20161020(ami-40d28157))的EC2实例从开始。
由于大多数可用的模块需要远程计算机上的python才能工作,而不是select使用不同的AMI,所以我尝试使用user_data脚本在启动的实例上安装python。
它有点工作(正确安装Python),但是在user_data脚本运行完成之前 , ec2任务将返回,所以下面的任务(实际上将configuration实例的任务)会失败,并显示错误'/bin/sh: 1: /usr/bin/python: not found\r\n'
由于没有python我甚至不能运行远程主机上的大多数任务,我不能简单地检查/usr/bin/python或标记文件是否存在,或者检查syslog输出。
我想在user_data脚本的末尾添加一个nc -l $SOME_PORT ,并用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
这里是你的问题的答案“如何等待用户数据脚本运行时启动EC2实例与ansible? :
- raw: test -f /var/lib/cloud/instance/boot-finished retries: 20 register: cmd_res changed_when: false until: cmd_res | success
这将通过ssh进行连接,并且在主机上的cloud-init模块完成所有模块(包括user_data脚本)之后创build的boot-finished存在时成功。
但我build议使用预先安装的Python创buildAMI,或者使用raw或script Ansible模块来检查/安装Python(这些模块不需要Python)。