Have a question about Chef variables, can't seem to get the variables to work
I'm trying to use server roles to add data into a template. I tried a couple of methods and can't seem to get it to work.
Here is the role code block.
ruby_block 'get_main_role_attr' do
block do
require 'mixlib/shellout'
rolecmd = Mixlib::ShellOut.new("cat /var/chef/role | cut -d',' -f 1 | cut -c2-")
rolecmd.run_command
rolecmd.error!
node.normal['mainrole'] = rolecmd.stdout
end
end
template "/etc/ssh/sshd_config" do
source "ssh/sshd_config.erb"
owner "root"
group "root"
mode "0600"
action :create
variables(
:allowed_users_team1 => "AllowUsers user1 user2 user3",
) if node['mainrole'] == "role1"
notifies :restart, "service[sshd]", :immediately
variables(
:allowed_users_team2 => "AllowUsers user4 user5 user6",
) if node['mainrole'] == "role2" || node['mainrole'] == "role3" || node['mainrole'] == "role4"
notifies :restart, "service[sshd]", :immediately
end
Method 1
variables(
:allowed_users => "AllowUsers user1 user2",
) if node['mainrole'] == "role1" || node['mainrole'] == "role2" || node['mainrole'] == "role3"
notifies :restart, "service[sshd]", :immediately
Method 2
variables(
:allowed_users => "AllowUsers user1 user2",
) if node['mainrole'] == "role1" || "role2" || "role3"
notifies :restart, "service[sshd]", :immediately
Method 3
variables(
:allowed_users_bidder => "AllowUsers user1 user2",
) if node['roles'].any? {|r| %w{role1 role2 role3}.include?(r)}
notifies :restart, "service[sshd]", :immediately
Let me knwo what I'm doign wrong please.. Thank you for the help
Thx
ruby linux chef
add a comment |
I'm trying to use server roles to add data into a template. I tried a couple of methods and can't seem to get it to work.
Here is the role code block.
ruby_block 'get_main_role_attr' do
block do
require 'mixlib/shellout'
rolecmd = Mixlib::ShellOut.new("cat /var/chef/role | cut -d',' -f 1 | cut -c2-")
rolecmd.run_command
rolecmd.error!
node.normal['mainrole'] = rolecmd.stdout
end
end
template "/etc/ssh/sshd_config" do
source "ssh/sshd_config.erb"
owner "root"
group "root"
mode "0600"
action :create
variables(
:allowed_users_team1 => "AllowUsers user1 user2 user3",
) if node['mainrole'] == "role1"
notifies :restart, "service[sshd]", :immediately
variables(
:allowed_users_team2 => "AllowUsers user4 user5 user6",
) if node['mainrole'] == "role2" || node['mainrole'] == "role3" || node['mainrole'] == "role4"
notifies :restart, "service[sshd]", :immediately
end
Method 1
variables(
:allowed_users => "AllowUsers user1 user2",
) if node['mainrole'] == "role1" || node['mainrole'] == "role2" || node['mainrole'] == "role3"
notifies :restart, "service[sshd]", :immediately
Method 2
variables(
:allowed_users => "AllowUsers user1 user2",
) if node['mainrole'] == "role1" || "role2" || "role3"
notifies :restart, "service[sshd]", :immediately
Method 3
variables(
:allowed_users_bidder => "AllowUsers user1 user2",
) if node['roles'].any? {|r| %w{role1 role2 role3}.include?(r)}
notifies :restart, "service[sshd]", :immediately
Let me knwo what I'm doign wrong please.. Thank you for the help
Thx
ruby linux chef
1
"can't seem to get it to work" is not a precise enough error description for us to help you. What doesn't work? How doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ?
– Jörg W Mittag
Nov 13 '18 at 20:16
Sorry, I have multiple variable blocks for a template. I'm trying to apply data to the file on the server, according to the chef role. Role1 should have a certain set of users, role2 should have another set of users. So when I run chef, its not applying the set of users per that server role. Hope that makes better sense.
– Devon
Nov 13 '18 at 20:40
add a comment |
I'm trying to use server roles to add data into a template. I tried a couple of methods and can't seem to get it to work.
Here is the role code block.
ruby_block 'get_main_role_attr' do
block do
require 'mixlib/shellout'
rolecmd = Mixlib::ShellOut.new("cat /var/chef/role | cut -d',' -f 1 | cut -c2-")
rolecmd.run_command
rolecmd.error!
node.normal['mainrole'] = rolecmd.stdout
end
end
template "/etc/ssh/sshd_config" do
source "ssh/sshd_config.erb"
owner "root"
group "root"
mode "0600"
action :create
variables(
:allowed_users_team1 => "AllowUsers user1 user2 user3",
) if node['mainrole'] == "role1"
notifies :restart, "service[sshd]", :immediately
variables(
:allowed_users_team2 => "AllowUsers user4 user5 user6",
) if node['mainrole'] == "role2" || node['mainrole'] == "role3" || node['mainrole'] == "role4"
notifies :restart, "service[sshd]", :immediately
end
Method 1
variables(
:allowed_users => "AllowUsers user1 user2",
) if node['mainrole'] == "role1" || node['mainrole'] == "role2" || node['mainrole'] == "role3"
notifies :restart, "service[sshd]", :immediately
Method 2
variables(
:allowed_users => "AllowUsers user1 user2",
) if node['mainrole'] == "role1" || "role2" || "role3"
notifies :restart, "service[sshd]", :immediately
Method 3
variables(
:allowed_users_bidder => "AllowUsers user1 user2",
) if node['roles'].any? {|r| %w{role1 role2 role3}.include?(r)}
notifies :restart, "service[sshd]", :immediately
Let me knwo what I'm doign wrong please.. Thank you for the help
Thx
ruby linux chef
I'm trying to use server roles to add data into a template. I tried a couple of methods and can't seem to get it to work.
Here is the role code block.
ruby_block 'get_main_role_attr' do
block do
require 'mixlib/shellout'
rolecmd = Mixlib::ShellOut.new("cat /var/chef/role | cut -d',' -f 1 | cut -c2-")
rolecmd.run_command
rolecmd.error!
node.normal['mainrole'] = rolecmd.stdout
end
end
template "/etc/ssh/sshd_config" do
source "ssh/sshd_config.erb"
owner "root"
group "root"
mode "0600"
action :create
variables(
:allowed_users_team1 => "AllowUsers user1 user2 user3",
) if node['mainrole'] == "role1"
notifies :restart, "service[sshd]", :immediately
variables(
:allowed_users_team2 => "AllowUsers user4 user5 user6",
) if node['mainrole'] == "role2" || node['mainrole'] == "role3" || node['mainrole'] == "role4"
notifies :restart, "service[sshd]", :immediately
end
Method 1
variables(
:allowed_users => "AllowUsers user1 user2",
) if node['mainrole'] == "role1" || node['mainrole'] == "role2" || node['mainrole'] == "role3"
notifies :restart, "service[sshd]", :immediately
Method 2
variables(
:allowed_users => "AllowUsers user1 user2",
) if node['mainrole'] == "role1" || "role2" || "role3"
notifies :restart, "service[sshd]", :immediately
Method 3
variables(
:allowed_users_bidder => "AllowUsers user1 user2",
) if node['roles'].any? {|r| %w{role1 role2 role3}.include?(r)}
notifies :restart, "service[sshd]", :immediately
Let me knwo what I'm doign wrong please.. Thank you for the help
Thx
ruby linux chef
ruby linux chef
edited Nov 13 '18 at 20:52
Devon
asked Nov 13 '18 at 20:03
DevonDevon
848
848
1
"can't seem to get it to work" is not a precise enough error description for us to help you. What doesn't work? How doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ?
– Jörg W Mittag
Nov 13 '18 at 20:16
Sorry, I have multiple variable blocks for a template. I'm trying to apply data to the file on the server, according to the chef role. Role1 should have a certain set of users, role2 should have another set of users. So when I run chef, its not applying the set of users per that server role. Hope that makes better sense.
– Devon
Nov 13 '18 at 20:40
add a comment |
1
"can't seem to get it to work" is not a precise enough error description for us to help you. What doesn't work? How doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ?
– Jörg W Mittag
Nov 13 '18 at 20:16
Sorry, I have multiple variable blocks for a template. I'm trying to apply data to the file on the server, according to the chef role. Role1 should have a certain set of users, role2 should have another set of users. So when I run chef, its not applying the set of users per that server role. Hope that makes better sense.
– Devon
Nov 13 '18 at 20:40
1
1
"can't seem to get it to work" is not a precise enough error description for us to help you. What doesn't work? How doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ?
– Jörg W Mittag
Nov 13 '18 at 20:16
"can't seem to get it to work" is not a precise enough error description for us to help you. What doesn't work? How doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ?
– Jörg W Mittag
Nov 13 '18 at 20:16
Sorry, I have multiple variable blocks for a template. I'm trying to apply data to the file on the server, according to the chef role. Role1 should have a certain set of users, role2 should have another set of users. So when I run chef, its not applying the set of users per that server role. Hope that makes better sense.
– Devon
Nov 13 '18 at 20:40
Sorry, I have multiple variable blocks for a template. I'm trying to apply data to the file on the server, according to the chef role. Role1 should have a certain set of users, role2 should have another set of users. So when I run chef, its not applying the set of users per that server role. Hope that makes better sense.
– Devon
Nov 13 '18 at 20:40
add a comment |
1 Answer
1
active
oldest
votes
Chef client run is divided into 2 parts: compile and converge. In the first stage Chef creates a collection of resources that should be applied on the node and in the second stage it applies the changes. More on it on the Chef docs
You are assigning node.normal['mainrole']
in converge state, but you check the values (node['mainrole'] == "role1"
) in compile stage, which is before that.
You need to either move assigning to compile state or move checks into converge state.
One of the examples how to move checks into converge state is to use not_if
or only_if
. You will need 2 template resources in this case:
template "/etc/ssh/sshd_config for 1,2,3" do
path "/etc/ssh/sshd_config"
source "ssh/sshd_config.erb"
owner "root"
group "root"
mode "0600"
action :create
variables(:allowed_users_team1 => "AllowUsers user1 user2 user3")
only_if { node['mainrole'] == "role1" }
notifies :restart, "service[sshd]", :immediately
end
template "/etc/ssh/sshd_config for 4,5,6" do
path "/etc/ssh/sshd_config"
source "ssh/sshd_config.erb"
owner "root"
group "root"
mode "0600"
action :create
variables(:allowed_users_team2 => "AllowUsers user4 user5 user6")
only_if { %w[role2 role3 role4].include? node['mainrole'] }
notifies :restart, "service[sshd]", :immediately
end
Another way is to move the logic inside the template itself and pass 2 different variables:
variables(:allowed_users_team2 => "AllowUsers user4 user5 user6",
:allowed_users_team1 => "AllowUsers user1 user2 user3",
:mainrole => node['mainrole'])
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288658%2fhave-a-question-about-chef-variables-cant-seem-to-get-the-variables-to-work%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Chef client run is divided into 2 parts: compile and converge. In the first stage Chef creates a collection of resources that should be applied on the node and in the second stage it applies the changes. More on it on the Chef docs
You are assigning node.normal['mainrole']
in converge state, but you check the values (node['mainrole'] == "role1"
) in compile stage, which is before that.
You need to either move assigning to compile state or move checks into converge state.
One of the examples how to move checks into converge state is to use not_if
or only_if
. You will need 2 template resources in this case:
template "/etc/ssh/sshd_config for 1,2,3" do
path "/etc/ssh/sshd_config"
source "ssh/sshd_config.erb"
owner "root"
group "root"
mode "0600"
action :create
variables(:allowed_users_team1 => "AllowUsers user1 user2 user3")
only_if { node['mainrole'] == "role1" }
notifies :restart, "service[sshd]", :immediately
end
template "/etc/ssh/sshd_config for 4,5,6" do
path "/etc/ssh/sshd_config"
source "ssh/sshd_config.erb"
owner "root"
group "root"
mode "0600"
action :create
variables(:allowed_users_team2 => "AllowUsers user4 user5 user6")
only_if { %w[role2 role3 role4].include? node['mainrole'] }
notifies :restart, "service[sshd]", :immediately
end
Another way is to move the logic inside the template itself and pass 2 different variables:
variables(:allowed_users_team2 => "AllowUsers user4 user5 user6",
:allowed_users_team1 => "AllowUsers user1 user2 user3",
:mainrole => node['mainrole'])
add a comment |
Chef client run is divided into 2 parts: compile and converge. In the first stage Chef creates a collection of resources that should be applied on the node and in the second stage it applies the changes. More on it on the Chef docs
You are assigning node.normal['mainrole']
in converge state, but you check the values (node['mainrole'] == "role1"
) in compile stage, which is before that.
You need to either move assigning to compile state or move checks into converge state.
One of the examples how to move checks into converge state is to use not_if
or only_if
. You will need 2 template resources in this case:
template "/etc/ssh/sshd_config for 1,2,3" do
path "/etc/ssh/sshd_config"
source "ssh/sshd_config.erb"
owner "root"
group "root"
mode "0600"
action :create
variables(:allowed_users_team1 => "AllowUsers user1 user2 user3")
only_if { node['mainrole'] == "role1" }
notifies :restart, "service[sshd]", :immediately
end
template "/etc/ssh/sshd_config for 4,5,6" do
path "/etc/ssh/sshd_config"
source "ssh/sshd_config.erb"
owner "root"
group "root"
mode "0600"
action :create
variables(:allowed_users_team2 => "AllowUsers user4 user5 user6")
only_if { %w[role2 role3 role4].include? node['mainrole'] }
notifies :restart, "service[sshd]", :immediately
end
Another way is to move the logic inside the template itself and pass 2 different variables:
variables(:allowed_users_team2 => "AllowUsers user4 user5 user6",
:allowed_users_team1 => "AllowUsers user1 user2 user3",
:mainrole => node['mainrole'])
add a comment |
Chef client run is divided into 2 parts: compile and converge. In the first stage Chef creates a collection of resources that should be applied on the node and in the second stage it applies the changes. More on it on the Chef docs
You are assigning node.normal['mainrole']
in converge state, but you check the values (node['mainrole'] == "role1"
) in compile stage, which is before that.
You need to either move assigning to compile state or move checks into converge state.
One of the examples how to move checks into converge state is to use not_if
or only_if
. You will need 2 template resources in this case:
template "/etc/ssh/sshd_config for 1,2,3" do
path "/etc/ssh/sshd_config"
source "ssh/sshd_config.erb"
owner "root"
group "root"
mode "0600"
action :create
variables(:allowed_users_team1 => "AllowUsers user1 user2 user3")
only_if { node['mainrole'] == "role1" }
notifies :restart, "service[sshd]", :immediately
end
template "/etc/ssh/sshd_config for 4,5,6" do
path "/etc/ssh/sshd_config"
source "ssh/sshd_config.erb"
owner "root"
group "root"
mode "0600"
action :create
variables(:allowed_users_team2 => "AllowUsers user4 user5 user6")
only_if { %w[role2 role3 role4].include? node['mainrole'] }
notifies :restart, "service[sshd]", :immediately
end
Another way is to move the logic inside the template itself and pass 2 different variables:
variables(:allowed_users_team2 => "AllowUsers user4 user5 user6",
:allowed_users_team1 => "AllowUsers user1 user2 user3",
:mainrole => node['mainrole'])
Chef client run is divided into 2 parts: compile and converge. In the first stage Chef creates a collection of resources that should be applied on the node and in the second stage it applies the changes. More on it on the Chef docs
You are assigning node.normal['mainrole']
in converge state, but you check the values (node['mainrole'] == "role1"
) in compile stage, which is before that.
You need to either move assigning to compile state or move checks into converge state.
One of the examples how to move checks into converge state is to use not_if
or only_if
. You will need 2 template resources in this case:
template "/etc/ssh/sshd_config for 1,2,3" do
path "/etc/ssh/sshd_config"
source "ssh/sshd_config.erb"
owner "root"
group "root"
mode "0600"
action :create
variables(:allowed_users_team1 => "AllowUsers user1 user2 user3")
only_if { node['mainrole'] == "role1" }
notifies :restart, "service[sshd]", :immediately
end
template "/etc/ssh/sshd_config for 4,5,6" do
path "/etc/ssh/sshd_config"
source "ssh/sshd_config.erb"
owner "root"
group "root"
mode "0600"
action :create
variables(:allowed_users_team2 => "AllowUsers user4 user5 user6")
only_if { %w[role2 role3 role4].include? node['mainrole'] }
notifies :restart, "service[sshd]", :immediately
end
Another way is to move the logic inside the template itself and pass 2 different variables:
variables(:allowed_users_team2 => "AllowUsers user4 user5 user6",
:allowed_users_team1 => "AllowUsers user1 user2 user3",
:mainrole => node['mainrole'])
answered Nov 14 '18 at 8:50
Draco AterDraco Ater
16.4k65180
16.4k65180
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288658%2fhave-a-question-about-chef-variables-cant-seem-to-get-the-variables-to-work%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
"can't seem to get it to work" is not a precise enough error description for us to help you. What doesn't work? How doesn't it work? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ?
– Jörg W Mittag
Nov 13 '18 at 20:16
Sorry, I have multiple variable blocks for a template. I'm trying to apply data to the file on the server, according to the chef role. Role1 should have a certain set of users, role2 should have another set of users. So when I run chef, its not applying the set of users per that server role. Hope that makes better sense.
– Devon
Nov 13 '18 at 20:40