TypeError:“NoneType”对象在ansib中不可订阅

2024-03-28 17:07:47 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的剧本.yml调用角色

剧本.yml

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

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

主.yml在任务中

^{2}$

主.yml在VAR中


# 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"

但是执行一本易懂的剧本剧本.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创建成功,但子网出现问题。为什么?在


Tags: nameinpyawsmainymllineansible
1条回答
网友
1楼 · 发布于 2024-03-28 17:07:47

最后,我找到了答案,想和大家分享

主.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

问题在于空间。所以请注意这些问题。在

相关问题 更多 >