ansible 学习记录(七) -- group 和 host 变量

  • inventory 文件定义
[master]
master ansible_user=root ansible_password=123456 ansible_connection=ssh

[slave]
node1 ansible_user=root ansible_password=123456 ansible_connection=ssh
node2 ansible_user=root ansible_password=123456 ansible_connection=ssh
  • main.yml 编写
---

- name: Hello World
  hosts: all
  gather_facts: no

  tasks:
    - name: test vars
      debug:
        msg: "ansible_user = {{ ansible_user }}, ansible_password = {{ ansible_password }}"
  • 指定 inventory 文件执行
[root@master ~]# ansible-playbook -i /home/ansible-study/playbook/group-host-vars/inventory/host /home/ansible-study/playbook/group-host-vars/main.yml

在这里插入图片描述

  • host 变量复用
# host
[control]
master

[control:vars]
ansible_user=root
ansible_password=123456
ansible_connection=ssh

[slave]
node1
node2

[slave:vars] 			# 指定 slave 组中变量
ansible_user=root
ansible_password=123456
ansible_connection=ssh
# main.yml
---

- name: Hello World
  hosts: control
  gather_facts: no

  tasks:
    - name: test vars
      debug:
        msg: "ansible_user = {{ ansible_user }}, ansible_password = {{ ansible_password }}"
# ansible
[root@master ~]# ansible-playbook -i /home/ansible-study/playbook/group-host-vars/inventory/host /home/ansible-study/playbook/group-host-vars/main.yml 

PLAY [Hello World] *******************************************************************************************************************************

TASK [test vars] *********************************************************************************************************************************
ok: [master] => {
    "msg": "ansible_user = root, ansible_password = 123456"
}

PLAY RECAP ***************************************************************************************************************************************
master                     : ok=1    changed=0    unreachable=0    failed=0

在这里插入图片描述

host && group 配置

  • group-host-vars/main.yml
---

- name: Hello World
  hosts: slave 			# 通过配置 "组名" || "主机名" 进行变量查找
  gather_facts: no

  tasks:
    - name: test vars
      debug:
        msg: "ansible_user = {{ ansible_user }}, ansible_password = {{ ansible_password }}, http_port = {{ http_port }}"
  • group-host-vars/inventory/host
[control] 		# 控制组
master 				# 控制组 -- master 节点

[slave] 		# 被控制组
node1 				# 被控制组 -- node1 节点
node2 				# 被控制组 -- node2 节点
  • group-host-vars/inventory/group_vars/control.yml – control 组 – 共用
# control 组 -- 配置变量
ansible_user: root
ansible_password: 123456
ansible_connection: ssh
http_port: 10080
  • group-host-vars/inventory/group_vars/slave.yml – slave 组 – 共用
# slave 组 -- 配置变量
ansible_user: root
ansible_password: 123456
ansible_connection: ssh
http_port: 80
  • group-host-vars/inventory/host_vars/node1.yml – node1 节点 – 独有
# node1 私有
http_port: 20080
  • ansible 执行
[root@master ~]# ansible-playbook -i /home/ansible-study/playbook/group-host-vars/inventory/host /home/ansible-study/playbook/group-host-vars/main.yml 

PLAY [Hello World] *******************************************************************************************************************************

TASK [test vars] *********************************************************************************************************************************
ok: [node1] => {
    "msg": "ansible_user = root, ansible_password = 123456, http_port = 20080" 		# 私有参数
}
ok: [node2] => {
    "msg": "ansible_user = root, ansible_password = 123456, http_port = 80"
}

PLAY RECAP ***************************************************************************************************************************************
node1                      : ok=1    changed=0    unreachable=0    failed=0   
node2                      : ok=1    changed=0    unreachable=0    failed=0

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值