ansible常用套路(一)

一、SSH互信

1 配置/etc/ansible/hosts 文件

[zabbix_agent]
172.26.4.203
172.26.4.204
172.26.4.205
[zabbix_agent:vars]
ansible_ssh_user=root
ansible_ssh_pass=root1234
ansible_ssh_port=22

  

2 在ansible-server端生成秘钥对

ssh-keygen -t rsa

  

3 创建playbook

1) 第一种方式: 
# 新增本地(ansible-server)公钥内容到远端客户端.ssh目录中authorized_keys文件,没有则创建authorized_keys文件
# state: 1) present 添加,2) absent 删除
---
- hosts: zabbix_agent
  gather_facts: false

  tasks:
    - name: deliver authorized_keys
      authorized_key:
        user: root
        key: "{{ lookup('file', '/root/.ssh/id_rsa.pub) }}"

# 解释:
# 添加或移除authorized keys为特定用户:比如上面的是添加读取本地的id_rsa.pub文件到远端主机的authorized_keys文件中
# 把ansible-server中的/root/.ssh/id_ras.pub中的文件内容,拷贝到其他主机中的/root/.ssh/authorized_keys 文件中,实现SSH互信

  

第二种书写方式:

- hosts: zabbix_agent
  remote_user: root

  tasks:
  - name: mkdir /root/.ssh
    command: mkdir -p /root/.ssh

  - name: copy ssh key
    copy: src=/root/.ssh/id_rsa.pub dest=/root/.ssh owner=root group=root mode=0644

 

这个playbook是经过测试的

# 新增本地(ansible-server)公钥内容到远端客户端.ssh目录中authorized_keys文件,没有则创建authorized_keys文件
# state: 1) present 添加,2) absent 删除
---
- hosts: zabbix_agent
  gather_facts: false
  remote_user: root

  tasks:
  - name: deliver authorized_keys
    authorized_key:
      user: root
      key: "{{ lookup('file', '/root/.ssh/id_rsa.pub') }}"

 

4 运行

cd /etc/ansible/playbook/ssh_trust
ansible-playbook rsync_key.yml 

  

5 可能出现的错误和解决方法

这里只会罗列出,我遇到的错误以及解决方法,如果你和我一样遇到了这个错误,那么恭喜你。咱俩还挺有缘

[root@ansible-server ssh_trust]# ansible-playbook rsync_key.yml 

PLAY [zabbix_agent] ************************************************************************************************************************************************

TASK [deliver authorized_keys] *************************************************************************************************************************************
fatal: [172.26.4.204]: FAILED! => {"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."}
fatal: [172.26.4.203]: FAILED! => {"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."}
fatal: [172.26.4.205]: FAILED! => {"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."}
	to retry, use: --limit @/etc/ansible/playbook/ssh_trust/rsync_key.retry

PLAY RECAP *********************************************************************************************************************************************************
172.26.4.203               : ok=0    changed=0    unreachable=0    failed=1   
172.26.4.204               : ok=0    changed=0    unreachable=0    failed=1   
172.26.4.205               : ok=0    changed=0    unreachable=0    failed=1 


2)原因和解决办法:
ssh第一次连接的时候一般会提示输入yes 进行确认为将key字符串加入到 ~/.ssh/known_hosts 文件中。而本机的~/.ssh/known_hosts文件中并有fingerprint key串
解决方法:在ansible.cfg文件中更改下面的参数:
#host_key_checking = False 将#号去掉即可

  

 

二 批量安装配置zabbix-agent

上面的很简单是不是,下面咱们来票大的。这里需要说明的是,使用playbook编写文件时,尽量使用ansible提供的模块,尽量避免使用shell这个模块。因为ansible具有【幂等性】这个特点,具体什么是幂等性,请自行谷歌吧。这里只是简单的说一下。举个栗子,假如我们创建一个文件,第一次没有,我们运行可以创建成功,如果第二次运行一般会报错,说文件已存在。如果使用ansible提供的模块,因为具备【幂等性】特点,则不会报错。他应该有检查机制,存在则不创建了。好了,下面开始咱们看一下使用ansible的最常用的套路吧

如果各位看官对playbook有兴趣可以移步:https://galaxy.ansible.com/

下面开始我们的实战了,激动不。

1 定义hosts文件

这个hosts文件位于/etc/ansible/hosts 如果是yum安装的,一般配置文件都位于这里,hosts文件中定义的组,主机等等。都是我们操作的对象

/etc/ansible/hosts

[zabbix_agent]
172.26.4.203
172.26.4.204
172.26.4.205
[zabbix_agent:vars]
ansible_ssh_user=root
ansible_ssh_pass=root1234
ansible_ssh_port=22

  

2 查看我们的目录结构

下面的目录结构是不是觉得很多,是很多,但是每个都是很有用的。

给你一个创建的shell吧,下面创建的目录是在 /etc/ansible 目录中创建的

#!/bin/bash

mkdir zabbix_centos7
mkdir zabbix_centos7/zabbix_agent
mkdir zabbix_centos7/zabbix_agent/roles
mkdir zabbix_centos7/zabbix_agent/roles
mkdir zabbix_centos7/zabbix_agent/roles/{common,install,uninstall,configure}/{handlers,files,meta,tasks,templates,vars} -p

  

下面是展开的样子

