我遵循了这三个指南:
我写了这个Ansible游戏
---
- hosts: localhost
connection: local
gather_facts: false
tasks:
- include_vars: aws_credentials.yml
- name: Creating EC2 Ubuntu instance
ec2:
instance_type: t1.micro
image: ami-86e0ffe7
region: us-west-2
key_name: my-aws-key
zone: us-west-2a
vpc_subnet_id: subnet-04199d61
group_id: sg-cf6736aa
assign_public_ip: yes
count: 1
wait: true
volumes:
- device_name: /dev/sda1
volume_type: gp2
volume_size: 10
instance_tags:
Name: ansible-test
Project: test
Ansible: manageable
register: ec2
然后我运行ansible-playbook create-ec2.yml -v --private-key ~/.ssh/my-key --vault-password-file ~/.password/to_ansible_vault
我收到了这条消息
播放[localhost] ******************************************* ********************
任务[include_vars] ******************************************* *****************
ok:[localhost] => {" ansible_facts":{" ec2_access_key":" decrypted_acces_key_XXXXX"," ec2_secret_key":" decrypted_secret_key_XXXXX"},&# 34;改变":false}
任务[创建EC2 Ubuntu实例] **************************************** ****
致命:[localhost]:失败! => {"更改":false,"失败":true," msg":"没有处理程序准备好进行身份验证。检查了1名处理程序。 [' HmacAuthV4Handler']检查您的凭据"}
没有更多的主人左翼***************************************** ******************
[警告]:无法创建重试文件' create-ec2.retry'。 [Errno 2]没有这样的文件或目录:''
回放********************************************* ************************
localhost:ok = 1 changed = 0 unreachable = 0 failed = 1
当我运行ansible-vault view aws_credentials.yml --vault-password-file ~/.password/to_ansible_vault
时,我获得了加密的aws_credentials.yml的可读内容,
像这样的东西:
---
ec2_access_key: "XXXXX"
ec2_secret_key: "XXXXX"
当我使用普通的aws_credentials.yml时,它也不起作用。只有在我导出凭据时,它才能正常运行。 有人可以帮助我,我如何编写playbook来创建存储在加密文件中的凭据的ec2实例?
答案 0 :(得分:2)
在这种情况下,我认为您应该直接将密钥提供给ec2模块 试试这个:
- name: Creating EC2 Ubuntu instance
ec2:
aws_access_key: "{{ ec2_access_key }}"
aws_secret_key: "{{ ec2_secret_key }}"
instance_type: t1.micro
image: ami-86e0ffe7
region: us-west-2
...
HttpClient表明它只检查模块的参数和环境变量,而不是主机变量。
答案 1 :(得分:1)
您还可以将AWS API密钥导出到OS环境变量,例如:
export AWS_ACCESS_KEY=XXXXXXX
在Ansible场景的情况下,您需要设置:
- name: Creating EC2 Ubuntu instance
ec2:
aws_access_key: "{{ lookup('env', 'AWS_ACCESS_KEY') }}"
aws_secret_key: "{{ lookup('env', 'AWS_SECRET_KEY') }}"
instance_type: t1.micro
image: ami-86e0ffe7
region: us-west-2