我有点被这个难住了......我很清楚Chef运行阶段和编译阶段之间的概念,但我似乎仍然不能弄清楚我需要完成什么。
我认为How to store array in node object chef?中列出的解决方案可能对正常的方法有所帮助,但我正在尝试创建一个自定义资源,它基本上读取具有以下结构的数据库:
{
"id": "local_files",
"development":
[
{
"source": "https://sourcecode.mydomain.com/projects/ASX-NBJG/repos/sapops/raw/config/lama/Z_LVM_CheckBackup.conf?at=refs%2Fheads%2Ffeature%2FADVR-575-as-a-basis-
user-i-want-to-be-able-to-use-chef-to-copy-files-locally-to-servers",
"target": "/tmp/some_folder/Z_LVM_CheckBackup.conf",
"owner": "root",
"group": "root",
"mode": "0777",
"action": "create",
"run_command" : "echo 'hello my beautiful world' > /tmp/helloworld.txt"
},
{
"source": "https://sourcecode.mydomain.com/projects/ABCD/repos/build/raw/copylocal/sudoerspolicy?at=refs%2Fheads%2Ffeature%2FADVR575CopyLocal",
"target": "/etc/sudoers.d/mysudoers",
"owner": "root",
"group": "root",
"mode": "0440",
"action": "create",
"run_command" : "echo \"sudoers file updated on `date`\" |tee -a /tmp/sudoers_updated.log"
},
{
"source": "file:///erpsoftware/auto/copylocal/testtar.tar",
"target": "/tmp/testtar.tar",
"owner": "root",
"group": "root",
"mode": "0440",
"action": "create",
"run_command" : "mkdir -p /tmp/letsSeeHowItGoes && tar -xvf /tmp/testtar.tar -C /tmp/letsSeeHowItGoes"
},
],
"qa":
[
{
"source": "https://sourcecode.mydomain.com/projects/ASX/repos/ops/raw/config/stuff/Z_LVM_CheckBackup.conf?at=refs%2Fheads%2Ffeature%2FADVR-575-as-a-basis-
user-i-want-to-be-able-to-use-chef-to-copy-files-locally-to-servers",
"target": "/tmp/some_folder/Z_LVM_CheckBackup.conf",
"owner": "root",
"group": "root",
"mode": "0777",
"action": "create",
"run_command" : "echo 'hello my beautiful world' > /tmp/helloworld.txt"
}
]
资源调用:
copy_local 'Ensure all files that need to be copied locally are handled...' do
databag_itemid node['srv']['copylocal_databag_id']
action :from_databag
end
我遇到的问题是,即使我删除了第一个文件,我的代码选择的“run_command”始终是散列数组中的最后一项。
我有这样一个简单的循环:
resource_name :copy_local
property :databag_itemid, String
property :databag_auth_required, [true, false], default: true
property :databag_name, String, default: node['srv']['databag_name']
property :databag_env, String, default: node['scm_appbranch'].downcase
property :secret_databag_itemid, String, default: 'sa'
property :secret_keysrc, String, default: node['srv']['sa_sec_key_src']
property :secret_key, String, default: node['srv']['sa_secret_key']
property :service_acctname, String, default: 'service_account'
property :service_acctpwname, String, default: 'service_account_pw'
property :files, Hash
property :files_env, String, default: node['scm_appbranch'].downcase
property :debug, [true, false], default: false
action :from_databag do
# load variables
skey = new_resource.secret_key
skeysrc = new_resource.secret_keysrc
sdbid = new_resource.secret_databag_itemid
dbenv = new_resource.databag_env
dbid = new_resource.databag_itemid
dbname = new_resource.databag_name
dbauthreq = new_resource.databag_auth_required
saname = new_resource.service_acctname
sapwname = new_resource.service_acctpwname
dbg = new_resource.debug
# DL the secret
remote_file skey do
source skeysrc
action :nothing
sensitive true
end.run_action(:create)
# Load the secret and try to decrypt
secret = Chef::EncryptedDataBagItem.load_secret(skey)
begin
credentials = data_bag_item(dbname, sdbid, secret)
user = credentials[saname]
password = credentials[sapwname]
rescue StandardError => msg
puts 'ERROR :: Could not get credentials from encrypted databag!!!'
raise msg
end
# load the data bag item for copy local functionality
all_local_files = data_bag_item(dbname, dbid)
# load the hash
my_files = missing?(all_local_files[dbenv]) ? all_local_files : all_local_files[dbenv]
# now loop through files and begin local copy via the remote_file resource
# This loop does not work, need to find a way to loop through and not only get the last item of the array
auth = "Basic #{Base64.encode64("#{user}:#{password}")}"
my_files.each do |file_obj|
Array.wrap(file_obj).each do |file|
# check all flavors for each file object
checks = check_all_flavors(file)
unless checks.has_value?(false)
# Debug
puts " :: D E B U G :: ==> file source : #{file['source']}" if dbg
puts " :: D E B U G :: ==> file target : #{file['target']}" if dbg
puts " :: D E B U G :: ==> file mode : #{file['mode']}" if dbg
puts " :: D E B U G :: ==> file owner : #{file['owner']}" if dbg
puts " :: D E B U G :: ==> file group : #{file['group']}" if dbg
puts " :: D E B U G :: ==> file action : #{file['action']}" if dbg
puts " :: D E B U G :: ==> file run_command: #{file['run_command']}" if dbg
# Create the directory for the parent folder of the file['target'] if it doesn't exist
directory dir_name(file['target']) do
recursive true
mode file['mode']
owner file['owner']
group file['group']
action :create
not_if { dir_exists?(dir_name(file['target'])) }
end
# use remote_file resource to copy the file locally
remote_file file['target'] do
source file['source']
mode file['mode']
owner file['owner']
group file['group']
headers('Authorization' => auth) if dbauthreq
action file['action']
notifies :run, 'execute[run-command-for-copy-local-databag]', :immediately unless missing?(file['run_command'])
end
# resource to execute any command specified in copy local attributes
# allows us to "copy local and execute a command"
execute 'run-command-for-copy-local-databag' do
command file['run_command']
environment file['run_env']
creates file['run_creates']
cwd file['run_cwd']
group file['run_group']
user file['run_user']
action :nothing
end
end
end
end
# # delete secret file once data bag is decrypted successfully
# file skey do
# action :delete
# end
end
但是正如我提到的,执行的不是附属的“run_command”,而是列表中的最后一个“run_command”。对我来说,这似乎是一个相当直接的循环,所有者、组和权限都正常工作,但是我的“run_command”执行资源块似乎总是拾取数据库中的最后一项。这意味着,即使我有意删除/tmp/some_folder/Z_LVM_CheckBackup.conf
文件,当它应该触发附属的echo "hello my beautiful world"
命令时,它正在运行rum_command键的列表中的最后一个命令:"run_command" : "mkdir -p /tmp/letsSeeHowItGoes && tar -xvf /tmp/testtar.tar -C /tmp/letsSeeHowItGoes"
我尝试使用www.example.com _state来实现How to store array in node object chef?中的策略node.run,但是我也没有得到任何运气,在这一点上我非常为难,非常感谢任何帮助。
谢谢!史蒂夫
1条答案
按热度按时间x3naxklr1#
所以我可以通过使我的执行块“唯一”来解决这个问题。
然后像这样调用我的执行资源:
问题现在解决了。执行块运行数组中最后一项的原因是执行资源的名称是静态的。一旦我添加了“each_with_index”,然后将其与文件名连接,并以相同的方式重命名执行块,它就修复了我的问题,现在可以一致地工作了。我希望这能帮助其他人解决问题!!