Ansible模块总结以及zabbix-proxy批量部署

ansblie模块总结
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

设置主机清单

[root@localhost ~]# vim /etc/ansible/hosts
[zabbix]
192.168.103.72

模块:

command:远程执行命令
shell:远程执行shell命令,可支持$之类的变量
copy:从ansible服务器主控端复制文件到远程主机
Fetch:从远端主机复制到主控端,与copy相反
file:修改文件元数据或者属主(组)
unarchive:解包解压缩,包可在主控端也可在远程主机,以copy进行区分
archive:将主控端打包压缩,存储于被管理节点
hostname:设置远程主机主机名
cron:指定被控主机计划任务
yum:指定被控主机安装包文件,仅仅支持RHEL系列
service:管理被控主机服务
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
预运行
[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   


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容