8

I'm trying to launch an instance with an IAM Role using Ansible.

But I don't find how to do it.

Does anyone know if it is possible?

If I can't use Ansible. I try it with boto

4 Answers 4

11

http://docs.ansible.com/ec2_module.html

instance_profile_name (added in 1.3):
Name of the IAM instance profile to use. Boto library must be 2.5.0+

0
2
- name: Get Instance ID
  debug: var=ec2_id

- name: set IAM ROLE
  ec2_instance:
    region: eu-west-3
    aws_access_key: "{{ package_aws_access_key }}"
    aws_secret_key: "{{ package_aws_secret_key }}"  
    instance_ids:
        - "{{ ec2_id }}"
    instance_role: "{{ iam_role }}"
  delegate_to: localhost
0

For instance_profile_name one simply has to use an IAM role name. If e.g. you have created an IAM role "webserver", then use "arn:aws:iam::123456789012:instance-profile/webserver" for instance_profile_name (edit arn to match your account).

make sure you also use the right trust policy principal:

{
  "Version": "2012-10-17",
  "Statement": {
    "Effect": "Allow",
    "Principal": {"Service": "ec2.amazonaws.com"},
    "Action": "sts:AssumeRole"
  }
} 
0

You can test the tasks below and attach an iam_role to your instance. I didn't try this with instance name but I think that it will work.

- name: Get Instance ID debug: 
   var=ec2_id

- name: set IAM ROLE 
  ec2_instance: 
    region: eu-west-3 
    aws_access_key: "{{ package_aws_access_key }}"
    aws_secret_key: "{{ package_aws_secret_key }}"
    instance_ids: 
      - "{{ ec2_id }}" 
    instance_role: "{{ iam_role }}" 
  delegate_to: localhost

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .