Bond
Bond

Reputation: 945

Ansible module to attach an IAM role to existing EC2 instances

I am trying to attach an IAM role to multiple EC2 instances based on tags. Is there a module already available which I can use. I have been searching for a bit but couldn't find anything specific.

Upvotes: 3

Views: 2681

Answers (2)

Eric Citaire
Eric Citaire

Reputation: 4513

I submitted a PR last year to add 2 AWS modules : boto3 and boto3_wait.

These 2 modules allow you to interact with AWS API using boto3.

For instance, you could attach a role to an existing EC2 instance by calling associate_iam_instance_profile method on EC2 service :

- name: Attach role MyRole
  boto3:
    service: ec2
    region: us-east-1
    operation: associate_iam_instance_profile
    parameters:
      IamInstanceProfile:
        Name: MyRole
      InstanceId: i-xxxxxxxxxx

Feel free to give the PR a thumbs-up if you like it! ;)

In addition to this, you can use AWS dynamic inventory to target instances by tag.

Upvotes: 1

helloV
helloV

Reputation: 52375

Attaching an IAM role to existing EC2 instances is a relatively new feature (announced in Feb 2017). There is no support for that in Ansible currently. If you AWS CLI 1.11.46 or higher installed, then you can use shell module to invoke the AWS CLI and achieve desired result.

See: New! Attach an AWS IAM Role to an Existing Amazon EC2 Instance by Using the AWS CLI

Upvotes: 2

Related Questions