w9 Ansible批量管理與維護
阿新 • • 發佈:2018-10-26
交互 ren == shp 狀態 不支持 change arc res
ssh秘鑰認證
基於口令的安全驗證
[root@m01 ~]# ssh 10.0.0.41 hostname [email protected]‘s password: backup [root@m01 ~]# ssh 10.0.0.41 whoami [email protected]‘s password: root [root@m01 ~]# ssh [email protected] whoami [email protected]‘s password: oldboyview
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 key‘s randomart image is:view+--[ DSA 1024]----+ | | | . + . | | = + E | | . + . o | | o . S | | ..o = . | | .o.... o | | .o..o.. | | .+o.. | +-----------------+
2.把鎖頭發送到 backup 和 nfs01上
發送到 backup [root@m01 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub [email protected] The authenticity of host ‘172.16.1.41 (172.16.1.41)view‘ 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. [root@m01 ~]# ssh 10.0.0.41 hostname backup ? ? #nfs01 [root@m01 ~]# 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. [root@m01 ~]# ssh 172.16.1.31 hostname nfs01
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 backupview
非交互式創建 鑰匙鎖頭
[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 key‘s 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 [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. ? ? [root@m01 ~]# 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. ? ? [root@m01 ~]# ssh 172.16.1.41 hostname backup [root@m01 ~]# ssh 172.16.1.31 hostname nfs01view
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/hostsinstall
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>&1view
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.logview
?
[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)
vieww9 Ansible批量管理與維護