Inventory变量(在主机清单配置文件中定义变量)
[root@contorl ansible]# vim hosts
[test]
node1 myvar1=“hello the world” myvar2=“content” #在node1主机中定义了两个变量
[proxy]
node2
[webserver]
node[3:4]
[database]
node5
[cluster:children]
webserver
database
[webserver:vars]
yourname=“jacob” #在webserver主机组所有主机定义变量
[root@contorl ansible]# vim inventory_var.yaml
---
- hosts: test
tasks:
- name: create a file with var.
shell: echo "{{myvar1}}" > /tmp/"{{myvar2}}"
- hosts: webserver
tasks:
- name: create a user with var
user:
name: "{{yourname}}"
[root@contorl ansible]# ansible-playbook inventory_var.yaml
[root@node1 ~]# ls /tmp
[root@node1 ~]# cat /tmp/content
[root@node3 ~]# id jocab
文章介绍了如何在Ansible的主机清单文件中定义变量,如在node1上设置myvar1和myvar2,以及在webserver组中定义yourname变量。通过playbook执行任务,如在test组主机创建带有变量的文件,在webserver组主机创建用户。展示了Ansible如何跨主机进行配置管理。

被折叠的 条评论
为什么被折叠?



