微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当我迭代由 jinja2 生成的字符串列表时,我得到一系列字符而不是字符串为什么会这样?

如何解决当我迭代由 jinja2 生成的字符串列表时,我得到一系列字符而不是字符串为什么会这样?

我一直在尝试通过 ansible 部署 ceph 集群。当我尝试将我的 deploy_ceph_cluster.sh.j2 渲染成一个 shell 脚本时,我遇到了一些问题。为了更好地说明,我将提供一个最小的工作示例。

这是我的库存文件

# inventory
[local]
localhost ansible_host=127.0.0.1 ansible_connection=local

[ceph_osd]
node1 ansible_host=192.168.1.2 rules="{'rule1': {'bcache': 'none','disks': '["/dev/sda","/dev/sdc","/dev/sdd","/dev/sde"]'}}"
node2 ansible_host=192.168.1.3 rules="{'rule1': {'bcache': 'none',"/dev/sde"]'}}"
node3 ansible_host=192.168.1.4 rules="{'rule1': {'bcache': 'none',"/dev/sde"]'}}"

我的 jinja2 模板文件

#!/bin/sh

createOSD() {
{% for host in groups['ceph_osd'] %}
{% for rule in hostvars[host].rules.values() %}
{% if rule.bcache != 'none' %}
    ssh {{ host }} make-bcache -B {{ rule.disks | join(' ') }} -C {{ rule.bcache }} --wipe-bcache
    bcache_name=$(lsblk -o KNAME {{ rule.bcache }} | grep bcache)
    ssh {{ host }} echo writeback > /sys/block/$bcache_name/bcache/cache_mode
    ceph-deploy osd create --data /dev/$bcache_name {{ host }}
{% else %}
echo {{rule.disks}}
{% for disk in rule.disks %}
    ceph-deploy osd create --data {{ disk }} {{ host }}
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}
}

createOSD

最后,我的 deploy.yml 在下面:

# deploy.yml file
---
- hosts: 127.0.0.1
  connection: local
  gather_facts: False
  name: render jinja2 template
  tasks:
  - name: render all template_out template files in local
    template: src=./deploy_ceph_cluster.sh.j2 dest=deploy_ceph_cluster.sh
...

您可以使用以下命令来渲染我的模板文件

$ ansible-playbook deploy.yml -i inventory 

PLAY [render jinja2 template] **************************************************

TASK [render all template_out template files in local] *************************
ok: [localhost]

PLAY RECAP *********************************************************************
localhost                  : ok=1    changed=0    unreachable=0    Failed=0    skipped=0    rescued=0    ignored=0   

到目前为止一切顺利。但是 deploy_ceph_cluster.sh 的上下文是错误的。其上下文如下:

#!/bin/sh

