ansible 模块:set_fact
作者:ghostwritten
- 2022 年 5 月 11 日
本文字数:1942 字
阅读完需:约 6 分钟

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
复制代码
划线
评论
复制
发布于: 刚刚阅读数: 2
版权声明: 本文为 InfoQ 作者【ghostwritten】的原创文章。
原文链接:【http://xie.infoq.cn/article/7728f9a19f9b55a906881d792】。文章转载请联系作者。
ghostwritten
关注
改变中国 2018.11.14 加入
虚心好学,勤奋努力,为中华之崛起而读书。










评论