0
votes

currently i am using tasks in ansible .The task include aws cli command like below:

command: aws ec2 describe-instances --region myregion --profile myawsprofile --filters

It works perfectly when ran through

ansible-playbook my.yml 

Now my question is : I have multiple aws profiles and don't want hard code in the aws cli commands(in tasks).
wanted to use variable for profile,so whatever variable passed to profile it will execute task and run ansible-playbook.

Request anyone to suggest how to proceed for this.

I looked at Environment Variables in aws cli , but didn't helped much.

3

3 Answers

0
votes

You have two options (change the [profile name] below with the actual value):

  • Either provide the --profile [profile name] as a command option for every command.
  • Alternatively, set the profile to be used as an environment variable. On Mac and Linux using either AWS_PROFILE or AWS_DEFAULT_PROFILE

    $ export AWS_PROFILE=[profile name]
    

    or on Windows

    C:\> setx AWS_PROFILE [profile name]
    

The advantage of using the latter method is that once configured it keeps its value for the duration of the terminal session (or until it is being replaced with another profile name).

Reference: the Named Profiles chapter of the AWS CLI documentation

1
votes

If you really want to use the command task and not the ec2_instance tasks then you could this:

command: aws ec2 describe-instances --region myregion --profile {{ myawsprofile }} --filters

and then to run your playbook

ansible-playbook my.yml  -e "myawsprofile=production"
0
votes

You'll be better off with ec2_instance.

In addition to this, you might want to review other 100+ Amazon modules.