w9 Ansible批量管理与维护

ssh秘钥认证

基于口令的安全验证

[[email protected] ~]# ssh 10.0.0.41 hostname
[email protected]‘s password:
backup
[[email protected] ~]# ssh 10.0.0.41 whoami
[email protected]‘s password:
root
[[email protected] ~]# ssh [email protected] whoami
[email protected]‘s password:
oldboy

view

1.生成钥匙和锁头

[[email protected] ~]# hostname
m01
[[email protected] ~]# ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
29:37:1b:e1:9f:0a:ab:77:a0:fc:60:41:2d:bc:85:dd [email protected]
The key‘s randomart image is:
+--[ DSA 1024]----+
| |
| . + . |
| = + E |
| . + . o |
| o . S |
| ..o = . |
| .o.... o |
| .o..o.. |
| .+o.. |
+-----------------+

view

2.把锁头发送到 backup 和 nfs01上

发送到 backup
[[email protected] ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub [email protected]
The authenticity of host ‘172.16.1.41 (172.16.1.41)‘ can‘t be established.
RSA key fingerprint is ac:0f:aa:d2:5b:ff:cf:ac:f0:76:37:a6:be:31:b9:f9.
Are you sure you want to continue connecting (yes/no)? y
Please type ‘yes‘ or ‘no‘: yes
Warning: Permanently added ‘172.16.1.41‘ (RSA) to the list of known hosts.
[email protected]‘s password:
Now try logging into the machine, with "ssh ‘[email protected]‘", and check in:
.ssh/authorized_keys
to make sure we haven‘t added extra keys that you weren‘t expecting.
[[email protected] ~]# ssh 10.0.0.41 hostname
backup
?
?
#nfs01
[[email protected] ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub [email protected]
The authenticity of host ‘172.16.1.31 (172.16.1.31)‘ can‘t be established.
RSA key fingerprint is ac:0f:aa:d2:5b:ff:cf:ac:f0:76:37:a6:be:31:b9:f9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘172.16.1.31‘ (RSA) to the list of known hosts.
[email protected]‘s password:
Now try logging into the machine, with "ssh ‘[email protected]‘", and check in:
.ssh/authorized_keys
to make sure we haven‘t added extra keys that you weren‘t expecting.
[[email protected] ~]# ssh 172.16.1.31 hostname
nfs01

view

3.把锁头发送到 backup 和 nfs01上 - 免密码

yum install sshpass -y

#sshpass 给ssh类提供密码

[[email protected] ~]# sshpass -p123456 ssh 172.16.1.41 hostname

backup

sshpass -p123456 ssh -o StrictHostKeyChecking=no 172.16.1.41 hostname

backup

view

非交互式创建 钥匙锁头

[[email protected] ~]# ssh-keygen -t dsa -P ‘‘ -f ~/.ssh/id_dsa
Generating public/private dsa key pair.
/root/.ssh/id_dsa already exists.
Overwrite (y/n)? y
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
e0:8d:9b:00:99:fe:fc:67:be:65:8c:b0:b1:cc:fc:8c [email protected]
The key‘s randomart image is:
+--[ DSA 1024]----+
| |
| o |
| + . |
| . . . + |
| . . = S |
| o = B o |
| o O . + |
| . +oo |
| E+=. |
+-----------------+

view

非交互式 分发公钥

[[email protected] ~]# sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa.pub "-o StrictHostKeyChecking=no [email protected]"
Now try logging into the machine, with "ssh ‘-o StrictHostKeyChecking=no [email protected]‘",
and check in:
.ssh/authorized_keys
to make sure we haven‘t added extra keys that you weren‘t expecting.
?
?
[[email protected] ~]# sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa.pub "-o StrictHostKeyChecking=no [email protected]"
Now try logging into the machine, with "ssh ‘-o StrictHostKeyChecking=no [email protected]‘",
and check in:
.ssh/authorized_keys
to make sure we haven‘t added extra keys that you weren‘t expecting.
?
?
[[email protected] ~]# ssh 172.16.1.41 hostname
backup
[[email protected] ~]# ssh 172.16.1.31 hostname
nfs01

view

ansible部署

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
?
#m01
yum install ansible -y
yum install libselinux-python -y
?
#backup nfs01
yum install libselinux-python -y
[[email protected] ~]# tree /etc/ansible/
/etc/ansible/
├── ansible.cfg #ansible的配置文件
├── hosts #ansible管理了 哪些服务器 服务器列表
└── roles
1 directory, 2 files
[[email protected] ~]# cat /etc/ansible/hosts
[oldboy]
172.16.1.31
172.16.1.41
?
ansible oldboy -m command -a "hostname"
ansible oldboy -m command -a "yum install cowsay -y"

install

测试:复制文件

[[email protected] ~]# ansible oldboy -m copy -a "src=/etc/hosts dest=/tmp owner=oldboy mode=0755"
172.16.1.41 | SUCCESS => {
"changed": true,
"checksum": "bc07bb4d3a780f4fd8cae94ec7bff04edb1a5a4e",
"dest": "/tmp/hosts",
"gid": 0,
"group": "root",
"md5sum": "55ee21bf1168f9be70abd35bf29d8e4a",
"mode": "0755",
"owner": "oldboy",
"size": 364,
"src": "/root/.ansible/tmp/ansible-tmp-1517744820.18-259504826638509/source",
"state": "file",
"uid": 500
}
172.16.1.31 | SUCCESS => {
"changed": true,
"checksum": "bc07bb4d3a780f4fd8cae94ec7bff04edb1a5a4e",
"dest": "/tmp/hosts",
"gid": 0,
"group": "root",
"md5sum": "55ee21bf1168f9be70abd35bf29d8e4a",
"mode": "0755",
"owner": "oldboy",
"size": 364,
"src": "/root/.ansible/tmp/ansible-tmp-1517744820.17-14642605512978/source",
"state": "file",
"uid": 500
}
?
?
[[email protected] ~]# ansible oldboy -m command -a "ls -l /tmp/hosts"
172.16.1.31 | SUCCESS | rc=0 >>
-rwxr-xr-x 1 oldboy root 364 Feb 4 19:47 /tmp/hosts
172.16.1.41 | SUCCESS | rc=0 >>
-rwxr-xr-x 1 oldboy root 364 Feb 4 19:47 /tmp/hosts

install

ansible oldboy -m copy -a "src=/etc/hosts dest=/tmp backup=yes"

ansible-doc -l|wc -l

ansible-doc -s copy # 查看文档

ansible oldboy -m copy -a "src=/server/scripts/yum-htop.sh dest=/server/scripts/ "

ansible oldboy -m shell -a "/bin/sh /server/scripts/yum-htop.sh"

ansible oldboy -m script -a "/server/scripts/yum.sh"

view

定时任务

[[email protected] scripts]# ansible oldboy -m cron -a "name=‘restart network‘ minute=00 hour=00 job=‘ /etc/init.d/network restart >/dev/null 2>&1‘"
172.16.1.31 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"restart network"
]
}
172.16.1.41 | SUCCESS => {
"changed": true,
"envs": [],
"jobs": [
"restart network"
]
}
?
[[email protected] scripts]# ansible oldboy -a "crontab -l"
172.16.1.41 | SUCCESS | rc=0 >>
#time sync by lidao at 2017-03-08
*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
#check & send result lee at 2017-01-01
00 03 * * * /bin/sh /server/scripts/check.sh >/dev/null 2>&1
#Ansible: restart network
00 00 * * * /etc/init.d/network restart >/dev/null 2>&1
172.16.1.31 | SUCCESS | rc=0 >>
#time sync by lidao at 2017-03-08
*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
#Ansible: restart network
00 00 * * * /etc/init.d/network restart >/dev/null 2>&1
?
mkdir -p /server/playbook
?
[[email protected] playbook]# cat ifconfig.yml
- hosts: oldboy
tasks:
- command: ifconfig
- shell: ifconfig >/tmp/ip.log
?
?
ansible-playbook -C ifconfig.yml
ansible-playbook ifconfig.yml
[[email protected] ~]# ansible oldboy -m cron -a "name=‘restart network‘ minute=00 hour=00 job=‘/etc/init.d/network restart >/dev/null 2>&1‘"
172.16.1.41 | SUCCESS => {
    "changed": true,
    "envs": [],
    "jobs": [
        "restart network"
    ]
}
172.16.1.31 | SUCCESS => {
    "changed": true,
    "envs": [],
    "jobs": [
        "restart network"
    ]
}
?
?
?
?
?
[[email protected] ~]# ansible oldboy -m cron -a "name=‘restart network‘ state=absent "
172.16.1.31 | SUCCESS => {
    "changed": true,
    "envs": [],
    "jobs": []
}
172.16.1.41 | SUCCESS => {
    "changed": true,
    "envs": [],
    "jobs": []
}
?

