1.配置文件
#编辑默认配置文件vim/home/daboluo/ansible/ansible.cfg[defaults]collections_path=/home/daboluo/ansible/mycollection:/usr/share/ansible/collections#连接时不提示输入 SSH 密码(前提是你已经配置了 SSH 免密登录)。ask_pass=False#指定主机清单文件路径。确保该文件存在,否则无法管理主机。inventory=/home/daboluo/ansible/inventory#指定 Ansible 尝试连接远程主机时使用的用户。remote_user=daboluoroles_path=/home/daboluo/ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles#建议设置。首次连接新主机时,不再询问 "yes/no" 确认指纹,避免自动化脚本卡住。host_key_checking=False[privilege_escalation]#开启提权。即使远程用户是普通用户,也会尝试切换到高权限用户执行任务。become=True#提权时不询问密码(前提是远程用户的 sudo 配置了 NOPASSWD)。become_ask_pass=False#使用 sudo 命令进行提权(最常用)。become_method=sudo#提权后的目标用户是 root。become_user=root2.ansible实现免密登陆
ansible-i/etc/ansible/inventory-bmaster,node-mauthorized_key\-a"user=root key=\"{{ lookup('file', '/root/.ssh/id_rsa.pub') }}\"\ state=present"