- name: Configure Kerberos for Hadoop Users
hosts: hadoop_servers
become: no
gather_facts: no
vars:
kerberos_server: hadoop1.xuexi.com
keytab_file_path: /home/hadoop/keys/hadoop.keytab
principals:
nn/
dn/
yarn/
starrock/
tasks:
- name: Ensure key directory exists
ansible.builtin.file:
path: /home/hadoop/hxy
state: directory
mode: '0755'
- name: Create Kerberos principals
ansible.builtin.command: >
kadmin.local -q 'addprinc -randkey { { item }}{ { inventory_hostname }}@XUEXI.COM'
register: addprinc_output
ignore_errors: yes
delegate_to: "{ { kerberos_server }}"
loop: "{ { principals }}"
loop_control:
extended: yes # Ensure extended loop variables are available
- name: Check principal creation status
ansible.builtin.fail:
msg: "Failed to create principal for { { item.item }}: { { item.stderr }}"
when: "'Principal already exists' not in item.stderr and item.rc != 0"
loop: "{ { addprinc_output.results }}"
loop_control:
label: "{ { item.item }}{ { inventory_hostname }}@XUEXI.COM"
- name: Generate keytab file for each principal
ansible.builtin.command: >
kadmin.local -q 'xst -k { { keytab_file_path }}.tmp -norandkey { { item }}{ { inventory_hostname }}@XUEXI.COM'
register: xst_output
delegate_to: "{ { kerberos_server }}"
loop: "{ { principals }}"
when: "'Principal already exists' in (addprinc_output.results | selectattr('item', 'equalto', item) | first).stderr or (addprinc_output.results | selectattr('item', 'equalto', item) | first).rc == 0"
Note: The above when condition is simplified and may need adjustment.
It assumes that if 'Principal already exists', it's okay to proceed.
However, a more robust solution would involve tracking success/failure per principal.
- name: Move keytab file to final location (on Kerberos server)
ansible.builtin.command: mv { { keytab_file_path }}.tmp { { keytab_file_path }}
delegate_to: "{ { kerberos_server }}"
when: xst_output is changed # This might not be perfect, as 'changed' depends on file existence, not Kerberos operation.
- name: Fetch the keytab file to the control machine
ansible.builtin.fetch:
src: "{ { keytab_file_path }}"
dest: "./hadoop.keytab"
flat: yes
delegate_to: "{ { kerberos_server }}"
run_once: yes # Ensure this task runs only once.
- name: Distribute keytab files to each target host
ansible.builtin.copy:
src: ./hadoop.keytab
dest: /data1/tmp/hadoop.keytab
loop: "{ { groups'hadoop_servers' }}"
delegate_to: "{ { item }}"
- name: Clean up local keytab file
ansible.builtin.file:
path: ./hadoop.keytab
state: absent
run_once: yes
ansible批量生产kerberos票据,并批量分发到所有其他主机脚本
蘑菇丁2025-01-24 8:05
相关推荐
wifi___1 小时前
全局异常处理的原理2601_963870206 小时前
【计算机毕业设计】基于Spring Boot的专科医院医疗管理系统Bingo_BIG6 小时前
Java Spring 批量修改,实体、接口、方法的定义画中有画6 小时前
软件架构中质量属性(性能、安全、可扩展性)的权衡设计Shaoxi Zhang6 小时前
JAVA学习笔记035——对象和JSON格式赵丙双7 小时前
CountDownLatch 源码分析余额瞒着我当琳7 小时前
C++--深拷贝三件套 + swap + 写时拷贝 + vector 扩容 + reservethefool1122667 小时前
翻转二叉树喜欢打篮球的普通人8 小时前
LLVM Backend Lowering 从入门到实战:把 IR 变成机器码的完整链路FL16238631298 小时前
室内易燃物识别易燃评估室内易燃程度识别分割数据集labelme格式1015张85类别