5

I have a piece of code which works, something like this (note this is inside CloudFormation Template for AWS auto deployment):

EFS_SERVER_IPS_ARRAY=( $(aws efs describe-mount-targets --file-system-id ${SharedFileSystem} | jq '.MountTargets[].IpAddress' -r) )
echo "IPs in EFS_SERVER_IPS_ARRAY:"
for element in "${EFS_SERVER_IPS_ARRAY[@]}"
do
    echo "$element"
    echo "$element $MOUNT_SOURCE" >> /etc/hosts
done

This works but looks ugly. I want to avoid the array variable and the for loop (basically I don't care about the first echo command).

Can I somehow use the output ($element, which is 1 or more, currently 2 lines of IPs) and funnel it into two executions of something like:

long AWS command >> echo $element $MOUNT_SOURCE  >> /etc/hosts

with echo executing as many times as there are variables in the array, in current implementation? How would I rewrite this?

The output of the AWS command is like this:

10.10.10.10
10.22.22.22

Then, the added lines in /etc/hosts look like:

10.10.10.10  unique-id.efs.us-east-1.amazonaws.com
10.22.22.22  unique-id.efs.us-east-1.amazonaws.com
  • @Jesse_b I edited, added sample output of the aws command. efs host needs two columns: the IP outputted, and the hostname in $MOUNT_SOURCE variable defined outside the snipped I added) – Carmageddon May 16 at 20:18
  • Yes but is this really adding the IP address to your /etc/hosts? It seems more likely it is just adding the literal numbers 0 and 1 to it. – Jesse_b May 16 at 20:19
  • @Jesse_b yes it works, already tested on AWS deployment. Why do you think it should output just 0 and 1? – Carmageddon May 16 at 20:21
  • @Jesse_b, Are you suggesting that potentially, if there'd be only one IP returned (single availability zone), it wont be an array anymore, and break the code? Currently as you see in the snippet, I have @ sign, I have array assignment =(..), so I am not sure if I get your point? – Carmageddon May 16 at 20:25
  • 1
    No I'm saying that when an array is called with the ${!name[@]} syntax it will expand to a list of the indices (0 1 2 3, etc) and not the elements (ip1 ip2 ip3, etc). – Jesse_b May 16 at 20:26
8
aws efs describe-mount-targets --file-system-id ${SharedFileSystem} \
    | jq --arg mntsrc "$MOUNT_SOURCE" '.MountTargets[].IpAddress | . + $mntsrc' -r >> /etc/hosts

or, if you prefer,

aws efs describe-mount-targets --file-system-id ${SharedFileSystem} \
    | jq '.MountTargets[].IpAddress' -r | sed -e "s~\$~$MOUNT_SOURCE~" >> /etc/hosts

All that's happening is adding some extra fixed text to the end of each line, which can happen either in jq (top) or in various ways outside (bottom). There's not really any array context here or anything being repeated, so you don't need a loop.

  • 1
    Damn hot! Thanks Michael!! I am a happy puppy now, the first one is great. I didnt know about the --args on jq, neat :) – Carmageddon May 16 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.