我的Ansible学习笔记

Posted ImJerryChan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我的Ansible学习笔记相关的知识,希望对你有一定的参考价值。

Ansible
常见错误 http://afewbug.com/article/26
官方文档 http://docs.ansible.com/ansible/
安装配置 http://sofar.blog.51cto.com/353572/1579894

 

目录
一、介绍
二、yum安装
三、Ansible配置
四、模块
五、playbook
六、实战演练--用Ansible实现LANMP的安装

注意:以下实验环境均为关闭selinux和清空防火墙配置,需要把/etc/my.cnf 改为/etc/my.cnf.bak

 

一、介绍
特性
(1)、no agents:不需要在被管控主机上安装任何客户端;
(2)、no server:无服务器端,使用时直接运行命令即可;
(3)、modules in any languages:基于模块工作,可使用任意语言开发模块;
(4)、yaml,not code:使用yaml语言定制剧本playbook;
(5)、ssh by default:基于SSH工作;
(6)、strong multi-tier solution:可实现多级指挥。

优点
(1)、轻量级,无需在客户端安装agent,更新时,只需在操作机上进行一次更新即可;
(2)、批量任务执行可以写成脚本,而且不用分发到远程就可以执行;
(3)、使用python编写,维护更简单,ruby语法过于复杂;
(4)、支持sudo。


二、yum安装

yum install -y epel-release
yum install -y ansible


附注:编译安装参考上述链接


三、Ansible配置
(1)SSH免密钥登陆设置具体参考SSH密钥登陆

http://www.cnblogs.com/ImJerryChan/p/6002508.html


(2)ansible配置
主机组配置参考:

# vi  /etc/ansible/hosts
# 在里面增加如下配置增加
[testhost]
127.0.0.1
192.168.75.135

配置解析:
在/etc/ansible/hosts文件里指定被监控的主机
其中testhost为自定义的主机组名。
下面两个ip为组内的机器ip。


(3)简单测试

ansible 192.168.75.135 -m command -a \'w\'   或者
ansible testhost  -m command -a \'w\'

如果结果为如下配置,则配置成功

附注:
错误:    "msg": "Aborting, target uses selinux but python bindings (libselinux-python) aren\'t installed!"
解决办法: yum install -y libselinux-python


四、模块
五、playbook
这两个部分过两天再补充吧,毕竟现在还没学完呢,路还很长

 

六、实战演练--用Ansible实现LANMP的安装

(这个也属于一个未完成版,因为在写这篇文章的时候还在敲其余部分的代码,因此这一两天内补上吧,mysql部分实验没问题)
(一)Mysql

cd /etc/ansible
mkdir mysql_install
mkdir -p mysql_install/roles/{common,delete,install}/{handlers,files,meta,tasks,templates,vars}

cd mysql_install/roles
//安装前准备
vi ./common/tasks/main.yml
- name: Install initialization require software
  yum: name={{ item }} state=installed
  with_item:
    - gcc
//安装过程
vi ./install/vars/main.yml
mysql_user: mysql
mysql_port: 3306
mysql_data_dir: /data/mysql
mysql_version: 5.5.54

ls ./install/templates
/etc/init.d/my.cnf   /etc/init.d/mysqld
分别为配置文件以及启动文件,也就是要拷贝进去

ls ./install/files
mysql-5.5.54.tar.gz
data_mysql.tar.gz
同上,一个小建议就是打包的时候最好到文件/目录所在处再打包,否则如果前面跟了目录的话,会把目录也打包进来的 vi .
/install/tasks/copy.yml - name: Copy MySQL Software To Redhat client copy: src=mysql-{{ mysql_version }}.tar.gz dest=/usr/local/src/mysql-{{ mysql_version }}.tar.gz owner=root group=root when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6 - name: Uncompression MySQL Software To Redhat Client shell: tar zxf /usr/local/src/mysql-{{ mysql_version }}.tar.gz -C /usr/local/ when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6 - name: Copy Data_Dir To Redhat client copy: src=data_mysql.tar.gz dest=/usr/local/src/data_mysql.tar.gz owner=root group=root when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6 - name: Uncompression Data_Dir To Redhat Client shell: tar zxf /usr/local/src/data_mysql.tar.gz -C / when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6 - name: Copy MySQL Start Script To Redhat Client template: src=mysqld dest=/etc/init.d/mysqld owner=root group=root mode=0755 when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6 - name: Copy MySQL Config To Redhat Client template: src=my.cnf dest=/etc/init.d/my.cnf when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6 vi ./install/tasks/install.yml - name: Create MySQL User In Redhat Client user: name={{ mysql_user }} state=present createhome=no shell=/sbin/nologin when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6 - name: Start MySQL Service In Redhat Client service: name=mysqld state=started# when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6 - name: Add Boot Start MySQL Service In Redhat Client shell: chkconfig --level 345 mysqld on when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6 vi ./install/tasks/delete.yml - name: Delete MySQL compression Software In Redhat Client shell: rm -rf /usr/local/src/mysql-{{ mysql_version }}.tar.gz when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6 vi ./install/tasks/main.yml - include: copy.yml - include: install.yml - include: delete.yml
//卸载过程
vi ./delete/vars/main.yml
mysql_user: mysql
mysql_port: 3306
mysql_data_dir: /data/mysql
mysql_version: 5.5.54-linux2.6-x86_64

vi ./delete/tasks/delete.yml
- name: Stop MySQL Service
  shell: ps -ef|grep mysql|grep -v grep|awk \'{print $2}\'|xargs kill -9 >>/dev/null 2>&1
  ignore_errors: yes
  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Delete MySQL Boot Start Script
  shell: chkconfig --del mysql
  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Delete MySQL Dir
  shell: rm -rf /usr/local/mysql
  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Delete MySQL User
  shell: userdel {{ mysql_user }}
  ignore_errors: yes
  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Delete MySQL Service Start Script
  shell: rm -rf /etc/init.d/mysqld
  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6
- name: Delete MySQL Service Config File
  shell: rm -rf /etc/init.d/my.cnf
  when: ansible_os_family == "RedHat" and ansible_distribution_version|int >=6

vi ./delete/tasks/main.yml
- include: delete.yml
//MySQL主入口配置文件
cd ../
vi install.yml
---
- hosts: 192.168.75.135
  remote_user: root
  gather_facts: True
  roles:
    - common
    - install
    
vi delete.yml
---
- hosts: 192.168.75.135
  remote_user: root
  gather_facts: True
  roles:
    - delete
完毕
安装MySQL:ansible-playbook install.yml
卸载MySQL:ansible-playbook delete.yml

 

以上是关于我的Ansible学习笔记的主要内容,如果未能解决你的问题,请参考以下文章

Ansible 学习笔记

ansible学习笔记

Ansible学习笔记

Ansible学习笔记1

Ansible 学习笔记

ansible学习笔记