我使用 "旧的 "EC2 Ansible模块创建了下面的playbook。
- name: Create Ec2 instances hosts: localhost become: False # import the secret file vars_files: - secrets.yml gather_facts: false tasks: # Block is a Group of Tasks combined together - name: Get Info Block block: - name: Get Running instance Info ec2_instance_info: register: ec2info - name: Print info debug: var="ec2info.instances" - name: Create EC2 Block block: - name: Launch ec2 instances tags: create_ec2 region: eu-west-1 key_name: ec2-tutorial group: launch-wizard-1 instance_type: t2.micro image: ami-04dd4500af104442f wait: yes wait_timeout: 500 count: 1 instance_tags: name: appservers os: ubuntu monitoring: no vpc_subnet_id: subnet-049055a2c1633c0eb assign_public_ip: yes aws_access_key: XXXX aws_secret_key: YYYYYYY register: ec2 delegate_to: localhost - name : Add instance to host group add_host: hostname: " {{ item.public_ip }} " groupname: launched loop: " {{ ec2.instances }} " - name: Wait for SSH to come up local_action: module: wait_for host: " {{ item.public_ip }} " port: 22 delay: 10 timeout: 120 loop: " {{ ec2.instances }} " # By specifying never on the tag of this block, # I let this block to run only when explicitely being called tags: [ 'never' , 'ec2-create' ]它工作正常,我能够创建EC2实例,并测试连接,检索其公共IP。
问题是,EC2已经被淘汰了,所以;我使用新的模块
ec2_instance
,重写了同样的游戏手册,它没有返回公共IP。
由于我无法测试连接,所以我临时放了一个
- meta: end_play
,以便我可以部署实例。
这是新的游戏手册
这是我运行playbook得到的错误,注释
- meta: end_play
(这使得playbook继续运行)。
ansible-playbook aws-ec2-creationtst3.yml --connection=local --tags=ec2-create -e "ansible_python_interpreter=/home/marcoreale/aws-venv/bin/python3" --ask-vault-pass -vvvv
TASK [Add instance to host group] **************************************************************************************
task path: /home/xxx/aws-venv/playbook/aws-ec2-create/aws-ec2-creationtst3.yml:70
fatal: [localhost]: FAILED! => {
"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'public_ip'\n\nThe error appears to be in '/home/marcoreale/aws-venv/playbook/aws-ec2-create/aws-ec2-creationtst3.yml': line 70, column 9, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Add instance to host group\n ^ here\n"
注意:用item.private_ip
替换item.public_ip
,但我需要用公共IP来测试连接。
你有什么建议吗?我应该如何改变我的游戏手册以测试使用公共IP的连接?