我需要访问一些使用服务器主机名构建密码的服务器,因此,假设主机名是:fancyHost并且密码前缀是“ principio”,那么sudoer用户的密码(在此示例中为contoso -r)将是:“ principiofancyHost”。以便为每个主机分配一个新密码。
问题是我设法连接并设置了一个自定义密码作为变量,然后按比例缩放为sudoer,但是似乎ansible的turn_pass并不意味着在任务内部进行操作,这使得将前缀与主机名连接起来很困难。 / p>
- hosts: test
vars:
prefix: "principio"
ansible_become_pass: “hardToGuessPassword123”
tasks:
- name: getting current server hostname
command: hostname
register: hostname
- name: getting current user name
command: whoami
register: current_username
- name: print current server hostname
debug:
msg: "Current user: {{current_username.stdout}} ||| Hostname is: {{ hostname.stdout}} ||| Password should be: {{ prefix + hostname.stdout}}"
- name: changing contoso-r' s password
become: True
become_user: contoso-r
become_method: su
user:
name: contoso-r
password: "{{ (prefix + hostname.stdout) | password_hash('sha512') }}"
state: present
shell: /bin/bash
system: no
createhome: no
如何设置相对于每个主机的自定义密码,而不是全局的begin_pass或ansible_become_pass?
感谢您的时间
答案 0 :(得分:0)
您说:
我设法连接...并升级为sudoer,...
在“ 更改contoso-r的密码”时,不必“成为 become_user:contoso-r ”。
- name: changing contoso-r' s password
user:
name: contoso-r
password: "{{ (prefix + hostname.stdout) | password_hash('sha512') }}"