渲染一个模板到远端,渲染结果应该只包含任务所在节点的信息运行

Rendering a template to the remote end and the rendered result should only contain information about the node on which the task is running

这里是这个问题的背景信息:我有一个清单,其中包含有关远程节点的信息。我还有一个模板文件。我正在尝试将此模板渲染到远程端,渲染结果应该只包含有关其目标的信息。例如,当我将模板渲染为 ops1 时,结果应该只包含在我的 Inventory 文件中定义的信息([ceph_osd] - ops1 - rules

这是我的库存文件:

[cluster]
ops1 ansible_host=192.168.15.80
ops2 ansible_host=192.168.15.81
ops3 ansible_host=192.168.15.82

[ceph_osd]
ops1 rules="{'rule1':{'bcache': '/dev/nvme1n1p7', 'disks': ['/dev/sdb']},'rule2':{'bcache': '/dev/nvme1n1p8', 'disks': ['/dev/sdc']}}"
ops2 rules="{'rule1':{'bcache': '/dev/nvme1n1p7', 'disks': ['/dev/sdb']},'rule2':{'bcache': '/dev/nvme1n1p8', 'disks': ['/dev/sdd']}}"
ops3 rules="{'rule1':{'bcache': '/dev/nvme1n1p7', 'disks': ['/dev/sdc']},'rule2':{'bcache': '/dev/nvme1n1p8', 'disks': ['/dev/sdf']}}"

这是我的 jinja2 模板文件:

# clean_bcache.sh.j2
#!/bin/sh

{% for host in groups['ceph_osd'] %}
{% for rule in hostvars[host].rules.values() %}
{% if rule.bcache != 'none' %}
    bcache_name=$(lsblk -o KNAME {{ rule.bcache }} | grep bcache)
    cset.uuid=$(bcache-super-show {{ rule.bcache }} | grep cset.uuid | awk -F' ' '{print }')
    echo ${cset.uuid} > /sys/block/$bcache_name/bcache/detach
    sleep 1
    echo 1 >/sys/fs/bcache/${cset.uuid}/unregister 
    sleep 1
{% for backing_dev in rule.disks %}
    echo 1 > /sys/block/{{backing_dev}}/bcache/stop
    
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}

我的ansible任务如下:

- hosts: cluster
  name: start bcache deployment
  gather_facts: False
  tasks:
  - name: template
    template: src=./clean_bcache.sh.j2 dest=/opt/clean_bcache.sh

执行该任务后,所有渲染文件的内容都一样,如下图。

# clean_bcache.sh
#!/bin/sh
    #########################
    bcache_name=$(lsblk -o KNAME /dev/nvme1n1p9 | grep bcache)
    cset.uuid=$(bcache-super-show /dev/nvme1n1p9 | grep cset.uuid | awk -F' ' '{print }')
    echo ${cset.uuid} > /sys/block/$bcache_name/bcache/detach
    sleep 1
    echo 1 >/sys/fs/bcache/${cset.uuid}/unregister 
    sleep 1
    echo 1 > /sys/block//dev/sdg/bcache/stop
    
    bcache_name=$(lsblk -o KNAME /dev/nvme1n1p8 | grep bcache)
    cset.uuid=$(bcache-super-show /dev/nvme1n1p8 | grep cset.uuid | awk -F' ' '{print }')
    echo ${cset.uuid} > /sys/block/$bcache_name/bcache/detach
    sleep 1
    echo 1 >/sys/fs/bcache/${cset.uuid}/unregister 
    sleep 1
    echo 1 > /sys/block//dev/sdc/bcache/stop
    
    #########################
    bcache_name=$(lsblk -o KNAME /dev/nvme1n1p9 | grep bcache)
    cset.uuid=$(bcache-super-show /dev/nvme1n1p9 | grep cset.uuid | awk -F' ' '{print }')
    echo ${cset.uuid} > /sys/block/$bcache_name/bcache/detach
    sleep 1
    echo 1 >/sys/fs/bcache/${cset.uuid}/unregister 
    sleep 1
    echo 1 > /sys/block//dev/sdc/bcache/stop
    bcache_name=$(lsblk -o KNAME /dev/nvme1n1p8 | grep bcache)
    cset.uuid=$(bcache-super-show /dev/nvme1n1p8 | grep cset.uuid | awk -F' ' '{print }')
    echo ${cset.uuid} > /sys/block/$bcache_name/bcache/detach
    sleep 1
    echo 1 >/sys/fs/bcache/${cset.uuid}/unregister 
    sleep 1
    echo 1 > /sys/block//dev/sdd/bcache/stop
  
    #########################
    bcache_name=$(lsblk -o KNAME /dev/nvme1n1p9 | grep bcache)
    cset.uuid=$(bcache-super-show /dev/nvme1n1p9 | grep cset.uuid | awk -F' ' '{print }')
    echo ${cset.uuid} > /sys/block/$bcache_name/bcache/detach
    sleep 1
    echo 1 >/sys/fs/bcache/${cset.uuid}/unregister 
    sleep 1
    echo 1 > /sys/block//dev/sdg/bcache/stop
    bcache_name=$(lsblk -o KNAME /dev/nvme1n1p8 | grep bcache)
    cset.uuid=$(bcache-super-show /dev/nvme1n1p8 | grep cset.uuid | awk -F' ' '{print }')
    echo ${cset.uuid} > /sys/block/$bcache_name/bcache/detach
    sleep 1
    echo 1 >/sys/fs/bcache/${cset.uuid}/unregister 
    sleep 1
    echo 1 > /sys/block//dev/sdf/bcache/stop
   