[root@ansible-server ansible]# tree zabbix_centos7
zabbix_centos7
├── init.sh
├── readme.md
└── zabbix_agent
    ├── roles
    │   ├── common
    │   │   ├── files
    │   │   ├── handlers
    │   │   │   └── main.yml
    │   │   ├── meta
    │   │   ├── tasks
    │   │   │   └── main.yml
    │   │   ├── templates
    │   │   └── vars
    │   │       └── main.yml
    │   ├── configure
    │   │   ├── files
    │   │   │   └── zabbix_scripts
    │   │   │       ├── discovery_tcp_port.sh
    │   │   │       └── tcp_connect_status.sh
    │   │   ├── handlers
    │   │   │   └── main.yml
    │   │   ├── meta
    │   │   ├── tasks
    │   │   │   ├── 01-sync-clock.yml
    │   │   │   ├── 02-allow-sudo.yml
    │   │   │   ├── 03-sync-conf_files.yml
    │   │   │   └── main.yml
    │   │   ├── templates
    │   │   │   ├── Userparameter_script.conf
    │   │   │   └── zabbix_agentd.conf
    │   │   └── vars
    │   │       └── main.yml
    │   ├── install
    │   │   ├── files
    │   │   │   └── ansible-zabbix-4.0.0.tar.gz
    │   │   ├── handlers
    │   │   ├── meta
    │   │   ├── tasks
    │   │   │   ├── 01-create-user.yml
    │   │   │   ├── 02-copy-code.yml
    │   │   │   ├── 03-start-zabbix.yml
    │   │   │   ├── 04-add-iptables.yml
    │   │   │   └── main.yml
    │   │   ├── templates
    │   │   │   ├── zabbix_agentd.conf
    │   │   │   └── zabbix-agent.service
    │   │   └── vars
    │   │       └── main.yml
    │   └── uninstall
    │       ├── files
    │       ├── handlers
    │       ├── meta
    │       ├── tasks
    │       │   ├── main.yml
    │       │   └── uninstall_zabbix.yml
    │       ├── templates
    │       └── vars
    │           └── main.yml
    ├── zabbix_configure.retry
    ├── zabbix_configure.yml
    ├── zabbix_delete.retry
    ├── zabbix_delete.yml
    ├── zabbix_install.retry
    └── zabbix_install.yml

  

3 安装程序的tasks任务列表

1)定义安装程序入口文件 zabbix_install.yml
[root@ansible /etc/ansible/zabbix_centos7/zabbix_agent ]# vim zabbix_install.yml 
---
- hosts: testhosts
  remote_user: root
  gather_facts: True
  roles:
    - common
    - install

2) 定义安装程序-创建用户任务01-create-user.yml
[root@ansible /etc/ansible/zabbix_rhel/zabbix_agent ]# vim roles/install/tasks/01-create-user.yml 
---
- name: Create zabbix user
  user: name={{ zabbix_user }} state=present create_home=no shell=/sbin/nologin

3) 定义安装程序-拷贝安装文件任务02-copy-code.yml
[root@ansible /etc/ansible/zabbix_rhel/zabbix_agent ]# vim roles/install/tasks/02-copy-code.yml 
---
- name: Create src dir
  file: path=/opt/source state=directory mode=0755

- name: Create install dir
  file: path={{ zabbix_dir }} state=directory mode=0755

- name: Copy zabbix agentd code file to clients
  copy: src=ansible-zabbix-{{ zabbix_version }}.tar.gz dest=/opt/source/ansible-zabbix-{{ zabbix_version }}.tar.gz
        owner=root group=root

- name: Uncompression ansible-zabbix-{{ zabbix_version }}.tar.gz
  shell: tar xf /opt/source/ansible-zabbix-{{ zabbix_version }}.tar.gz -C {{ zabbix_dir }}

- name: Copy zabbix start scripts
  template: src=zabbix-agent.service dest=/usr/lib/systemd/system/zabbix-agent.service owner=root group=root mode=0755

- name: Copy zabbix config file
  template: src=zabbix_agentd.conf dest={{ zabbix_dir }}/etc/zabbix_agentd.conf owner={{ zabbix_user }} group={{ zabbix_user }}
            mode=0644

- name: Modify zabbix basedir permission
  file: path={{ zabbix_dir }} owner={{ zabbix_user }} group={{ zabbix_user }} mode=0755 recurse=yes

- name: Link zabbix-agent.service command
  file: src={{ zabbix_dir }}/sbin/zabbix_agentd dest=/usr/local/sbin/zabbix_agentd state=link

- name: Delete ansible-zabbix-{{ zabbix_version }}.tar.gz source file
  shell: rm -f /opt/source/ansible-zabbix-{{ zabbix_version }}.tar.gz

4) 定义安装程序-启动zabbix_agentd服务任务03-start-zabbix.yml 
[root@ansible /etc/ansible/zabbix_rhel/zabbix_agent ]# vim roles/install/tasks/03-start-zabbix.yml 
---
- name: add permit tmp
  file: path=/tmp owner=root mode=0777 recurse=yes
- name: Start zabbix service
  service: name=zabbix-agent.service state=started enabled=yes

5) 定义安装程序-添加iptable规则04-add-iptables.yml 
---
- name: insert iptables rule for zabbix
  lineinfile: dest=/etc/sysconfig/iptables create=yes state=present regexp="{{ zabbix_agentd_port }}"
              insertafter="^:OUTPUT"
              line="-A INPUT -p tcp --dport {{ zabbix_agentd_port }} -s {{ zabbix_server_ip }} -j ACCEPT"

  notify: stop firewalld

6) 定义安装程序-tasks任务列表的主调用接口文件main.yml
---
- include: 01-create-user.yml
- include: 02-copy-code.yml
- include: 03-start-zabbix.yml
- include: 04-add-iptables.yml

  

总结一下上面的

总结: tasks任务列表说明:
Playbook允许用户将tasks任务细分为多个任务列表,通过一个main任务来调用。
当然你也可以将涉及的所有任务全部写到main.yml文件中。

  

继续...

下面的提供一下git地址吧。自行去看吧。

https://github.com/wanstack/playbook

 

posted @ 2018-10-29 13:49  wanstack  阅读(420)  评论(0编辑  收藏  举报