view

简单例子1:批量执行命令

把所有服务器的ip地址追加到/tmp/ip.log中

[[email protected] playbook]# cat print-ip.yml
- hosts: all
  tasks:
  - name: get ip address
    shell: ifconfig eth0 |awk -F "[ :]+" ‘NR==2{print $4}‘ >>/tmp/ip.log

ansible-playbook -C print-ip.yml
ansible-playbook print-ip.yml
ansible all -a "tail -1 /tmp/ip.log"
?

ansible oldboy -m cron -a ‘name="restart network" minute=00 hour=00 job="/etc/init.d/network restart >/dev/null 2>&1" state=present‘
?
?
[[email protected] playbook]# cat add-cron.yml
- hosts: oldboy
  tasks:
  - name: add restart network cron
    cron: name="restart network" minute=00 hour=00 job="/etc/init.d/network restart >/dev/null 2>&1" state=present
?
?

playbook添加定时任务
[[email protected] playbook]# ansible oldboy -a "crontab -l"
172.16.1.41 | SUCCESS | rc=0 >>
#time sync by lidao at 2017-03-08
*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
#check & send result lee at 2017-01-01
00 03 * * * /bin/sh /server/scripts/check.sh >/dev/null 2>&1
172.16.1.31 | SUCCESS | rc=0 >>
#time sync by lidao at 2017-03-08
*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1

