假如有这样的需求,要求我们在所以机器上设置一个变量,变量的名称就叫做zhiming,他的值就是主机名,(现实中不存在这样的需求,因为主机名本身就可以去到,但是我们可以定义一个变量来放一些本来没有的,比如定义一个购买时间啊,升级时间什么的),步骤如下:
1:在第一个play中,通过set_fact来定义变量
2:在第二个play中,使用这些变量
---
- hosts: test_node
remote_user: root
gather_facts: no
tasks:
- name: set zhiming on every node
set_fact:
zhiming: '{{inventory_hostname}}'
- hosts: test_node
remote_user: root
gather_facts: no
tasks:
- name: output the value of zhiming
debug: msg="the value is {{zhiming}}"
Latest posts by Zhiming Zhang (see all)
- aws eks node 自动化扩展工具 Karpenter - 8月 10, 2022
- ReplicationController and ReplicaSet in Kubernetes - 12月 20, 2021
- public key fingerprint - 5月 27, 2021