ansible变量:你能动态创建它们然后循环遍历它们吗?

ansible variables: can you create them dynamically and then loop through them?

我有以下剧本:

---
- hosts: lxc_hosts
  name:  get list of lxc containers on lxc host
  tasks:
  - name: get list of containers
    shell: >
      lxc-ls | awk -vRS= -vFS="\n" '//'
    register: containers
  - debug: msg="{{containers.stdout}}"

调试 returns 值如下:

TASK [debug] *******************************************************************
ok: [10.1.1.1] => {
    "msg": "container1\ncontainer2\ncontainer3"
}
ok: [10.1.1.2] => {
    "msg": "container22\ncontainer23\ncontainer24"
}

我希望有一种方法可以将我返回的结果解析成这样的列表:

container1
container2
container3

和/或

container22
container23
container24

然后我希望能够以某种方式循环遍历这些列表并 运行 与他们进行另一场比赛。像这样:

shell:  lxc-attach --name={{item}}
register: attach_results
with_items:  <list of containers>

我该怎么做?

你能试试这个吗:

  - debug:
      msg: "{{ containers.stdout | select("match", ". container*") | list }}"

这就是最终的效果:

 - debug: msg="{{containers.stdout}}"
   with_items:
     - "{{ containers.stdout.split(',')|select('match', 'container*')|list }}"