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

When I iterated a string list which generate by jinja2, I get a series of chars rather than strings. Why this happend?

我一直在尝试通过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', 'disks': '["/dev/sda", "/dev/sdc", "/dev/sdd", "/dev/sde"]'}}"
node3 ansible_host=192.168.1.4 rules="{'rule1': {'bcache': 'none', 'disks': '["/dev/sda", "/dev/sdc", "/dev/sdd", "/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/sdc, /dev/sdd, /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/sdc, /dev/sdd, /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/sdc, /dev/sdd, /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']}