view

2.playbook添加定时任务

  不支持tab

- hosts: oldboy
  tasks:
  - name: add restart network cron
    cron: name="restart network" minute=00 hour=00 job="/etc/init.d/network restart >/dev/null 2>&1" state=present
- hosts: oldboy
  tasks:
  - name: add restart network cron
    cron:
    name: restart network
    minute: 00
    hour: 00
    job: /etc/init.d/network restart >/dev/null 2>&1
    state: present

两种书写格式


例3:对同一台机器配置多个任务

重启网络 service

安装软件 yum

显示时间信息到文件 date

[[email protected] playbook]# cat manage.yml
- hosts: all
  tasks:
    - name: restart network
      service:                    #服务
      name: network               #服务器名
      state: restarted            #状态
    - name: install tree nmap lrzsz iftop htop iotop nc
      shell: yum install -y tree nmap lrzsz iftop htop iotop nc
    - name: print date to file
      shell: date +%F >>/tmp/date.log

view


?

[[email protected] playbook]# cat hosts.yml
- hosts: 172.16.1.41
  tasks:
    - name: mkdir
      shell: mkdir -p /oldboy/backup
- hosts: 172.16.1.31
  tasks:
    - name: find
      shell: find /etc -type f -name "*.conf" >>/tmp/name.log

ansible安装rsync服务器

nfs服务器

配置sersync数据同步

如何使用pssh (pssh pscp prsync)

view

原文地址:https://www.cnblogs.com/wenyule/p/9857982.html

时间: 2024-11-04 06:42:52

w9 Ansible批量管理与维护的相关文章

ansible批量管理远程服务器

使用ansible批量管理远程服务器 背景 本地需要管理远程的一批服务器,主要执行以下任务: 1) 将本地的文件复制到远端所有服务器:  2) 需要在远程服务器中执行一个个命令: 远端服务器路径并非完全一致,一般访问通过环境变量中定义的变量路径访问:  比如在.bashrc中定义$app_path=/opt/app/bin 最终选择ansible,使用这个自动化运维工具可以满足我的需求:  下面介绍下对于我这种场景需要使用的ansible的主要模块:  关于ansible是什么以及安装配置请自行

使用ansible批量管理远程服务器

