在Ansible中处理首次登录强制密码更改并推送SSH密钥可以通过以下步骤实现:
---
- name: Configure SSH key
hosts: your_host
remote_user: your_remote_user
become: yes
vars:
ssh_key_path: "/path/to/your/ssh/key.pub"
tasks:
- name: Copy SSH key to remote host
copy:
src: "{{ ssh_key_path }}"
dest: "/home/{{ remote_user }}/.ssh/authorized_keys"
owner: "{{ remote_user }}"
group: "{{ remote_user }}"
mode: "0644"
- name: Force password change on first login
shell: chage -d 0 {{ remote_user }}
changed_when: false
替换"your_host"为目标主机的IP地址或主机名,"your_remote_user"为远程用户的用户名。确保Ansible能够通过SSH连接到目标主机。
将你的公钥保存为一个文件,比如"your_ssh_key.pub",并将路径更新到Playbook文件中的"ssh_key_path"变量。
运行Playbook文件:
ansible-playbook ssh_key.yml
这个Playbook文件将首先将公钥复制到远程主机的.ssh/authorized_keys文件中。然后,它会使用chage命令将远程用户的密码更改日期设置为0,这将强制用户在首次登录时更改密码。
请确保在运行Playbook之前,已经通过SSH手动连接到远程主机并更改了初始密码。这样,当使用Ansible连接时,不会被强制更改密码。
注意:在Ansible中执行类似于更改密码的操作需要谨慎对待,确保你的系统和授权配置正确,并且你有足够的权限进行此操作。
上一篇:Ansible:如何遍历字典