kumar kittu
kumar kittu

Reputation: 87

aws cli command using profile as variable

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.

Upvotes: 0

Views: 1105

Answers (2)

matsev
matsev

Reputation: 33787

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

Upvotes: 0

WarrenG
WarrenG

Reputation: 1850

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"

Upvotes: 1

Related Questions