使用ansible批量管理远程服务器 背景 本地需要管理远程的一批服务器,主要执行以下任务: 1) 将本地的文件复制到远端所有服务器: 2) 需要在远程服务器中执行一个个命令: 远端服务器路径并非完全一致,一般访问通过环境变量中定义的变量路径访问: 比如在.bashrc中定义$app_path=/opt/app/bin 最终选择ansible,使用这个自动化运维工具可以满足我的需求: 下面介绍下对于我这种场景需要使用的ansible的主要模块: 关于ansible是什么以及安装配置请自行百度:

免密码交互方式+ansible批量管理服务介绍

介绍了ssh服务 1) 远程连接加密传输数据协议,实现远程连接登录,默认端口222)ssh远程连接原理依赖于锁头(公钥)和钥匙(私钥),实现远程加密连接3)ssh基于秘钥远程登录原理a 管理服务器创建秘钥対,将公钥传输发送给给管理端b 管理端请求与被管理端建立连接c 被管理向管理端发送公钥质询d 管理端处理质询信息,实现管理与被管理端免密码交互4)基于ssh协议相关命令ssh scp sftp netstat -lntup |egrep sshd 查看ssh端口 1.1 部署ssh+key (免

ansible批量管理常见的配置方法

第7章 ansible的管理????103 7.1 ansible概念的介绍????103 7.1.1 ansible的概念????104 7.1.2 ansible使用的一些意义????104 7.1.3 ansible拥有哪些服务????104 7.1.4 ansible服务的一些特点????104 7.2 ansible部署过程????104 7.2.1 服务的安装????104 7.2.2 服务版本的检查????105 7.3 ansible的主机清单讲解????105 7.3.1 根据

Linux中ansible批量管理软件部署及剧本编写

服务器版本信息: Centos6.9 [[email protected] ~]# uname -a Linux db02 2.6.32-696.el6.x86_64 #1 SMP Tue Mar 21 19:29:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux ansible软件介绍 ansible是一个基于Python开发的自动化运维工具! 其功能实现基于SSH远程连接服务! ansible可以实现批量系统配置.批量软件部署.批量文件拷贝.批量运行命令等

通过ansible批量管理Linux服务器:配置Inventory和批量执行命令

ansible是一款比较新的自动化运维工具,基于Python开发,集合了众多运维工具(puppet.cfengine.chef.func.fabric)的优点,实现了批量系统配置.批量程序部署.批量运行命令等功能.ansible是基于模块工作的,本身没有批量部署的能力.真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架.主要包括:(1).连接插件connection plugins:负责和被监控端实现通信:(2).host inventory:指定操作的主机,是一个配

ansible批量管理服务详解

在开始之前我们先试想一个场景,你的公司有成百上千台的服务器,这时候领导要求在所有服务器上都添加一个定时任务,或者是执行某个命令,你可能会说用xshell一个一个去连或者是编写一个shell脚本,但是如果过你经历过类似的事情,你一定可以体会到其中的痛苦,因为这么多服务器你用xshell去连的话工作量将变得特别大,但如果你是选择写一个脚本的话也会特别的麻烦,因为每执行一个不同的东西你就要去修改脚本的类容,而且批量脚本一般采用循环的方式去执行一些任务,如果节点太多的话,执行的效率会变得比较低,而批量管

利用ansible批量管理windows2008 R2 以上dns服务器

准备:安装dns服务器net3.5  PowerShell DnsShell插件用到.DnsShell插件附件里面 ansible 支持环境也要安装的 ansible 目录结构说明: . ├── DnsShell #PowerShell dns支持模块 cp to C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules 正在文件夹 │   ├── DNSShell.dll │   ├── DnsShell.Format.ps1xml │   ├── 

Linux(11):期中架构(3)--- SSH远程管理服务 & ansible 批量管理服务

SSH远程管理服务 1. 远程管理服务知识介绍 # 1.1 SSH远程登录服务介绍说明 SSH是Secure Shell Protocol的简写,由 IETF 网络工作小组(Network Working Group)制定: 在进行数据传输之前,SSH先对联机数据包通过加密技术进行加密处理,加密后在进行数据传输.确保了传递的数据安全. SSH是专为远程登录会话和其他网络服务提供的安全性协议. 利用SSH协议可以有效的防止远程管理过程中的信息泄露问题,在当前的生产环境运维工作中, 绝大多数企业普遍