How to pass variable from one playbook to another playbook?

2024-01-16 17:25:09

在实际工作中,我们有时候需要将一个playbook的变量传递给另外一个playbook.

这里分为两个情况,我们分别来看看

同一个目标主机之间的playbook 变量传递
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@ansible-server ~]# cat global.yaml
---
# Combine multiple playbooks
- import_playbook: test.play1.yaml
- import_playbook: test.play2.yaml
[root@ansible-server ~]#
#test.play1.yaml
---
- hosts: localhost
gather_facts: false

tasks:
- name: Register a new value
shell: echo "/etc/resolv.conf"
register: PLAY1VAR

- debug: msg="{{PLAY1VAR.stdout}}"

#test.play2.yaml
---
- hosts: localhost
gather_facts: false

tasks:
- name: Echo the output - PLAY1 variable vaule
shell: cat "{{PLAY1VAR.stdout}}" |tail -1
register: PLAY2_RESULTS

- debug: msg="{{PLAY2_RESULTS.stdout}}"

输出结果为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@ansible-server ~]# ansible-playbook -i localhost global.yaml

ansible-playbook -i localhost global.yaml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that
the implicit localhost does not match 'all'

PLAY [localhost] ***************************************************************

TASK [Register a new value] ****************************************************
changed: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
"msg": "/etc/resolv.conf"
}

PLAY [localhost] ***************************************************************

TASK [Echo the output - PLAY1 variable vaule] **********************************
changed: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] => {
"msg": "search ."
}

PLAY RECAP *********************************************************************
localhost : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
不同目标主机之间的playbook 变量传递

如果我们将test.play2.yaml的目标主机改变,test.play1.yaml的目标主机保持不变

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
> ansible-playbook -i localhost global.yaml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
[WARNING]: Could not match supplied host pattern, ignoring: 192.168.1.9

PLAY [192.168.1.9] ***********************************************************************************************************
skipping: no hosts matched

PLAY [localhost] *************************************************************************************************************

TASK [Echo the output - PLAY1 variable vaule] ********************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'PLAY1VAR' is undefined\n\nThe error appears to be in '/home/tiger/test.play2.yaml': line 6, column 6, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - name: Echo the output - PLAY1 variable vaule\n ^ here\n"}

PLAY RECAP *******************************************************************************************************************
localhost : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

怎么解决呢?

调整test.paly1.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
---
- hosts: localhost
gather_facts: false

tasks:
- name: Register a new value
shell: echo "/etc/resolv.conf"
register: PLAY1VAR

- debug: msg="{{PLAY1VAR.stdout}}"

- name: Register dummy host with variable
add_host:
name: "DUMMY_HOST"
PLAY1VAR_NEW: " {{ PLAY1VAR.stdout }}"

调整test.play2.yaml

1
2
3
4
5
6
7
8
9
10
---
- hosts: 192.168.1.9
gather_facts: false

tasks:
- name: Echo the output - PLAY1 variable vaule
shell: cat {{ hostvars['DUMMY_HOST']['PLAY1VAR_NEW'] }} |tail -1
register: PLAY2_RESULTS

- debug: msg="{{PLAY2_RESULTS.stdout}}"