0
- name: Create VPC
  ec2_vpc_net:
    name: arunvpc
    cidr_block: 10.4.0.0/16
    region: ap-northeast-1
    aws_access_key: "{{ aws_access_key }}"
    aws_secret_key: "{{ aws_secret_key }}"
    state: "present"
 register: my_vpc


- name: saving vpc id
  set_fact:
    vpc_id: "{{ my_vpc.vpc.id }}"


- name: creating subnet
  ec2_vpc_subnet:
     state: present
     vpc_id: "{{ vpc_id }}"
     cidr: 10.4.0.0/24
     region: ap-northeast-1
     aws_access_key: "{{ aws_access_key }}"
     aws_secret_key: "{{ aws_secret_key }}"
     resource_tags:
         name: "arun_subnet"
 register: my_subnet


- name: saving subnet id
  set_fact:
     sub_id: "{{ my_subnet.subnet.id }}"


- name: creating security group
  ec2_group:
      name: my_security_group
      description: security
      vpc_id: "{{ vpc_id }}"
      region: ap-northeast-1
      aws_access_key: "{{ aws_access_key }}"
      aws_secret_key: "{{ aws_secret_key }}"
      rules:
        - proto: tcp
          from_port: 22
          to_port: 22
          cidr_ip: 0.0.0.0/0


- name: creating linux vm
  ec2:
    key_name: my_key
    group: my_security_group
    instance_type: m1.small
    image: ami-775e4f16
    instance_tags:
      Name: arunlinux
    wait: yes
    region: ap-northeast-1
    count: 1
    vpc_subnet_id: "{{ sub_id }}"
    assign_public_ip: yes
  register: ec2_out

While executing this playbook, I am getting following error:

TASK [Gathering Facts] *********************************************************
ok: [localhost]

TASK [vpc : Create VPC] ********************************************************
ok: [localhost]

TASK [vpc : saving vpc id] *****************************************************
ok: [localhost]

TASK [vpc : creating subnet] ***************************************************
ok: [localhost]

TASK [vpc : saving subnet id] **************************************************
ok: [localhost]

TASK [vpc : creating security group] *******************************************
ok: [localhost]

TASK [vpc : creating linux vm] *************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AttributeError: 'module' object has no attribute 'ProfileNotFoundError'
fatal: [localhost]: FAILED! => {"changed": false, "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_9UtXSg/ansible_module_ec2.py\", line 1725, in <module>\n    main()\n  File \"/tmp/ansible_9UtXSg/ansible_module_ec2.py\", line 1673, in main\n    ec2 = ec2_connect(module)\n  File \"/tmp/ansible_9UtXSg/ansible_modlib.zip/ansible/module_utils/ec2.py\", line 320, in ec2_connect\nAttributeError: 'module' object has no attribute 'ProfileNotFoundError'\n", "module_stdout": "", "msg": "MODULE FAILURE", "rc": 0}
to retry, use: --limit @/playbooks/aws/main.retry

PLAY RECAP *********************************************************************
localhost                  : ok=6    changed=0    unreachable=0    failed=1

Note: the key-pair is already present. Client is CentOS 7 machine.

1

You didn't set aws_access_key and aws_secret_key in task "creating linux vm".

As I think, it is bad idea to store credentials in Ansible (except Ansible Vault). It is better to keep them in separate shell-file and use before run playbook, for example:

File with credentials, aws_testing_profile.sh:

export AWS_ACCESS_KEY_ID=''
export AWS_SECRET_ACCESS_KEY=''

run playbook:

source aws_testing_profile.sh
ansible-playbook -i inventory/testing/ my_playbook.yml

Of course, you can keep credentials in ~/.aws/credentials, but I don't like it, because any command you run will have your AWS rights without confirmation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.