ansible:ansible_ssh_user覆盖组中主机的值

时间:2016-07-31 13:33:48

标签: ansible ansible-playbook ansible-2.x

我有一个库存文件,其中包含多个主机的不同登录信息。但是在playbook中调用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 $6 }'
  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 $6 }'", 
            "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 $6 }'", 
            "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 $6 }'", 
            "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   

1 个答案:

答案 0 :(得分:0)

实际上,这是安赛的工作原理。 当你运行你的剧本; ansible-playbook -i inventory my_playbook.yml,ansible按照您指定的顺序读取您在其中定义的主机及其变量。如果遇到稍后在另一个组中定义的同一主机的相同变量,则即使在您的游戏中,您也会使用最后一个变量来定位在您的广告资源顶部定义的一组主机。