ansible 一次循环做多个task和追加list

ansible 一次循环做多个task和追加list

ansible 一次循环做多个task和追加list

问题1

ansible 能否一个loop里面 先执行一个task 得到输出, 然后用这个输出执行第二个task 完成一次循环?

通过 include 完成

一次循环 完成两个任务,你需要将这两个任务做成一个task.yml
然后循环include

比如:

文件1: includeFile.yml

- name: Finding the package.
shell: rpm -qa | grep "{{pkgName}}"
register: package

- name: Uninstalling the package.
shell: rpm -e "{{package}}"

文件2:

  ---
- hosts: all
vars:
- packageList : ["pkg1","pkg2","pkg3","pkg4"]

tasks:
- include: includeFile.yml pkgName="{{item}}"
with_items: "{{ packageList }}"

这样就能一次循环完成两个动作

改进一下,更复杂的对象:

includeFile.yml:
- name: run shell
shell: echo "{{pkgName.name}} {{pkgName.disk}}"
register: pkg

- name: echo
debug:
msg: "{{ pkg.stdout }}"
upandrun.yml:
- hosts: 10.xxxx
vars:
- packageList :
- { name: "pkg1", disk: "haha1j" }
- { name: "pkg2", disk: "hahax" }
- { name: "pkg3", disk: "hahad" }
- { name: "pkg4", disk: "haha5" }
tasks:
- include: includeFile.yml pkgName="{{ item }}"
with_items: "{{ packageList }}"

结果:


PLAY [1xxxx] ********************************************************************************************************************************************************************

TASK [exe] ***************************************************************************************************************************************************************************
changed: [10xxx]

TASK [debug] *************************************************************************************************************************************************************************
ok: [10.1xxxx2] => {
"msg": " 16:54:13 up 6:20, 3 users, load average: 0.00, 0.01, 0.05\r\nxx\r\n"
}

TASK [include] ***********************************************************************************************************************************************************************
included: /home/gcpadmin/ansible_work/adhoc/includeFile.yml for 10.10.1.72
included: /home/gcpadmin/ansible_work/adhoc/includeFile.yml for 10.10.1.72
included: /home/gcpadmin/ansible_work/adhoc/includeFile.yml for 10.10.1.72
included: /home/gcpadmin/ansible_work/adhoc/includeFile.yml for 10.10.1.72

TASK [run shell] *********************************************************************************************************************************************************************
changed: [10.xxxx]

TASK [echo] **************************************************************************************************************************************************************************
ok: [10xxxx] => {
"msg": "pkg1 haha1j"
}

TASK [run shell] *********************************************************************************************************************************************************************
changed: [10.xxxx]

TASK [echo] **************************************************************************************************************************************************************************
ok: [10.xxx] => {
"msg": "pkg2 hahax"
}

TASK [run shell] *********************************************************************************************************************************************************************
changed: [10.xxxx]

TASK [echo] **************************************************************************************************************************************************************************
ok: [10xxx] => {
"msg": "pkg3 hahad"
}

TASK [run shell] *********************************************************************************************************************************************************************
changed: [10xxx]

TASK [echo] **************************************************************************************************************************************************************************
ok: [10xxx] => {
"msg": "pkg4 haha5"
}

PLAY RECAP ***************************************************************************************************************************************************************************
10.xxx : ok=14 changed=5 unreachable=0 failed=0

问题2

能否一次loop后将结果追加到一个dict或者list等。 第二次loop时使用?

Appending a single item to a list is trickier, especially when it’s a string. Here’s how to do it:

- name: Initialize an empty list for our strings
set_fact:
my_strings: []

- name: Setup a string variable
set_fact:
my_name: "Max"

- name: Append string to list
set_fact:
my_strings: "{{ my_strings }} + [ '{{ my_name }}' ]"

- debug: var=my_strings

- name: Append another item to the list
set_fact:
my_strings: "{{ my_strings }} + [ 'Power' ]"

- debug: var=my_strings

The output will be:

TASK [Initialize an empty list for our strings] ********************************
ok: [localhost]

TASK [Setup a string variable] *************************************************
ok: [localhost]

TASK [Append string to list] ***************************************************
ok: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
"my_strings": [
"Max"
]
}

TASK [Append another item to the list] *****************************************
ok: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
"my_strings": [
"Max",
"Power"
]
}

问题3 如何合并两个list

How to merge two lists together:

- debug: msg="Append list to list, or merge two lists"

- name: Setup two lists to be merged
set_fact:
list_one:
- 1
- 2
- 3
list_two:
- 4
- 5
- 6

- name: Merge the two lists
set_fact:
lists_merged: "{{ list_one }} + {{ list_two }}"

- name: Demonstrate merged lists
debug: var=lists_merged

The output will be:

TASK [debug] *******************************************************************
ok: [localhost] => {
"msg": "Append list to list, or merge two lists"
}

TASK [Setup two lists to be merged] ********************************************
ok: [localhost]

TASK [Merge the two lists] *****************************************************
ok: [localhost]

TASK [Demonstrate merged lists] ************************************************
ok: [localhost] => {
"lists_merged": [
1,
2,
3,
4,
5,
6
]
}