相关文章推荐
玩足球的烈马  ·  Functional analysis ...·  4 月前    · 
冷冷的夕阳  ·  sum、max 和 min ...·  6 月前    · 
爱看球的围巾  ·  zmq使用-掘金·  10 月前    · 
逆袭的沙滩裤  ·  BottomNavigationView和A ...·  11 月前    · 

ansible模块amazon.aws.ec2_instance - 无法获得公共IP

1 人关注

我使用 "旧的 "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 ,以便我可以部署实例。
这是新的游戏手册

- 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" # By specifying always on the tag, # # I let this block to run all the time by module_default # # this is for security to net create ec2 instances accidentally tags: [ 'always' , 'getinfoonly' ] - name: Create EC2 Block block: - name: Launch ec2 instances tags: create_ec2 ec2_instance: name: "Test new Ansible ec2 module" region: eu-west-1 #Availability zone is mutually excluded with vpc_subnet_id #availability_zone: eu-west-1c #key_name: ec2-tutorial key_name: testec2key security_group: launch-wizard-1 instance_type: t2.micro image_id: ami-04dd4500af104442f wait: yes volumes: - device_name: /dev/sdb volume_size: 8 delete_on_termination: true #- device_name: /dev/sdb # volume_type: gp2 # volume_size: 5 # delete_on_termination: true wait_timeout: 500 # count not available is this new module #count: 1 tags: name: "Test" os: AwsAmi #vpc_subnet_id: subnet-049055a2c1633c0eb vpc_subnet_id: subnet-08a394b8718bbff45 network: assign_public_ip: true #termination_protection: yes #aws_access_key: XXXXXXX #aws_secret_key: YYYYYYYYYYY register: ec2 delegate_to: localhost #- meta: end_play - 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 }} " 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' ]

这是我运行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的连接?

amazon-ec2
module
ansible
mlist
mlist
发布于 2021-12-30
2 个回答
β.εηοιτ.βε
β.εηοιτ.βε
发布于 2022-02-26
0 人赞同

文档中指出,公共IP地址现在是通过 public_ip_address 属性嵌套。

所以,你的任务应该可以用

- name: Add instance to host group
  add_host:
    hostname: "{{ item.public_ip_address }}"