文章目錄
- 1. 介紹
- 2. 示例
- 2.1 定義并輸出變量
- 2.2 傳回值設定變量
- 2.3 跨play調用變量
–
1. 介紹
set_fact子產品在tasks中定義變量
2. 示例
2.1 定義并輸出變量
set_fact.yaml
---
- hosts: localhost
remote_user: root
tasks:
- set_fact:
test: "123456"
- debug:
msg: "{{test}}"
執行輸出:
ansible-playbook set_fact.yaml
PLAY [localhost] *******************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [set_fact] ********************************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [debug] ***********************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "123456"
}
PLAY RECAP *************************************************************************************************************************************************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
2.2 傳回值設定變量
set_fact2.yaml
- hosts: localhost
remote_user: root
vars:
test1: "123456"
tasks:
- shell: echo "hello world"
register: result
- set_fact:
test_one: "{{test1}}"
test_two: "{{result.stdout}}"
- debug:
msg: " test_one is {{test_one}}; test_two is {{test_two}}"
$ ansible-playbook set_fact2.yaml
PLAY [localhost] *******************************************************************************************************************************************************************************************************************************************
TASK [Gathering Facts] *************************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [shell] ***********************************************************************************************************************************************************************************************************************************************
changed: [localhost]
TASK [set_fact] ********************************************************************************************************************************************************************************************************************************************
ok: [localhost]
TASK [debug] ***********************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": " test_one is 123456; test_two is hello world"
}
PLAY RECAP *************************************************************************************************************************************************************************************************************************************************
localhost : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0