createOSD() {
echo [/dev/sda,/dev/sdc,/dev/sdd,/dev/sde]
    ceph-deploy osd create --data [ node1
    ceph-deploy osd create --data / node1
    ceph-deploy osd create --data d node1
    ceph-deploy osd create --data e node1
    ceph-deploy osd create --data v node1
    ceph-deploy osd create --data / node1
    ceph-deploy osd create --data s node1
    ceph-deploy osd create --data d node1
    ceph-deploy osd create --data a node1
    ceph-deploy osd create --data,node1
    ceph-deploy osd create --data   node1
    ceph-deploy osd create --data / node1
    ceph-deploy osd create --data d node1
    ceph-deploy osd create --data e node1
    ceph-deploy osd create --data v node1
    ceph-deploy osd create --data / node1
    ceph-deploy osd create --data s node1
    ceph-deploy osd create --data d node1
    ceph-deploy osd create --data c node1
    ceph-deploy osd create --data,node1
    ceph-deploy osd create --data   node1
    ceph-deploy osd create --data / node1
    ceph-deploy osd create --data d node1
    ceph-deploy osd create --data e node1
    ceph-deploy osd create --data v node1
    ceph-deploy osd create --data / node1
    ceph-deploy osd create --data s node1
    ceph-deploy osd create --data d node1
    ceph-deploy osd create --data d node1
    ceph-deploy osd create --data,node1
    ceph-deploy osd create --data   node1
    ceph-deploy osd create --data / node1
    ceph-deploy osd create --data d node1
    ceph-deploy osd create --data e node1
    ceph-deploy osd create --data v node1
    ceph-deploy osd create --data / node1
    ceph-deploy osd create --data s node1
    ceph-deploy osd create --data d node1
    ceph-deploy osd create --data e node1
    ceph-deploy osd create --data ] node1
echo [/dev/sda,/dev/sde]
    ceph-deploy osd create --data [ node2
    ceph-deploy osd create --data / node2
    ceph-deploy osd create --data d node2
    ceph-deploy osd create --data e node2
    ceph-deploy osd create --data v node2
    ceph-deploy osd create --data / node2
    ceph-deploy osd create --data s node2
    ceph-deploy osd create --data d node2
    ceph-deploy osd create --data a node2
    ceph-deploy osd create --data,node2
    ceph-deploy osd create --data   node2
    ceph-deploy osd create --data / node2
    ceph-deploy osd create --data d node2
    ceph-deploy osd create --data e node2
    ceph-deploy osd create --data v node2
    ceph-deploy osd create --data / node2
    ceph-deploy osd create --data s node2
    ceph-deploy osd create --data d node2
    ceph-deploy osd create --data c node2
    ceph-deploy osd create --data,node2
    ceph-deploy osd create --data   node2
    ceph-deploy osd create --data / node2
    ceph-deploy osd create --data d node2
    ceph-deploy osd create --data e node2
    ceph-deploy osd create --data v node2
    ceph-deploy osd create --data / node2
    ceph-deploy osd create --data s node2
    ceph-deploy osd create --data d node2
    ceph-deploy osd create --data d node2
    ceph-deploy osd create --data,node2
    ceph-deploy osd create --data   node2
    ceph-deploy osd create --data / node2
    ceph-deploy osd create --data d node2
    ceph-deploy osd create --data e node2
    ceph-deploy osd create --data v node2
    ceph-deploy osd create --data / node2
    ceph-deploy osd create --data s node2
    ceph-deploy osd create --data d node2
    ceph-deploy osd create --data e node2
    ceph-deploy osd create --data ] node2
echo [/dev/sda,/dev/sde]
    ceph-deploy osd create --data [ node3
    ceph-deploy osd create --data / node3
    ceph-deploy osd create --data d node3
    ceph-deploy osd create --data e node3
    ceph-deploy osd create --data v node3
    ceph-deploy osd create --data / node3
    ceph-deploy osd create --data s node3
    ceph-deploy osd create --data d node3
    ceph-deploy osd create --data a node3
    ceph-deploy osd create --data,node3
    ceph-deploy osd create --data   node3
    ceph-deploy osd create --data / node3
    ceph-deploy osd create --data d node3
    ceph-deploy osd create --data e node3
    ceph-deploy osd create --data v node3
    ceph-deploy osd create --data / node3
    ceph-deploy osd create --data s node3
    ceph-deploy osd create --data d node3
    ceph-deploy osd create --data c node3
    ceph-deploy osd create --data,node3
    ceph-deploy osd create --data   node3
    ceph-deploy osd create --data / node3
    ceph-deploy osd create --data d node3
    ceph-deploy osd create --data e node3
    ceph-deploy osd create --data v node3
    ceph-deploy osd create --data / node3
    ceph-deploy osd create --data s node3
    ceph-deploy osd create --data d node3
    ceph-deploy osd create --data d node3
    ceph-deploy osd create --data,node3
    ceph-deploy osd create --data   node3
    ceph-deploy osd create --data / node3
    ceph-deploy osd create --data d node3
    ceph-deploy osd create --data e node3
    ceph-deploy osd create --data v node3
    ceph-deploy osd create --data / node3
    ceph-deploy osd create --data s node3
    ceph-deploy osd create --data d node3
    ceph-deploy osd create --data e node3
    ceph-deploy osd create --data ] node3
}

createOSD

如您所见,每个磁盘变量都类似于 [/dev/sda,/dev/sde],但是在下面的代码中,它被拆分为字符,例如, /dev/sda 被拆分为“/”、“d”, 'e','v' 巴拉巴拉...

createOSD() {
{% for host in groups['ceph_osd'] %}
{% for rule in hostvars[host].rules.values() %}
...  omit some irrelevant code
{% for disk in rule.disks %}
    ceph-deploy osd create --data {{ disk }} {{ host }}
{% endfor %}
{% endfor %}
{% endfor %}
}

我有什么问题吗?谁能给我一些提示或建议?提前致谢。

解决方法

{...,'disks': '["/dev/sda","/dev/sdc","/dev/sdd","/dev/sde"]'}

'disks' 的值用引号括起来,因此实际上它是 str 而不是 list。您需要删除外部引号:

{...,'disks': ['/dev/sda','/dev/sdc','/dev/sdd','/dev/sde']}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。