0
votes

this is my playbook.yml calling role

playbook.yml

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

my roles aws-vpc contains tasks,vars directory

main.yml in tasks

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

But executing ansible-playbook playbook.yml , it is showing following error:

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     

Why it is coming and how to resolve it ..?

Here vpc is being created succesfully but subnet is creating problem. Why?

1
can you add the region parameter to the ec2_vpc_subnet module as it's using us-east-1` by default but your vpc is in us-west-2Arbab Nazar

1 Answers

0
votes

Finally,I found the answer and like to share with you

main.yml in tasks

- 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

Issue is with space.So please take care of these issues.