ruby - Chef, File does not exists after force the creation -
i have following logic in recipe creates file if it's missing , tries insert lines in it.
file "/root/myfile" owner 'root' group 'root' mode '400' action :create_if_missing end file = chef::util::fileedit.new('/root/myfile') data_bag('my_databag').each |user| # ... lines using chef resources user, group, etc. if node.chef_environment != "my_env" line = "something write in myfile" chef::log.info("-----> #{line}") file.insert_line_if_no_match(/#{line}/, line) end end file.write_file
the recipe fails in case myfile
doesn't exist despite i'm forcing creation of file before instruction causes exception. here error:
argumenterror ------------- file '/root/.testfile' not exist relevant file content: ---------------------- 18: 19>> file = chef::util::fileedit.new('/root/.testfile') 20:
can me understand why doesn't work? on appreciated.
update: tried touch file first still failing in same line:
26: fileutils.touch('/root/myfile') 27>> file = chef::util::fileedit.new('/root/.testfile')
this yet issue of 2 phase nature of chef. pure ruby code runs in first pass. "compile" or "resource gathering" phase. ruby code chef resources adds resource definition list of resources. after complete, second "execution" phase happens. phase execute resources. file
declaration doesn't until after fileutils code fails.
two things do:
first, should avoid using fileutils in recipe if @ possible. far better create file template if can.
second, if change file declaration little, can hack run during first phase: xz
file "/root/myfile" owner 'root' group 'root' mode '400' action :nothing end.run_action(:create_if_missing)
Comments
Post a Comment