centos下ansible安装 和ansible基础 shell 路径

推荐阅读:

【Ansible】ansible 基础知识_ansible 帮助文档-CSDN博客

# 指定ansible版本:

yum -y install https://releases.ansible.com/ansible/rpm/release/epel-7-x86_64/ansible-2.6.5-1.el7.ans.noarch.rpm

rm -rf /root/.ssh/id_rsa*
cd /root/.ssh
ssh-keygen -t rsa -N '' -f id_rsa -q
ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.66.1.205
ansible uat_zj  -m shell -a "chdir=/home/worker/sysinit/ ./SystemInitial.sh" # 配置执行路径

批量免密:

ansible nodes -m authorized_key -a "user=root key='{{ lookup('file','/root/.ssh/id_rsa.pub')}}' path='/root/.ssh/authorized_keys' manage_dir=no" --ask-pass -c paramiko

原文:(611条消息) Ansible 安装与使用/ssh免密互信-以及解决免密不生效_常名先生的博客-CSDN博客_ansible ssh-keygen

---------- 报错未解决

ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.17.20.224
# -bash: /usr/bin/ssh-copy-id: /bin/sh: bad interpreter: Operation not permitted

------------------------------ansible入门

1 安装ansible

yum install epel-release -y
yum list | grep ansible
yum -y install ansible


2 安装ssh常见公钥私钥

安装sshd client 并配置

yum install openssh-clients*   # 安装openssh

vim /etc/ssh/sshd_config # 配置sshd_config

 RSAAuthentication yes # 启用 RSA 认证
 PubkeyAuthentication yes # 启用公钥私钥配对认证方式
 AuthorizedKeysFile %h/.ssh/authorized_keys # 公钥文件路径

创建本地存放公钥的文件 

 # -t 指定类型,-f指定文件生成路径,-P指定文件密码(''表示无密码)

ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''

#推送公钥 两个命令都可以用  第一个不需要输入密码 :

sshpass -pbyzoro.com123 ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.66.1.200

#这个命令会提示输入 该ip的ssh登录密码 推荐使用:

ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.66.1.205

3 配置ansible
vim /etc/ansible/hosts

# 直接跟主机IP及其他端口
192.168.192.129
192.168.192.129:2333
 
# 使用主机hostname及其他端口
ansible-node1
ansible-node1:2333
 
# 主机分组
[webserver]
192.168.192.129
ansible-node1:2333
 
# 连续主机
[dbserver]
192.168.192.[100:200]    # 表示192.168.192.100--192.168.192.200之间的所有主机
ansible-node[10:20]:2333 # 表示ansible-node10:2333--ansible-node20:2333之间的所有主机


[A8]
192.66.1.205
192.66.1.207
..
[OA]
192.66.1.200

[all_agent:children]
A8
OA

4测试单个组组和children命令

ansible all_agent -m ping 
ansible A8 -m yum -a "name=tree state=installed"
ansible all_agent -m yum -a "name=tree state=installed"
ansible all_agent  -m shell -a  “sed -i 'N;96aServer=192.168.1.96' ”
ansible all_agent  -m shell -a  “systemctl restart zabbix-agent ”
ansible all_agent  -m shell -a  “systemctl enable zabbix-agent ”

# 常用shell命令

ansible all_agent -m shell -a "cat /etc/zabbix/zabbix_agentd.conf | grep -v ^# | grep -v ^$"

# 配合sed使用

sed -i 's/old ip/new ip/g' file.txt
ansible all_agent -m shell -a “sed -i 's/10.102.21.96/11.11.11.11/g' /etc/zabbix/zabbix_agentd.conf ”

# 指定shell执行路径

ansible prod_bike -m shell -a "chdir=/root/code ./install-node.sh"

公钥私钥有问题的童鞋请阅读 : https://blog.csdn.net/JLongSL/article/details/78157112

### ansible进阶

# ansible使用plybook 推送密钥
https://www.cnblogs.com/caoguo/p/5351127.html
# ansible 手册
https://www.w3cschool.cn/automate_with_ansible/automate_with_ansible-qzva27p4.html

# ansible-galaxy

Ansible Galaxy — 国内最专业的Ansible中文官方学习手册
# ansible推荐教程
https://blog.csdn.net/qq_43355223/article/details/88111875

# ansible-playbook  yum模块

Linux 7 Ansible 初学 一个简单的 playbook 学习 yum 模块 - Josh_Xie - 博客园icon-default.png?t=N7T8https://www.cnblogs.com/xiejh/p/13055356.html

### 已验证 ansible增加删除用户 修改用户密码

ansible-playbook批量增加用户,批量修改用户密码,批量删除用户_xingzhehxiang的技术博客_51CTO博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值