shellchef-infrainspec

How to use attributes in an Inspec command?


I defined some basic Inspec tests to check if a loadbalancer is active:

proxy = attribute('proxy_netlb_arn')

control 'Checks if all the ECE Load balancers are active ' do
impact 1.0
title 'Checks if all the ECE Load balancers are active'
describe command("aws elbv2 describe-load-balancers --load-balancer-arn proxy['value'] | jq -r '.[][].State.Code'") do
    its('stdout') { should match "active" }
end
end

I use a variable called "proxy" which contains the ARN of the Loadbalancer. Unfortunately the variable is not recognized as such because it is inside the command.


Solution

  • you should use string interpolation to get the value of your string variable.

    assuming proxy['value'] returns the value of the proxy variable. then you can do it as follows:

    proxy = attribute('proxy_netlb_arn')
    
    control 'Checks if all the ECE Load balancers are active ' do
    impact 1.0
    title 'Checks if all the ECE Load balancers are active'
    describe command("aws elbv2 describe-load-balancers --load-balancer-arn #{proxy['value']} | jq -r '.[][].State.Code'") do
        its('stdout') { should match "active" }
    end
    end