高山_流水
海阔凭鱼跃,天高任鸟飞

场景:将动态脚本分发到各个机器的指定目录下

 

说明:使用SaltStack的 file.managed

file.managed 里面可以定义内容的几种方式 

- source:

- contents:

- contents_pillar:

contents_grains:

这里我们使用 - contents_pillar:

代码:

{% set script_dir = pillar.get("script_dir","") %}
{% set script_content = pillar.get("script_content","") %}

{{script_dir}}:
  file.managed:
    - mode: 0644
    - contents_pillar: {{ script_content }}
    - template: jinja
    - user: root

调用方式模拟:

hostname state.sls test pillar='{"script_content":"script_content","script_content":"123/n456","script_dir":"/data/aaa/aa.sh"}'

说明:

hostname:要分发的主机名称

test:编写的sls文件名称

这里相当于使用了变量嵌套的方式(script_content)进行传值,解决了上传内容不能换行和中文乱码的问题

注意:

这里使用的是- contents_pillar:,如果使用- contents:,没法进行换行,并且有中文乱码的问题。

posted on 2019-10-18 11:26  高山_流水  阅读(508)  评论(0编辑  收藏  举报