这是我要转换成Ansible的厨师食谱。什么可能相当于它? 我有路径和网址。
remote_file 'abc_artifact' do
path abc_artifact_path
source node[:base][:agent][:abc_url] % {
:version => node[:base][:agent][:agent_version],
:env => environment
}
checksum node[:base][:agent][:agent_sha256]
notifies :run, 'execute[unzip_abc_artifact]', :immediately
action :create_if_missing
end
这是第二部分,
其中" node.run_state [:abc] || = Mash.new"
case node[:platform_family]
when "debian"
abc_attributes = node[:abc][:default].merge(node[:abcone][:agent]).merge(:username=>node.run_state[:abc][:abc_username])
abc_url = im_abc_url(Mash.new(abc_attributes.to_hash))
答案 0 :(得分:2)
快速简便的解决方案:
vars:
source: "https://mydomain/myapp/1.0.0.zip"
abc_artifact: /path/to/my/file.zip
agent_sha256: 17054df9a6b887dbba25...
tasks:
- name: download abc artifact
get_url:
url: "{{ source }}"
dest: "{{ abc_artifact }}"
sha256sum: "{{ agent_sha256 }}"
notify: unzip abc artifact
handlers:
- name: unzip abc artifact
shell: "unzip -f {{ abc_artifact }}" # or whatever
这很容易。好的,我会尝试解释一些需要考虑的事情:
source node[:base][:agent][:abc_url] % {
:version => node[:base][:agent][:agent_version],
:env => environment
}
您可以使用format
jinja2 filter。有关详细信息,请参阅python string formatting documentation。但它可能类似于以下内容:
vars:
source: "https://%(env).mydomain/myapp/%(version)s.zip"
agent_version: 1.0.0
env: dev
tasks:
- name: download abc artifact
get_url:
url: "{{ source|format(**{'version': agent_version, 'env': env }) }}"
notifies :run, 'execute[unzip_abc_artifact]', :immediately
通常你应该使用处理程序:
tasks:
- name: download abc artifact
get_url: # [...]
notify: unzip abc artifact
handlers:
- name: unzip abc artifact
shell: "unzip -f {{ abc_artifact }}"
但如果您需要立即,也可以使用playbook conditionals:
tasks:
- name: download abc artifact
get_url: # [...]
register: abc_artifact_download
- name: unzip abc artifact
shell: "unzip -f {{ abc_artifact }}"
when: abc_artifact_download|changed
我在这里看到的唯一问题是,如果zip文件存在但尚未解压缩,则解压缩任务将不会运行。您的厨师示例中也存在此问题。也许您可以检查是否已提取特定文件。
action :create_if_missing
如果您出于某种原因需要保持此行为,请使用the stat
module和playbook conditionals:
tasks:
- name: check if abc_artifact_stat exists
stat: path={{ abc_artifact }}
register: abc_artifact_stat
- name: download abc artifact
get_url: # [...]
when: not abc_artifact_stat.stat.exists