How do I create a new key in Ansible using the ec2_key module?
3 answers
The examples in the Ansible documentation show exactly how to do this:
# Creates a new ec2 key pair named `example` if not present, returns generated
# private key
- name: example ec2 key
local_action:
module: ec2_key
name: example
# Creates a new ec2 key pair named `example` if not present using provided key
# material
- name: example2 ec2 key
local_action:
module: ec2_key
name: example2
key_material: 'ssh-rsa AAAAxyz...== me@example.com'
state: present
# Creates a new ec2 key pair named `example` if not present using provided key
# material
- name: example3 ec2 key
local_action:
module: ec2_key
name: example3
key_material: "{{ item }}"
with_file: /path/to/public_key.id_rsa.pub
+4
source to share
But you cannot save the generated private key , so this method is not suitable. Instead of using the ec2_key module, use the command module and use the aws cli to create and store the private key in the pem file. eg.
- command: /usr/local/bin/aws ec2 create-key-pair --key-name keypair_name > ~/keypair_name.pem
register: keypair
-1
source to share