安装
vim /etc/yum.repos.d/ansible.repo
[ansible]
name=ansible
baseurl=https://mirror.tuna.tsinghua.edu.cn/epel/7/x86_64/
gpgcheck=0
#创建完成后执行
yum clean all
yum makecache
yum install ansible -y
如果出现报错:{“msg”: “Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host’s fingerprint to your known_hosts file to manage this host.”}
解决办法:
修改/etc/ansible/ansible.cfg下的:host_key_checking = False
基本命令
复制文件到主机
#hostgroup参数在hosts里定义
ansible hostsgroup -m copy -a "src=/home/elk/metricbeat.yml dest=/etc/metricbeat/"
用playbook部署zabbix
$ ls
hosts install_zabbix_agent.yml README.md roles/
#其中hosts为操作的主机群,install_zabbix_agent.yml为playbook,安装主入口,而roles为主要执行动作
$ ls roles/install_zabbix_agent/
defaults/ files/ handlers/ meta/ README.md tasks/ templates/ tests/ vars/
#files存放要传输的文件,tasks里放置核心执行文件main.yml,里面规定了要对主机做的操作
install_zabbix_agent.yml文件:
---
- name: install zabbix agent
hosts: "{{ zabbix_agent_group }}"
gather_facts: no
serial: 2
roles:
- install_zabbix_agent
tasks里的main.yml:
---
- name: stop zabbix_agentd
service:
name: zabbix_agentd
state: stopped
ignore_errors: True
ignore_unreachable: True
# - name: remove existed folder
# shell: "rm /usr/local/zabbix* -rf"
# - name: create user zabbix if not exist
# shell: "id zabbix || useradd zabbix"
- name: add zabbix host
shell: "sed -i '/zabbix/d' /etc/hosts && echo '{{ zabbix_server }} zabbix' >> /etc/hosts"
- name: install zabbix rpm
shell: "rpm -ivh http://mirrors.aliyun.com/zabbix/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm"
- name: install zabbix agent
shell: "yum install -y zabbix-agent"
# - name: unarchive zabbix pkg
# unarchive:
# src: "zabbix-{{ zabbix_version }}.tar.gz"
# dest: /opt
# - name: configure before make
# shell: "cd /opt/zabbix-{{ zabbix_version }} && ./configure --prefix=/usr/local/zabbix --enable-agent > /dev/null"
# - name: make
# shell: "cd /opt/zabbix-{{ zabbix_version }} && make install > /dev/null"
# - name: copy zabbix_agentd
# copy:
# src: zabbix_agentd
# dest: /etc/init.d/zabbix_agentd
# owner: root
# group: root
# mode: 0755
- name: copy zabbix_agentd.conf
copy:
src: zabbix_agentd.conf
dest: /etc/zabbix_agentd.conf
owner: root
group: root
mode: 0644
- name: powerboot zabbix
shell: "systemctl enable zabbix-agent"
- name: start zabbix-agent
service:
name: zabbix-agent
state: started
- name: wait zabbix port up
wait_for: "timeout=60 port={{ zabbix_port }} state=started"
编写完脚本后执行:ansible-playbook -v install_zabbix_agent.yml -i hosts -e "zabbix_agent_group=172.56.30.53 zabbix_server=172.56.30.50" -b -K