[Ansible系列]ansible内置主机变量及魔法变量
ansible,自动化,运维,devops
目录
前言
ansible变量种类有很多,其中我们前面提到过的有:inventory中定义 主机变量或主机组变量,playbook中定义vars变量或者引用varsfile变量,register接收被控端命令执行的返回结果作为变量,set_fact变量,lookup定义变量等等,但是我们忽略了ansible内置的一些基础变量,这篇文章就展开说说ansible的内置变量。
内置主机变量
所谓内置变量其实就是ansible.cfg配置文件中的选项,在其前加上 ansible_ 即成为内置变量。当然 内置变拥有比ansible.cfg中选项更高的优先级,而且针对不同的主机,可以定义不同的值。 之所以叫内置主机变量,其实就是这些变量一般写在inventory的主机或主机组中。
# 一般连接
ansible_host #用于指定被管理的主机的真实IP
ansible_port #用于指定连接到被管理主机的ssh端口号,默认是22
ansible_user #ssh连接时默认使用的用户名
# 特定ssh连接
ansible_connection #SSH连接的类型:local, ssh, paramiko,在ansible 1.2 之前默认是
paramiko,后来智能选择,优先使用基于ControlPersist的ssh(如果支持的话)
ansible_ssh_pass #ssh连接时的密码
ansible_ssh_private_key_file #秘钥文件路径,如果不想使用ssh‐agent管理秘钥文件时可以使用此选项
ansible_ssh_executable #如果ssh指令不在默认路径当中,可以使用该变量来定义其路径
# 特权升级
ansible_become #相当于ansible_sudo或者ansible_su,允许强制特权升级
ansible_become_user #通过特权升级到的用户,相当于ansible_sudo_user或者ansible_su_user
ansible_become_pass # 提升特权时,如果需要密码的话,可以通过该变量指定,相当于
ansible_sudo_pass或者ansible_su_pass
ansible_sudo_exec #如果sudo命令不在默认路径,需要指定sudo命令路径
# 远程主机环境参数
ansible_shell_executable # 设置目标机上使用的shell,默认为/bin/sh
ansible_python_interpreter #用来指定python解释器的路径,默认为/usr/bin/python 同样可以指
定ruby 、perl 的路径
ansible_*_interpreter #其他解释器路径,用法与ansible_python_interpreter类似,这里"*"可以
是ruby或才perl等其他语
示例: 定义server1主机的ansible_host变量,以及定义主机组[init_server]的组变量
[init_server]
server1 ansible_host=192.168.194.129
192.168.194.130
192.168.194.131
[init_server:vars]
ansible_ssh_pass=123
#ansible_ask_pass=False
#ansible_user=root
魔法变量
Ansible默认会提供一些内置的变量以实现一些特定的功能,我们称之为魔法变量。
1. hostvars
获取inventory中定义的所有主机的相关变量。
例如:hosts文件如下
192.168.194.131
[init_server]
192.168.194.129
192.168.194.130
以下playbook将会在获取到129,130和131上的相关变量。
- hosts: init_server
gather_facts: no
tasks:
- name: debug
debug:
msg:
- "{{ hostvars }}"
返回值是json串,我们可以取相应的值。
2. inventory_hostname
inventory_hostname是Ansible所识别的当前正在运行task的主机的主机名。如果在inventory里定 义过别名,那么这里就是那个别名,如果inventory包含如下一行:
server1 ansible_ssh_host=192.168.194.129
则 inventory_hostname 即为 server1 。
示例1: 利用 hostvars 和 inventory_hostname 变量,可以输出与当前主 机相关联的所有变量
## hosts文件:
[init_server]
server1 ansible_ssh_host=192.168.194.129
192.168.194.130
##plsybook
- hosts: init_server
gather_facts: no
tasks:
- name: debug
debug:
msg:
- "{{ hostvars[inventory_hostname] }}"
##获取所有内容:
[root@clinet ansible_2]# ansible-playbook yum_file/mo_fa/hostavar.yml
PLAY [init_server] **************************************************************************************************************************************************
TASK [debug] ********************************************************************************************************************************************************
ok: [server1] => {
"msg": [
{
"ansible_check_mode": false,
"ansible_diff_mode": false,
"ansible_facts": {},
"ansible_forks": 5,
"ansible_inventory_sources": [
"/root/ansible_test/ansible_2/hosts"
],
"ansible_playbook_python": "/usr/bin/python2",
"ansible_run_tags": [
"all"
],
"ansible_skip_tags": [],
"ansible_ssh_host": "192.168.194.129",
"ansible_verbosity": 0,
"ansible_version": {
"full": "2.9.27",
"major": 2,
"minor": 9,
"revision": 27,
"string": "2.9.27"
},
"group_names": [
"init_server"
],
"groups": {
"all": [
"192.168.194.131",
"server1",
"192.168.194.130"
],
"init_server": [
"server1",
"192.168.194.130"
],
"ungrouped": [
"192.168.194.131"
]
},
"inventory_dir": "/root/ansible_test/ansible_2",
"inventory_file": "/root/ansible_test/ansible_2/hosts",
"inventory_hostname": "server1",
"inventory_hostname_short": "server1",
"playbook_dir": "/root/ansible_test/ansible_2/yum_file/mo_fa"
}
]
}
ok: [192.168.194.130] => {
"msg": [
{
"ansible_check_mode": false,
"ansible_diff_mode": false,
"ansible_facts": {},
"ansible_forks": 5,
"ansible_inventory_sources": [
"/root/ansible_test/ansible_2/hosts"
],
"ansible_playbook_python": "/usr/bin/python2",
"ansible_run_tags": [
"all"
],
"ansible_skip_tags": [],
"ansible_verbosity": 0,
"ansible_version": {
"full": "2.9.27",
"major": 2,
"minor": 9,
"revision": 27,
"string": "2.9.27"
},
"group_names": [
"init_server"
],
"groups": {
"all": [
"192.168.194.131",
"server1",
"192.168.194.130"
],
"init_server": [
"server1",
"192.168.194.130"
],
"ungrouped": [
"192.168.194.131"
]
},
"inventory_dir": "/root/ansible_test/ansible_2",
"inventory_file": "/root/ansible_test/ansible_2/hosts",
"inventory_hostname": "192.168.194.130",
"inventory_hostname_short": "192",
"playbook_dir": "/root/ansible_test/ansible_2/yum_file/mo_fa"
}
]
}
PLAY RECAP **********************************************************************************************************************************************************
192.168.194.130 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
server1 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@clinet ansible_2]#
示例2: 直接通过inventory_hostname获取对应的hostname
[root@clinet ansible_2]# ansible init_server -m debug -a "msg='{{ inventory_hostname}}'"
server1 | SUCCESS => {
"msg": "server1"
}
192.168.194.130 | SUCCESS => {
"msg": "192.168.194.130"
}
与inventory_hostname相近的还有一个inventory_hostname_short,如果一台主机的 inventory_hostname为server1.exmaple.com,则inventory_hostname_short的值为server1;如果主机的inventory_hostname为192.168.194.129,则inventory_hostname_short为192
3. group_names
group_names返回的是主机所属主机组,如果该主机在多个组中,则返回多个组,如果它不在组 中,则返回ungrouped这个特殊组。
示例1: 返回主机所在的主机组
hosts文件如下:
192.168.194.131
[init_server]
server1 ansible_ssh_host=192.168.194.129
192.168.194.130
[web]
192.168.194.130
执行结果:
[root@clinet ansible_2]# ansible all -m debug -a "msg='{{ group_names }}'"
192.168.194.130 | SUCCESS => {
"msg": [
"init_server",
"web"
]
}
192.168.194.131 | SUCCESS => {
"msg": [
"ungrouped"
]
}
server1 | SUCCESS => {
"msg": [
"init_server"
]
}
[root@clinet ansible_2]#
4. groups
groups是inventory中所有主机组的列表,可用于枚举主机组中的所有主机。
hosts文件:
192.168.194.131
[init_server]
server1 ansible_ssh_host=192.168.194.129
192.168.194.130
[web]
192.168.194.130
执行结果;
[root@clinet ansible_2]# ansible all -m debug -a "msg='{{ groups }}'"
server1 | SUCCESS => {
"msg": {
"all": [
"192.168.194.131",
"192.168.194.130",
"server1"
],
"init_server": [
"server1",
"192.168.194.130"
],
"ungrouped": [
"192.168.194.131"
],
"web": [
"192.168.194.130"
]
}
}
192.168.194.131 | SUCCESS => {
"msg": {
"all": [
"192.168.194.131",
"192.168.194.130",
"server1"
],
"init_server": [
"server1",
"192.168.194.130"
],
"ungrouped": [
"192.168.194.131"
],
"web": [
"192.168.194.130"
]
}
}
192.168.194.130 | SUCCESS => {
"msg": {
"all": [
"192.168.194.131",
"192.168.194.130",
"server1"
],
"init_server": [
"server1",
"192.168.194.130"
],
"ungrouped": [
"192.168.194.131"
],
"web": [
"192.168.194.130"
]
}
}
[root@clinet ansible_2]#
示例2: 可以在j2文件中通过for循环遍历,获取相应组的ip
- hosts: init_server
gather_facts: no
tasks:
- name: debug
debug:
msg:
- "{{ groups }}"
- name: template
template:
src: /root/ansible_test/ansible_2/yum_file/mo_fa/tamplate.j2
dest: /tmp/xhz
{% for host in groups.all}
server {{ host }}
{% endfor %}
[root@route ~]# cat /tmp/xhz
server 192.168.194.131
server 192.168.194.130
server server1
[root@route ~]#
5. play_hosts
当前playbook会在哪些hosts上运行。
- hosts: init_server
gather_facts: no
tasks:
- name: debug
debug:
msg:
- "{{ play_hosts }}"
[root@clinet ansible_2]# ansible-playbook yum_file/mo_fa/hostavar.yml
PLAY [init_server] **************************************************************************************************************************************************
TASK [debug] ********************************************************************************************************************************************************
ok: [server1] => {
"msg": [
[
"server1",
"192.168.194.130"
]
]
}
ok: [192.168.194.130] => {
"msg": [
[
"server1",
"192.168.194.130"
]
]
}
PLAY RECAP **********************************************************************************************************************************************************
192.168.194.130 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
server1 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@clinet ansible_2]#
6. inventory_dir
机清单所在目录。
[root@clinet ansible_2]# ansible all -m debug -a "msg='{{ inventory_dir }}'"
192.168.194.131 | SUCCESS => {
"msg": "/root/ansible_test/ansible_2"
}
server1 | SUCCESS => {
"msg": "/root/ansible_test/ansible_2"
}
192.168.194.130 | SUCCESS => {
"msg": "/root/ansible_test/ansible_2"
}
[root@clinet ansible_2]#
7. inventory_file
主机清单文件。
[root@clinet ansible_2]# ansible all -m debug -a "msg='{{ inventory_file }}'"
192.168.194.131 | SUCCESS => {
"msg": "/root/ansible_test/ansible_2/hosts"
}
192.168.194.130 | SUCCESS => {
"msg": "/root/ansible_test/ansible_2/hosts"
}
server1 | SUCCESS => {
"msg": "/root/ansible_test/ansible_2/hosts"
}
[root@clinet ansible_2]#
更多推荐
所有评论(0)