目录
- 概述
- 实践
- 代码
- 执行效果
概述
简单实用版本
实践
代码
---
- name: Get IP Addresshosts: allgather_facts: notasks:- name: Get IP Addressansible.builtin.setup:register: host_ip- name: Print IP Addressansible.builtin.debug:msg: "The IP Address of {{ ansible_hostname }} is {{ host_ip.ansible_facts.ansible_default_ipv4.address }}"# - name: 打印主机的相关变量(打印的太多)
# debug:
# msg:
# - "{{ hostvars[inventory_dir] }} {{ hostvars[inventory_hostname] }}"- name: 打印主机的相关变量debug:msg:- "inventory_dir={{ inventory_dir }} inventory_hostname={{ inventory_hostname }} group_names={{ group_names }} groups={{ groups }} 当前playbook会在哪些hosts上运行={{ play_hosts }}"
执行效果
[root@ks8u-k8s-test01 k8s-ansible]# ansible-playbook -i hosts test-ip.yml PLAY [Get IP Address] **************************************************************************************************************************************************************TASK [Get IP Address] **************************************************************************************************************************************************************
ok: [10.32.36.51]
ok: [10.32.36.131]TASK [Print IP Address] ************************************************************************************************************************************************************
ok: [10.32.36.51] => {"msg": "The IP Address of ks8u-k8s-test01 is 10.32.36.51"
}
ok: [10.32.36.131] => {"msg": "The IP Address of KS8P-Test-K8S01 is 10.32.36.131"
}TASK [打印主机的相关变量] *******************************************************************************************************************************************************************
ok: [10.32.36.51] => {"msg": ["inventory_dir=/root/k8s-ansible inventory_hostname=10.32.36.51 group_names=[u'k8s', u'nodes'] groups={'ungrouped': [], 'all': [u'10.32.36.51', u'10.32.36.131'], u'k8s': [u'10.32.36.51', u'10.32.36.131'], u'nodes': [u'10.32.36.51', u'10.32.36.131']} 当前playbook会在哪些hosts上运行=[u'10.32.36.51', u'10.32.36.131']"]
}
ok: [10.32.36.131] => {"msg": ["inventory_dir=/root/k8s-ansible inventory_hostname=10.32.36.131 group_names=[u'k8s', u'nodes'] groups={'ungrouped': [], 'all': [u'10.32.36.51', u'10.32.36.131'], u'k8s': [u'10.32.36.51', u'10.32.36.131'], u'nodes': [u'10.32.36.51', u'10.32.36.131']} 当前playbook会在哪些hosts上运行=[u'10.32.36.51', u'10.32.36.131']"]
}PLAY RECAP *************************************************************************************************************************************************************************
10.32.36.131 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
10.32.36.51 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0