我使用多个主机,如果清单或剧本中未设置 ansible_become_user 和 ansible_become_password,我想提示用户提供 sudo 用户名和密码
我使用以下内容:
库存文件:
os:
children:
centos:
hosts:
clean_centos_1:
vars:
ansible_become_user: root
ansible_become_password: root
rocky:
hosts:
clean_rocky_1:
ubuntu:
hosts:
clean_ubuntu_1:
debian:
hosts:
clean_debian_1:
alpine:
hosts:
clean_alpine_1:
vars:
ansible_become_user: root
ansible_become_password: root
vars:
ansible_user: test
ansible_password: test
剧本
- name: "PLAY1"
hosts: all
gather_facts: no
tasks:
## Identify ansible_become_user if present
#------------------------------------------------------
- name: "Test if ansible_become_user is empty"
debug:
var: ansible_become_user
register: result
when: ansible_become_user | length > 0
ignore_errors: yes
- name: "Set status_ansible_become_user"
set_fact:
status_ansible_become_user: "{{ status_ansible_become_user|default({}) | combine( { ansible_host: 'absent' if result.failed is true else 'present' }) }}"
## Get user input
#------------------------------------------------------
- name: "User-input: <ansible_become_user>"
pause:
prompt: "\nEnter root username or sudo username for host: {{ansible_host}}"
when: item == 'absent'
loop: "{{status_ansible_become_user.values()}}"
- set_fact:
ansible_become_user: "{{ result.user_input }}"
when: result.failed is false
- debug:
var: item == 'absent'
loop: "{{status_ansible_become_user.values()}}"
上例中因为 clean_centos_1 排在第一位,并且设置了 ansible_become_user 和 ansible_become_passwor,其他主机如下跳过,没有提示
TASK [User-input: <ansible_become_user>] **********************************************************************************************************************************************************************************************************************************
skipping: [clean_centos_1] => (item=present)
TASK [set_fact] ***********************************************************************************************************************************************************************************************************************************************************
fatal: [clean_centos_1]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'user_input'\n\nThe error appears to be in '/git/ansible/role/rar.pkg.python/playbook/test.yml': line 28, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - set_fact:\n ^ here\n"}
skipping: [clean_rocky_1]
skipping: [clean_debian_1]
skipping: [clean_ubuntu_1]
fatal: [clean_alpine_1]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'user_input'\n\nThe error appears to be in '/git/ansible/role/rar.pkg.python/playbook/test.yml': line 28, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - set_fact:\n ^ here\n"}
问题:
如何正确验证是否设置了 ansible_become_user 和 ansible_become_password,如果未设置,如何提示使用以提供每个主机可能不同的 ansible_become_user 和 ansible_become_password?
例如,在下面的剧本中
创建主机和变量的列表和字典
运行一次,迭代块中的列表,并包含文件enter_ansible_become.yml中的任务
(可选)设置参数
echo: false
以隐藏密码。默认值为true。见回声。例如,输入缺失变量的值
这将创建字典ab_update
仍然在块中,组合字典
在下一个任务中使用模块add_host并创建动态组test。在下一场比赛中使用这个小组。变量应正确声明