使用查找插件检查 SHA512 密码哈希

Using lookup plugin to check SHA512 password hash

在我的 Ansible git 存储库中,我有一个包含如下内容的 var 文件

vault_users:
  alex:
    password: $PwhqORmvn$tXctAkh9RLs60ZFhn9Cxz/eLZEx1UhQkbDIoM6xWsk7M18TApDd9/b8CHJnEiaiQE2YJ8mqu6kvsGuImDt4dy/
  danny:
    password: $PwhqORmvn$tXctAkh9RLs60ZFhn9Cxz/eLZEx1UhQkbDIoM6xWsk7M18TApDd9/b8CHJnEiaiQE2YJ8mqu6kvsGuImDt4dy/
  gary:
    password: $PwhqORmvn$tXctAkh9RLs60ZFhn9Cxz/eLZEx1UhQkbDIoM6xWsk7M18TApDd9/b8CHJnEiaiQE2YJ8mqu6kvsGuImDt4dy/

现在,我想检查此 var 文件中的密码哈希值是否与远程服务器上 /etc/shadow 文件中的密码哈希值匹配。我知道可以混合使用 Ansible 和 bash/python 脚本来获得我想要的东西。我想知道是否可以仅使用纯 Ansible 剧本(没有 bash/python 脚本)使用查找插件或其他一些 Ansible 功能来做到这一点。

您可以使用文件中的行来检查行是否已更改,如果 lineinfile 模块返回,则注册结果并将其存储在另一个变量中"changed"。

不幸的是,由于 this bug 你不能简单地使用 with_items 和 lineinfile 模块中的 backrefs 来检查字符串是否有效,所以我使用了一些 include hack。

所以我们有一个名为 playbook.yml 的剧本和一个名为 checkpasswords.yml 的任务,让我们分别解释一下。

playbook.yml

- hosts: localhost
  tasks:
    # execute checkpasswords.yml for each user in vault_users dict 
    # and pass each user (or item) as {{ user }} variable to included task
    - include: checkpasswords.yml user="{{ item }}"
      with_items: "{{ vault_users }}"
    - debug: msg="{{ changed_users|default([]) }}"

checkpasswords.yml

- name: check for user and hash
  lineinfile:
    dest: /etc/shadow
    regexp: '{{ user }}:([^:]+):(.*)'
    # replace sting with user:hashed_password:everything_that_remains
    line: '{{ user }}:{{ vault_users[user].password }}:'
    state: present
    backrefs: yes
  register: u

- name: changed users
  set_fact:
    # set changed_users list to [] if not present and add [user] element
    # when user password has changed
    changed_users: "{{ changed_users|default([]) + [user] }}"
  when: u.changed

hashvars.yml

vault_users:
  root:
    password: "nothing to see here"
  my_user:
    password: "nothing here"

我将变量包含到 hashvars.yml 文件中,并更改了 my_user 的散列和其中的根目录。所以执行这个剧本的结果将类似于下面的输出,不要忘记 --check!

ansible-playbook playbook.yml -e @hashvars.yml --check

   PLAY [localhost] ***************************************************************

   TASK [setup] *******************************************************************
   ok: [localhost]

   TASK [include] *****************************************************************
   included: /home/my_user/workspace/so/checkpasswords.yml for localhost
   included: /home/my_user/workspace/so/checkpasswords.yml for localhost

   TASK [check for user and hash] *************************************************
   changed: [localhost]

   TASK [changed users] ***********************************************************
   ok: [localhost]

   TASK [check for user and hash] *************************************************
   changed: [localhost]

   TASK [changed users] ***********************************************************
   ok: [localhost]

   TASK [debug] *******************************************************************
   ok: [localhost] => {
       "msg": [
           "my_user",
           "root"
       ]
   }

   PLAY RECAP *********************************************************************
   localhost                  : ok=8    changed=2    unreachable=0    failed=0