码迷,mamicode.com
首页 > 其他好文 > 详细

w9 Ansible批量管理与维护

时间:2018-10-26 19:46:59      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:vat   read   network   哪些   ...   sam   time   ext   director   

ssh秘钥认证

基于口令的安全验证

技术分享图片
[root@m01 ~]# ssh 10.0.0.41 hostname
root@10.0.0.41‘s password:
backup
[root@m01 ~]# ssh 10.0.0.41 whoami
root@10.0.0.41‘s password:
root
[root@m01 ~]# ssh oldboy@10.0.0.41 whoami
oldboy@10.0.0.41‘s password:
oldboy
view

1.生成钥匙和锁头

技术分享图片
[root@m01 ~]# hostname
m01
[root@m01 ~]# 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 root@m01
The keys randomart image is:
+--[ DSA 1024]----+
| |
| . + . |
| = + E |
| . + . o |
| o . S |
| ..o = . |
| .o.... o |
| .o..o.. |
| .+o.. |
+-----------------+
view

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

技术分享图片
发送到 backup
[root@m01 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@172.16.1.41
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 ‘yesorno‘: yes
Warning: Permanently added ‘172.16.1.41‘ (RSA) to the list of known hosts.
root@172.16.1.41‘s password:
Now try logging into the machine, with "ssh ‘root@172.16.1.41‘", and check in:
.ssh/authorized_keys
to make sure we haven‘t added extra keys that you weren‘t expecting.
[root@m01 ~]# ssh 10.0.0.41 hostname
backup
?
?
#nfs01
[root@m01 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@172.16.1.31
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.
root@172.16.1.31‘s password:
Now try logging into the machine, with "ssh ‘root@172.16.1.31‘", and check in:
.ssh/authorized_keys
to make sure we haven‘t added extra keys that you weren‘t expecting.
[root@m01 ~]# ssh 172.16.1.31 hostname
nfs01
view

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

技术分享图片
yum install sshpass -y

#sshpass 给ssh类提供密码

[root@m01 ~]# sshpass -p123456 ssh 172.16.1.41 hostname

backup

sshpass -p123456 ssh -o StrictHostKeyChecking=no 172.16.1.41 hostname

backup
view

 

非交互式创建 钥匙锁头

技术分享图片
[root@m01 ~]# 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 root@m01
The keys randomart image is:
+--[ DSA 1024]----+
| |
| o |
| + . |
| . . . + |
| . . = S |
| o = B o |
| o O . + |
| . +oo |
| E+=. |
+-----------------+
view

非交互式 分发公钥

技术分享图片
[root@m01 ~]# sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa.pub "-o StrictHostKeyChecking=no root@172.16.1.41"
Now try logging into the machine, with "ssh ‘-o StrictHostKeyChecking=no root@172.16.1.41‘",
and check in:
.ssh/authorized_keys
to make sure we haven‘t added extra keys that you weren‘t expecting.
?
?
[root@m01 ~]# sshpass -p123456 ssh-copy-id -i /root/.ssh/id_dsa.pub "-o StrictHostKeyChecking=no root@172.16.1.31"
Now try logging into the machine, with "ssh ‘-o StrictHostKeyChecking=no root@172.16.1.31‘",
and check in:
.ssh/authorized_keys
to make sure we haven‘t added extra keys that you weren‘t expecting.
?
?
[root@m01 ~]# ssh 172.16.1.41 hostname
backup
[root@m01 ~]# 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
[root@m01 ~]# tree /etc/ansible/
/etc/ansible/
├── ansible.cfg #ansible的配置文件
├── hosts #ansible管理了 哪些服务器 服务器列表
└── roles
1 directory, 2 files
[root@m01 ~]# 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

 

测试:复制文件

技术分享图片

技术分享图片
[root@m01 ~]# 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
}
?
?
[root@m01 ~]# 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

 

定时任务

技术分享图片

技术分享图片
[root@m01 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"
]
}
?
[root@m01 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
?
[root@m01 playbook]# cat ifconfig.yml
- hosts: oldboy
tasks:
- command: ifconfig
- shell: ifconfig >/tmp/ip.log
?
?
ansible-playbook -C ifconfig.yml
ansible-playbook ifconfig.yml
[root@m01 ~]# 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"
    ]
}
?
?
?
?
?
[root@m01 ~]# 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中

技术分享图片
[root@m01 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‘
?
?
[root@m01 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添加定时任务
[root@m01 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

技术分享图片
[root@m01 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

?
技术分享图片
[root@m01 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

w9 Ansible批量管理与维护

标签:vat   read   network   哪些   ...   sam   time   ext   director   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!