Chef check for hostname?

10,496

Solution 1

Ohai should allow you to get hostname?

If you want something node specific it seems like you could just plug in to the attribute precedence in chef. You would set a default value for the attribute maybe at the cookbook level and then set an explicit attribute on the node. It also means if you need 2 of these servers to have the value you don't change the cookbook just the config on the servers.

Solution 2

Use

node.name

or

node.name.split('.')[0]

Solution 3

I just ran into this trying to deploy a special version of a file to one hostname. I used not_if, only_if

cookbook_file 'file/to/replace' do
  not_if {node.name == 'host.domain.name'}
  source 'file_version.4'
  action :create
end

cookbook_file '/file/to/replace' do
  only_if {node.name == 'host.domain.name'}
  source 'file_version.5'
  action :create
end
Share:
10,496
wsani
Author by

wsani

Updated on June 04, 2022

Comments

  • wsani
    wsani almost 2 years

    It turns out I can't check for a node(host) name in Chef, so I'm trying to figure out the best way to make the following happen:

    If hostname is X
     ldap_access_filter = memberOf=<%= node['sssd_ldap']['ldap_access_node_filter'] %>
    else
     ldap_access_filter = memberOf=<%= node['sssd_ldap']['ldap_access_filter'] %>
    end
    

    The idea is that when the node name (or some matching variable) is true, then it uses the ldap_access_node_filter, which is a unique value, else, it uses the default value. I'm basically configuring sssd config, and one of the hosts requires a special ldap access filter.

    If there is a better way, please let me know.

    Please help.