Reputation: 454
I am trying to figure out how to pass a value from Terraform to Chef. I have an attribute set in Terraform.
run_list = ['cookbook::default']
attributes_json = <<EOF
{
"department": "CC"
}
EOF
Here is my Chef recipe
department = ''
if node['platform_family'] == 'windows'
user 'test' do
password 'hash'
comment department
end
if department == 'CC'
group 'Administrators' do
action :modify
members 'test'
append true
end
end
end
When I run Terraform, it completes successfully but the conditional for department is not met so it does not add test account to admin group. I plugged department into the user comment as well so I could see the value (if anything). Nothing is being passed.
Can anyone see what I am doing wrong?
Upvotes: 1
Views: 388
Reputation: 454
OK so I finally figured out how to get the values. I made a single non-critical change to the Terraform json just in case I wanted to pass extra values.
attributes_json = <<EOF
{
"access": {
"department": "CC"
}
}
EOF
However, I was not referencing this value correctly in Chef.
department = node['access']['department']
if node['platform_family'] == 'windows'
user 'test' do
password 'hash'
comment department
end
if department == 'CC'
group 'Administrators' do
action :modify
members 'test'
append true
end
end
end
I needed to set the attribute created in Chef equal to the value coming from Terraform. Hope this helps someone else.
Upvotes: 1
Reputation: 28774
When you pass JSON to the json_attributes
argument in Terraform's Chef provisioner, it is cast as a hash and added to the node
hash when passed to Chef. Therefore, your values are accessible as key-value pairs within the hash assigned to node
, and your specific key is department
and value is CC
. Therefore, the lookup syntax for this in Chef (and Ruby) would be:
node['department']
Updating your code in Chef we arrive at:
if node['department'] == 'CC'
group 'Administrators' do
action :modify
members 'test'
append true
end
end
Upvotes: 1