ansblie模塊總結(jié)
1,如何使用模塊当窗?
[root@localhost ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:iLerlu9akP5Hy63e6ubR7rJcB1AY/qQfJsBI19gqx10 root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| . .+.o. |
| . +..+.E |
| ..ooo.. |
| .o+o.= |
| +oo S = |
| . o ..= o |
| ..oo.o+ . |
| oo o*=.. |
| .o==*O*+ |
+----[SHA256]-----+
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# ssh-copy-id 192.168.103.71
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.103.71 (192.168.103.71)' can't be established.
ECDSA key fingerprint is SHA256:w+Imcr2p+/4fLG1IXfvKT+RqmyH1MiHq5bzkAQUWfbY.
ECDSA key fingerprint is MD5:dd:e5:80:59:b1:5f:80:2b:38:dd:53:2d:77:c9:d8:b0.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.103.71's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.103.71'"
and check to make sure that only the key(s) you wanted were added.
[root@localhost ~]# rsync -av ./.ssh 192.168.103.72:~/
The authenticity of host '192.168.103.72 (192.168.103.72)' can't be established.
ECDSA key fingerprint is SHA256:w+Imcr2p+/4fLG1IXfvKT+RqmyH1MiHq5bzkAQUWfbY.
ECDSA key fingerprint is MD5:dd:e5:80:59:b1:5f:80:2b:38:dd:53:2d:77:c9:d8:b0.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.103.72' (ECDSA) to the list of known hosts.
root@192.168.103.72's password:
sending incremental file list
.ssh/
.ssh/authorized_keys
.ssh/id_rsa
.ssh/id_rsa.pub
.ssh/known_hosts
sent 3,185 bytes received 96 bytes 504.77 bytes/sec
total size is 2,847 speedup is 0.87
設(shè)置主機(jī)清單
[root@localhost ~]# vim /etc/ansible/hosts
[zabbix]
192.168.103.72
模塊:
command:遠(yuǎn)程執(zhí)行命令
shell:遠(yuǎn)程執(zhí)行shell命令酸钦,可支持$之類的變量
copy:從ansible服務(wù)器主控端復(fù)制文件到遠(yuǎn)程主機(jī)
Fetch:從遠(yuǎn)端主機(jī)復(fù)制到主控端窄潭,與copy相反
file:修改文件元數(shù)據(jù)或者屬主(組)
unarchive:解包解壓縮濒旦,包可在主控端也可在遠(yuǎn)程主機(jī)害淤,以copy進(jìn)行區(qū)分
archive:將主控端打包壓縮平窘,存儲(chǔ)于被管理節(jié)點(diǎn)
hostname:設(shè)置遠(yuǎn)程主機(jī)主機(jī)名
cron:指定被控主機(jī)計(jì)劃任務(wù)
yum:指定被控主機(jī)安裝包文件炉旷,僅僅支持RHEL系列
service:管理被控主機(jī)服務(wù)
user與group:管理用戶與用戶組
[root@localhost ~]# cat ~/zabbix-agent.sh
#!/bin/bash
HN=$(hostname)
if [ ! -f /etc/yum.repos.d/zabbix.repo ]
then
rpm -Uvh http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm
fi
rpm -q zabbix-agent &>/dev/null
[ $? -ne 0 ] && yum -y install zabbix-agent
cp /etc/zabbix/zabbix_agentd.conf{,-$(date +%F%T)}
sed -i 's/Server=127.0.0.1/Server=192.168.30.21/g' /etc/zabbix/zabbix_agentd.conf
sed -i 's/ServerActive=127.0.0.1/ServerActive=192.168.30.21/g' /etc/zabbix/zabbix_agentd.conf
sed -i 's/Hostname=Zabbix server/Server=$HN/g' /etc/zabbix/zabbix_agentd.conf
systemctl restart zabbix-agent
[root@localhost ~]# cat /etc/ansible/zabbix-agent.yml
- hosts: all
remote_user: root
tasks:
- name: install yum
copy: src=/root/zabbix-agent.sh dest=/opt/zabbix-agent.sh mode=777
notify:
- script agent
handlers:
- name: script agent
command: /opt/zabbix-agent.sh
預(yù)運(yùn)行
[root@localhost ~]# ansible-playbook -C /etc/ansible/zabbix-agent.yml
PLAY [all] ***********************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************
ok: [192.168.103.72]
TASK [install yum] ***************************************************************************************************
ok: [192.168.103.72]
PLAY RECAP ***********************************************************************************************************
192.168.103.72 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@localhost ~]#
[root@localhost ~]# ansible-playbook /etc/ansible/zabbix-agent.yml
PLAY [all] ***********************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************
ok: [192.168.103.72]
TASK [install yum] ***************************************************************************************************
changed: [192.168.103.72]
RUNNING HANDLER [script agent] ***************************************************************************************
changed: [192.168.103.72]
PLAY RECAP ***********************************************************************************************************
192.168.103.72 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0