我要做的是将这个文件渲染成三个不同的文件,分别位于不同的节点上。我想达到的目标如下:

远程 ops1 上的 clean_bcache.sh 应该如下所示。可以看到,上面的Inventory

里面只包含了ops1相关的信息
#!/bin/sh
    bcache_name=$(lsblk -o KNAME /dev/nvme1n1p9 | grep bcache)
    cset.uuid=$(bcache-super-show /dev/nvme1n1p9 | grep cset.uuid | awk -F' ' '{print }')
    echo ${cset.uuid} > /sys/block/$bcache_name/bcache/detach
    sleep 1
    echo 1 >/sys/fs/bcache/${cset.uuid}/unregister 
    sleep 1
    echo 1 > /sys/block//dev/sdg/bcache/stop
    
    bcache_name=$(lsblk -o KNAME /dev/nvme1n1p8 | grep bcache)
    cset.uuid=$(bcache-super-show /dev/nvme1n1p8 | grep cset.uuid | awk -F' ' '{print }')
    echo ${cset.uuid} > /sys/block/$bcache_name/bcache/detach
    sleep 1
    echo 1 >/sys/fs/bcache/${cset.uuid}/unregister 
    sleep 1
    echo 1 > /sys/block//dev/sdc/bcache/stop

这是 ops2 上的 clean_bcache.sh

#!/bin/sh
    bcache_name=$(lsblk -o KNAME /dev/nvme1n1p9 | grep bcache)
    cset.uuid=$(bcache-super-show /dev/nvme1n1p9 | grep cset.uuid | awk -F' ' '{print }')
    echo ${cset.uuid} > /sys/block/$bcache_name/bcache/detach
    sleep 1
    echo 1 >/sys/fs/bcache/${cset.uuid}/unregister 
    sleep 1
    echo 1 > /sys/block//dev/sdc/bcache/stop
    bcache_name=$(lsblk -o KNAME /dev/nvme1n1p8 | grep bcache)
    cset.uuid=$(bcache-super-show /dev/nvme1n1p8 | grep cset.uuid | awk -F' ' '{print }')
    echo ${cset.uuid} > /sys/block/$bcache_name/bcache/detach
    sleep 1
    echo 1 >/sys/fs/bcache/${cset.uuid}/unregister 
    sleep 1
    echo 1 > /sys/block//dev/sdd/bcache/stop

这是 ops3

上的 clean_bcache.sh
#!/bin/sh
    bcache_name=$(lsblk -o KNAME /dev/nvme1n1p9 | grep bcache)
    cset.uuid=$(bcache-super-show /dev/nvme1n1p9 | grep cset.uuid | awk -F' ' '{print }')
    echo ${cset.uuid} > /sys/block/$bcache_name/bcache/detach
    sleep 1
    echo 1 >/sys/fs/bcache/${cset.uuid}/unregister 
    sleep 1
    echo 1 > /sys/block//dev/sdg/bcache/stop
    bcache_name=$(lsblk -o KNAME /dev/nvme1n1p8 | grep bcache)
    cset.uuid=$(bcache-super-show /dev/nvme1n1p8 | grep cset.uuid | awk -F' ' '{print }')
    echo ${cset.uuid} > /sys/block/$bcache_name/bcache/detach
    sleep 1
    echo 1 >/sys/fs/bcache/${cset.uuid}/unregister 
    sleep 1
    echo 1 > /sys/block//dev/sdf/bcache/stop

我用谷歌搜索了很多,但 none 有用。

有什么想法吗?提前致谢!!

要解决这个问题,你只需要在你的模板文件中添加一个if语句if host == inventory_hostname

#!/bin/sh

{% for host in groups['ceph_osd'] %}
{% if host == inventory_hostname %}
{% for rule in hostvars[host].rules.values() %}
{% if rule.bcache != 'none' %}
    bcache_name=$(lsblk -o KNAME {{ rule.disks | first }} | grep bcache)
    cset_uuid=$(bcache-super-show {{ rule.bcache }} | grep cset.uuid | awk -F' ' '{print }')
    echo $cset_uuid > /sys/block/$bcache_name/bcache/detach
    sleep 1
    echo 1 >/sys/fs/bcache/$cset_uuid/unregister 
    sleep 1
{% for backing_dev in rule.disks %}
    echo 1 > /sys/block/{{backing_dev.split("/") | last}}/bcache/stop
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}