ansible:ansible_ssh_user 覆盖组中主机的值
ansible: ansible_ssh_user Overriding values for hosts in a group
我有一个清单文件,其中包含多个 hosts.But 的不同登录信息,同时在剧本中调用 ansible_ssh_user 值时我只得到最后一个条目。
为什么它需要最后一个条目 ansible_ssh_user?
库存文件:
[local]
10.41.42.2 ansible_ssh_user=root ansible_ssh_pass=root123
[DSC_root]
10.41.42.2 ansible_ssh_user=root ansible_ssh_pass=root123
10.41.42.70 ansible_ssh_user=root ansible_ssh_pass=root
10.41.42.71 ansible_ssh_user=root ansible_ssh_pass=root
[DSC_admin]
10.41.42.2 ansible_ssh_user=root ansible_ssh_pass=root123
10.41.42.70 ansible_ssh_user=dscadmin ansible_ssh_pass=dscadmin
10.41.42.71 ansible_ssh_user=dscadmin ansible_ssh_pass=dscadmin
剧本:
---
- hosts: DSC_root
remote_user: root
tasks:
- name: get root user home grep and register
shell: >
grep "^{{ansible_ssh_user}}:" /etc/passwd | awk -F: '{ print }'
changed_when: false
register: v_getuserhome
- debug: var=v_getuserhome
日志:
[root@mavosdsc ansible]# ansible-playbook -i inventory1 get_home.yml
PLAY [DSC_root] ****************************************************************
TASK [setup] *******************************************************************
ok: [10.41.42.71]
ok: [10.41.42.70]
ok: [10.41.42.2]
TASK [get root user home grep and register] ************************************
ok: [10.41.42.70]
ok: [10.41.42.71]
ok: [10.41.42.2]
TASK [debug] *******************************************************************
ok: [10.41.42.70] => {
"v_getuserhome": {
"changed": false,
"cmd": "grep \"^dscadmin:\" /etc/passwd | awk -F: '{ print }'",
"delta": "0:00:00.005985",
"end": "2016-07-31 09:23:42.743393",
"rc": 0,
"start": "2016-07-31 09:23:42.737408",
"stderr": "",
"stdout": "/home/dscadmin",
"stdout_lines": [
"/home/dscadmin"
],
"warnings": []
}
}
ok: [10.41.42.2] => {
"v_getuserhome": {
"changed": false,
"cmd": "grep \"^root:\" /etc/passwd | awk -F: '{ print }'",
"delta": "0:00:00.005801",
"end": "2016-07-31 18:53:44.074155",
"rc": 0,
"start": "2016-07-31 18:53:44.068354",
"stderr": "",
"stdout": "/root",
"stdout_lines": [
"/root"
],
"warnings": []
}
}
ok: [10.41.42.71] => {
"v_getuserhome": {
"changed": false,
"cmd": "grep \"^dscadmin:\" /etc/passwd | awk -F: '{ print }'",
"delta": "0:00:00.006740",
"end": "2016-07-31 09:23:43.347801",
"rc": 0,
"start": "2016-07-31 09:23:43.341061",
"stderr": "",
"stdout": "/home/dscadmin",
"stdout_lines": [
"/home/dscadmin"
],
"warnings": []
}
}
PLAY RECAP *********************************************************************
10.41.42.2 : ok=3 changed=0 unreachable=0 failed=0
10.41.42.70 : ok=3 changed=0 unreachable=0 failed=0
10.41.42.71 : ok=3 changed=0 unreachable=0 failed=0
实际上,ansible 就是这样工作的。
当你 运行 你的剧本时; ansible-playbook -i inventory my_playbook.yml
,ansible 会按照您指定的顺序读取您在那里定义的主机及其变量。如果它遇到稍后在另一组中定义的同一主机的相同变量,即使在您的游戏中您瞄准了在清单顶部定义的一组主机,也会使用最后一个变量。
我有一个清单文件,其中包含多个 hosts.But 的不同登录信息,同时在剧本中调用 ansible_ssh_user 值时我只得到最后一个条目。
为什么它需要最后一个条目 ansible_ssh_user?
库存文件:
[local]
10.41.42.2 ansible_ssh_user=root ansible_ssh_pass=root123
[DSC_root]
10.41.42.2 ansible_ssh_user=root ansible_ssh_pass=root123
10.41.42.70 ansible_ssh_user=root ansible_ssh_pass=root
10.41.42.71 ansible_ssh_user=root ansible_ssh_pass=root
[DSC_admin]
10.41.42.2 ansible_ssh_user=root ansible_ssh_pass=root123
10.41.42.70 ansible_ssh_user=dscadmin ansible_ssh_pass=dscadmin
10.41.42.71 ansible_ssh_user=dscadmin ansible_ssh_pass=dscadmin
剧本:
---
- hosts: DSC_root
remote_user: root
tasks:
- name: get root user home grep and register
shell: >
grep "^{{ansible_ssh_user}}:" /etc/passwd | awk -F: '{ print }'
changed_when: false
register: v_getuserhome
- debug: var=v_getuserhome
日志:
[root@mavosdsc ansible]# ansible-playbook -i inventory1 get_home.yml
PLAY [DSC_root] ****************************************************************
TASK [setup] *******************************************************************
ok: [10.41.42.71]
ok: [10.41.42.70]
ok: [10.41.42.2]
TASK [get root user home grep and register] ************************************
ok: [10.41.42.70]
ok: [10.41.42.71]
ok: [10.41.42.2]
TASK [debug] *******************************************************************
ok: [10.41.42.70] => {
"v_getuserhome": {
"changed": false,
"cmd": "grep \"^dscadmin:\" /etc/passwd | awk -F: '{ print }'",
"delta": "0:00:00.005985",
"end": "2016-07-31 09:23:42.743393",
"rc": 0,
"start": "2016-07-31 09:23:42.737408",
"stderr": "",
"stdout": "/home/dscadmin",
"stdout_lines": [
"/home/dscadmin"
],
"warnings": []
}
}
ok: [10.41.42.2] => {
"v_getuserhome": {
"changed": false,
"cmd": "grep \"^root:\" /etc/passwd | awk -F: '{ print }'",
"delta": "0:00:00.005801",
"end": "2016-07-31 18:53:44.074155",
"rc": 0,
"start": "2016-07-31 18:53:44.068354",
"stderr": "",
"stdout": "/root",
"stdout_lines": [
"/root"
],
"warnings": []
}
}
ok: [10.41.42.71] => {
"v_getuserhome": {
"changed": false,
"cmd": "grep \"^dscadmin:\" /etc/passwd | awk -F: '{ print }'",
"delta": "0:00:00.006740",
"end": "2016-07-31 09:23:43.347801",
"rc": 0,
"start": "2016-07-31 09:23:43.341061",
"stderr": "",
"stdout": "/home/dscadmin",
"stdout_lines": [
"/home/dscadmin"
],
"warnings": []
}
}
PLAY RECAP *********************************************************************
10.41.42.2 : ok=3 changed=0 unreachable=0 failed=0
10.41.42.70 : ok=3 changed=0 unreachable=0 failed=0
10.41.42.71 : ok=3 changed=0 unreachable=0 failed=0
实际上,ansible 就是这样工作的。
当你 运行 你的剧本时; ansible-playbook -i inventory my_playbook.yml
,ansible 会按照您指定的顺序读取您在那里定义的主机及其变量。如果它遇到稍后在另一组中定义的同一主机的相同变量,即使在您的游戏中您瞄准了在清单顶部定义的一组主机,也会使用最后一个变量。