ansible使用

安装

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值