ansible使用用户密码远程执行命令

ansible使用用户密码远程执行命令

ansible使用用户密码远程执行命令是使用sshpass,免去ssh输入密码的交互。

  • 安装ansible
yum install ansible
  • 修改ansible配置
vim /etc/ansible/ansible.cfg

# defaults section增加如下配置:
[defaults]
host_key_checking = False # 第一次远程ssh主机需要将机器指纹添加到known_hosts,此设置忽略该步骤
command_warnings=False  #忽略ansible执行命令的告警信息
deprecation_warnings=False
  • 增加远程机器
vim /etc/ansible/hosts

# 密码包含特殊符号,需要使用双引号
10.83.192.20  ansible_host=10.83.192.20   ansible_user=admin  ansible_password="xxx"
10.83.192.21  ansible_host=10.83.192.21   ansible_user=admin  ansible_password="xxx"
  • 执行命令
# 不加setsid或nohup,会报"RECEIVED SIGNAL 1: SIGHUP",导致服务启动失败
ansible 10.83.192.20 -m shell -a 'setsid hdfs --daemon start datanode'
ansible 10.83.192.20 -m shell -a 'setsid hdfs --daemon stop datanode'

ansible 10.83.192.21 -m shell -a 'nohup hdfs --daemon start datanode &'

SIGHUP

If a process is being run from terminal and that terminal suddenly goes away then the process receives this signal. “HUP” is short for “hang up” and refers to hanging up the telephone in the days of telephone modems.

nohup、setsid、Daemon介绍
https://www.cnblogs.com/csuliujia/p/9939502.html

https://ls0f.github.io/post/ansbile-%E5%8F%91%E5%B8%83%E5%BC%95%E8%B5%B7%E7%9A%84sighup%E9%97%AE%E9%A2%98/

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
远程添加hosts到Ansible,你可以按照以下步骤进行操作: 1. 打开Ansible的配置文件`/etc/ansible/ansible.cfg`。 2. 在文件中找到`\[defaults\]`部分。 3. 在`\[defaults\]`部分中添加`inventory`配置指令,指定你要使用的inventory文件的路径。例如:`inventory = /path/to/your/inventory/file`。默认的inventory文件路径是`/etc/ansible/hosts`。\[1\] 4. 保存并关闭配置文件。 这样,你就可以在指定的inventory文件中添加远程主机的信息了。打开inventory文件,按照以下格式添加主机信息: ``` hostname ansible_host=IP地址 ansible_user=用户ansible_password="密码" ``` 其中,`hostname`是你为主机指定的别名,`IP地址`是远程主机的IP地址,`用户名`是远程主机的用户名,`密码`是远程主机的密码。确保密码使用双引号括起来,特别是当密码包含特殊字符时。\[2\] 保存并关闭inventory文件。 现在,你可以使用Ansible远程控制这些主机了。例如,使用以下命令执行远程命令: ``` ansible hostname -m shell -a 'command' ``` 其中,`hostname`是你在inventory文件中为主机指定的别名,`command`是你要在远程主机上执行的命令。\[2\] 希望这个回答对你有帮助! #### 引用[.reference_title] - *1* *3* [Ansible 制定演员表inventory即hosts解析](https://blog.csdn.net/qq_34556414/article/details/107844927)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [ansible使用用户密码远程执行命令](https://blog.csdn.net/Shyllin/article/details/123690458)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值