- 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
在执行此剧本时,我收到以下错误:
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
注意:密钥对已经存在。客户端是CentOS 7机器。
答案1
您没有在任务“创建 Linux 虚拟机”中设置aws_access_key
和。aws_secret_key
我认为,将凭证存储在 Ansible 中(Ansible Vault 除外)是个坏主意。最好将它们保存在单独的 shell 文件中并在运行 playbook 之前使用,例如:
带有凭证的文件,aws_testing_profile.sh
:
export AWS_ACCESS_KEY_ID=''
export AWS_SECRET_ACCESS_KEY=''
运行剧本:
source aws_testing_profile.sh
ansible-playbook -i inventory/testing/ my_playbook.yml
当然,您可以将凭证保留在 中~/.aws/credentials
,但我不喜欢它,因为您运行的任何命令都将拥有您的 AWS 权限,而无需确认。