什么可能等同于遵循 Chef 食谱到 Ansible?
What could be the possible equivalent of following Chef recipe to Ansible?
这是我想转换成 Ansible 的厨师食谱。可能的等价物是什么?
我有路径和 url。
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))
快速简便的解决方案:
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 for that. See the 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
如果出于任何原因需要保持此行为,请使用 stat
模块 和 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
这是我想转换成 Ansible 的厨师食谱。可能的等价物是什么? 我有路径和 url。
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))
快速简便的解决方案:
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 for that. See the 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 conditionalstasks:
- 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
如果出于任何原因需要保持此行为,请使用 stat
模块 和 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