ansible变量:你可以动态创建它们然后循环它们吗?

时间:2016-05-19 18:36:18

标签: ansible ansible-playbook ansible-2.x

我有以下剧本:

---
- 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}}"

调试返回如下值:

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>

我该怎么做呢?

2 个答案:

答案 0 :(得分:0)

你可以试试这个:

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

答案 1 :(得分:0)

这就是最终的工作:

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