目录
目录
链接:https://pan.baidu.com/s/196q-lgmyA-7fV90m5Y_llA
提取码:h59p
一:安装ansible
- 安装gcc
yum install -y gcc*
- 在线安装ansible(与离线安装2选1)
yum install -y ansible
- 离线安装ansible(与在线安装2选1)
将 ansible_install.sh 和软件包放入/usr/local/src/ansible 目录下执行 sh ansible_install.sh
- 查看是否安装完成
ansible --version
- 配置
mkdir /etc/ansible
进入ansible-1.7.2 examples下
cp hosts ansible.cfg /etc/ansible/
二:解压脚本文件
- 解压并拷贝ansible_config文件
复制 ansible_config.tar到/etc/ansible目录下
tar -zxvf ansible_config.tar
cd ansible_consig
cp * ../
cd ..
2.拷贝skyform-agent-centos7.3.tar文件
复制skyform-agent-centos7.3.tar到/usr/local/src目录下
二:配置待安装的虚拟机ip
- hosts 文件中添加zabbix_agent主机组并添加ip列表
[root@centos1 ~]# cd /etc/ansible/
[root@centos1 ansible]# ls
ansible.cfg hosts roles
[root@centos1 ansible]# vi hosts
[zabbix_agent]
10.15.21.2
10.15.21.3
- 查看主机组内的主机
[root@centos1 ansible]# ansible zabbix_agent --list-hosts
hosts (3):
10.15.21.2
10.15.21.3
- 测试是否可以ping通
[root@zk3 ansible]# ansible all -m ping
10.15.21.4 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).\r\n",
"unreachable": true
}
10.15.21.2 | SUCCESS => {
"changed": false,
"ping": "pong"
}
10.15.21.3 | SUCCESS => {
"changed": false,
"ping": "pong"
}
- 查看hosts文件并向所有主机推送修改好的hosts文件(当zabbix-agent配置指定ip时可忽略)
ansible all -m copy -a "src=/etc/hosts dest=/etc/hosts"
- 查看组内主机是否配置zabbix_agent
ansible all -m shell -a "ps -ef | grep zabbix_agent"
三:手动配置免密登录
- 配置ssh免密通信
[root@centos1 ~]# ssh-keygen
[root@centos1 ~]# ssh-copy-id 10.15.21.2
[root@centos1 ~]# ssh-copy-id 10.15.21.3
四:批量配置免密登录
- 在cd /etc/ansible/目录下创建ssh-addkey.yml文件
cd /etc/ansible/
touch ssh-addkey.yml
# ssh-addkey.yml
- hosts: ssh_key
gather_facts: no
tasks:
- name: install ssh key
authorized_key: user=root
key="{{ lookup('file', '/root/.ssh/id_rsa.pub') }}"
state=present
- 执行免密登录
ssh-keygen
ansible-playbook ssh-addkey.yml
- 测试脚本(在脚本中获取当前ip)
#sh-addkey.yml
- hosts: ssh_key
gather_facts: no
tasks:
- name: create a file
shell: "echo {{inventory_hostname}}"
五:配置zabbix_agent.yaml
- hosts: zabbix_agent
remote_user: root
tasks:
- name: copy install_shell to clien
copy: src=/etc/ansible/zabbix_agent.sh dest=/usr/local/src/zabbix_agent.sh
- name: sent tar
copy: src=/usr/local/src/skyform-agent-centos7.3.tar dest=/usr/local/src/skyform-agent-centos7.3.tar
notify: install shell
handlers:
- name: install shell
shell: /bin/bash /usr/local/src/zabbix_agent.sh
六:配置zabbix_agent.sh
#!/bin/bash
cd /usr/local/src
rm -rf skyform-agent-centos7.3
tar -zxvf skyform-agent-centos7.3.tar
systemctl status zabbix-agent
./skyform-agent-centos7.3/uinstall.sh
rpm -ivh /usr/local/src/skyform-agent-centos7.3/packages/zabbix-agent-3.4.1-1.el7.x86_64.rpm
\cp /usr/local/src/skyform-agent-centos7.3/packages/zabbix_agentd.conf /etc/zabbix/
ipAddr=$(/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6 | awk '{print $2}' | tr -d "addr:")
sed -i "15i Hostname=$ipAddr" /etc/zabbix/zabbix_agentd.conf
systemctl enable zabbix-agent
systemctl start zabbix-agent
七:执行zabbix_agent.yaml
ansible-playbook zabbix_agent.yaml
问题
[root@ntp_server ansible]# ansible zabbix_agent -m ping
10.15.21.212 | FAILED => to use the 'ssh' connection type with passwords, you must install the sshpass program
10.15.21.211 | FAILED => to use the 'ssh' connection type with passwords, you must install the sshpass program
yum -y install sshpass