TypeError:“ NoneType”对象在Ansible中无法下标

时间:2019-08-31 14:36:05

标签: python amazon-web-services ansible

这是我的 playbook.yml 呼叫角色

playbook.yml

---
- hosts: localhost
  name: ec2 launcher
  connection: local
  roles:
       - {role: aws-vpc}

我的角色 aws-vpc 包含任务,vars目录

main.yml 在任务中

---
# tasks file for aws-vpc


- name: create VPC
  ec2_vpc_net:
    name: "{{ vpc_name }}"
    cidr_block: "{{ vpc_cidr }}"
    region: "{{ region }}"
    state: present
    aws_access_key: "{{ aws_access_key }}"
    aws_secret_key: "{{ aws_secret_key }}"
  register: vpc

- name: print vpc details
  debug:
        var: vpc

- name: create public subnet2
  ec2_vpc_subnet:
    state: present
    vpc_id: "{{ vpc.vpc.id }} "
    cidr: "{{ subnet2_cidr }}"
    az: "{{ az2 }}"
    aws_access_key: "{{ aws_access_key }}"
    aws_secret_key: "{{ aws_secret_key }}"
    map_public: yes
    resource_tags:
       Name: "{{ subnet2_name }}"
  register: subnet2

- name: print subnet2 details
  debug:
        var: subnet2

main.yml in vars


# vars file for aws-vpc

aws_access_key: "..........."
aws_secret_key: "............"
region: "us-west-2"

# VPC
vpc_cidr: 10.10.0.0/26
vpc_name: "my vpc"

# Subnet1
subnet1_name: "td-sb1"
subnet1_cidr: 10.10.0.0/27

# Subnet2
subnet2_name: "td-sb2"
subnet2_cidr: 10.10.0.32/27

# Availability zones
az1: "us-west-2a"
az2: "us-west-2b"
az3: "us-west-2c"
az4: "us-west-2d"

但是执行 ansible-playbook playbook.yml 时,显示以下错误:

TASK [aws-vpc : create public subnet2] *********************************************************************************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: TypeError: 'NoneType' object is not subscriptable
fatal: [localhost]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n  File \"/home/prudhvi/.ansible/tmp/ansible-tmp-1567259099.58-219222309055552/AnsiballZ_ec2_vpc_subnet.py\", line 114, in <module>\n    _ansiballz_main()\n  File \"/home/prudhvi/.ansible/tmp/ansible-tmp-1567259099.58-219222309055552/AnsiballZ_ec2_vpc_subnet.py\", line 106, in _ansiballz_main\n    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)\n  File \"/home/prudhvi/.ansible/tmp/ansible-tmp-1567259099.58-219222309055552/AnsiballZ_ec2_vpc_subnet.py\", line 49, in invoke_module\n    imp.load_module('__main__', mod, module, MOD_DESC)\n  File \"/usr/lib/python3.6/imp.py\", line 235, in load_module\n    return load_source(name, filename, file)\n  File \"/usr/lib/python3.6/imp.py\", line 170, in load_source\n    module = _exec(spec, sys.modules[name])\n  File \"<frozen importlib._bootstrap>\", line 618, in _exec\n  File \"<frozen importlib._bootstrap_external>\", line 678, in exec_module\n  File \"<frozen importlib._bootstrap>\", line 219, in _call_with_frames_removed\n  File \"/tmp/ansible_ec2_vpc_subnet_payload_v3vahv3o/__main__.py\", line 602, in <module>\n  File \"/tmp/ansible_ec2_vpc_subnet_payload_v3vahv3o/__main__.py\", line 592, in main\n  File \"/tmp/ansible_ec2_vpc_subnet_payload_v3vahv3o/__main__.py\", line 504, in ensure_subnet_present\n  File \"/tmp/ansible_ec2_vpc_subnet_payload_v3vahv3o/__main__.py\", line 517, in ensure_final_subnet\nTypeError: 'NoneType' object is not subscriptable\n", "module_stdout": "", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}

PLAY RECAP *************************************************************************************************************************************************************************************************
localhost     

为什么会来以及如何解决..?

此处成功创建了vpc,但是子网产生了问题。为什么?

1 个答案:

答案 0 :(得分:0)

最后,我找到了答案,很想与您分享

main.yml 在任务中

- name: create public subnet2
  ec2_vpc_subnet:
    state: present
    vpc_id: "{{ vpc.vpc.id }}"     # initially it was vpc_id: "{{ vpc.vpc.id }} " 
    cidr: "{{ subnet2_cidr }}"
    az: "{{ az2 }}"
    aws_access_key: "{{ aws_access_key }}"
    aws_secret_key: "{{ aws_secret_key }}"
    map_public: yes
    resource_tags:
       Name: "{{ subnet2_name }}"
  register: subnet2

- name: print subnet2 details
  debug:
        var: subnet2

问题与空间有关,所以请注意这些问题。