Ansible学习笔记

Ansible

介绍

Ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。无客户端。
在这里插入图片描述

部署Ansible

环境

一台ansible服务器,多台ansible客户机,只需要服务器安装ansible

ansible服务器与客户机添加本地解析:

vim /etc/hosts
   xxx.xxx.xxx.10 ansbile
   xxx.xxx.xxx.11 host1
   xxx.xxx.xxx.12 host2
   xxx.xxx.xxx.13 host3
   xxx.xxx.xxx.14 host4

ansible服务器操作:

yum -y install ansible

vim /etc/ansible/hosts
   host1
   host2
   host3
   host4
                         //定义主机清单

测试连通性
ansible的ping,是探测ssh程序是否连接。不是icmp协议

ansible host1(host2....) -m ping   
  //host1(host2....)表示客户机
  //-m指定模块 什么功能 ping只是其中一个模块,还有shell yum等

ansible host1 -m ping -u root -k -o    
  //-u 用户选项 -k 密码选项

vim /etc/ssh/ssh_config
   StrictHostKeyChecking no   
  //取消(yes/no)的询问,若是不取消,在测试连通性时,ssh没有连接过客户机,会发生报错.或者可以提前做好免密
  
systemctl restart sshd  
  //重启ssh服务

Ansible基础

Inventory-主机清单
1.增加主机

将host1…host4划分到webserver组
测试组内机器连通性

vim /etc/ansible/hosts
  [webserver]
   host1
   host2
   host3
   host4
   
ansible webserver -m ping -o         

2.增加用户名 密码

host[1:4]表示1至4台 ansible_ssh_user表示用户 ansible_ssh_pass表示密码

vim /etc/ansible/hosts
 [webserver]
 host[1:4] ansible_ssh_user='root' ansible_ssh_pass='666666'

ansible webserver -m ping -o    
3.增加端口

ansible_ssh_port表示端口
将host2—host4机器的ssh端口改为2222

vim /etc/ansible/hosts
 [webserver]
 host1 ansible_ssh_user='root' ansible_ssh_pass='666666'   
 host[2:4] ansible_ssh_user='root' ansible_ssh_pass='777777' ansible_ssh_port='2222'
 
ansible webserver -m ping -o    
4.组:变量

[webserver:vars]表示webserver分组中的公用变量

vim /etc/ansible/hosts
 [webserver]
 host[1:4]            
 [webserver:vars]
 ansible_ssh_user='root'
 ansible_ssh_pass='666666'  
 
 ansible webserver -m ping -o 

其他变量
在这里插入图片描述

5.子分组

[webserver:children] 表示apache与mysql是webserver的子分组

vim /etc/ansible/hosts
 [apache]
  host[1:2]
 [mysql]
  host[3:4]
 [webserver:children]
  apache
  mysql
 [webserver:vars]
  ansible_ssh_user='root'
  ansible_ssh_pass='666666'
  
ansible webserver -m ping -o 
6.自定义主机列表

在任意目录下创建主机清单

vim hostlist  
 [dockers]
  host1
  host2
 [dockers:vars]
  ansible_ssh_user='root'
  ansible_ssh_pass='666666'

ansible -i  /xx/hostlist dockers  -m ping  -o   //-i 表示指定外部清单路径 

Ad-Hoc-点对点模式

1.复制模块

ansible-doc copy   //帮助

ansible host1 -m copy -a'src=/root/1 dest=/tmp/1 force=no' 
//复制,如果客户机存在该文件,则不继续复制   强制复制force=yes或者不写force

ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777'    
//将本机的/etc/hosts  复制到客户机的/tmp/2.txt中 并且属主为root 属组为bin  权限为777

ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777 backup=yes'   
//在复制后,进行备份

将客户机的文件复制到ansible服务器

ansible 被管理机名称或组名 -m fetch -a 'src=原路径 dest=复制过去的路径'

ansible host1 -m fetch -a'src=/tmp/1 dest=/root' 

2.用户模块

ansible-doc user

ansible webserver -m user -a'name=yzp state=present' 
//创建用户yzp   state=present表示创建

echo '1qw' | openssl passwd -1 -stidn  
//生成加密密码
ansible webserver -m user -a'name=yzp password="********"'  
//修改用户yzp的密码,其中***表示加密密码的密文

ansible webserver -m user -a 'name=yzp shell=/sbin/nologin append=yes'   
//修改用户启动的shell  append表示追加

ansible webserver -m user -a 'name=yzp state=absent'  
//删除用户  state=absent表示删除   -a'..... remove=yes'彻底删除用户

3.软件包管理模块

ansible-doc yum

ansible host1 -m yum -a 'name="*" state=latest'  
//升级或安装所有包   state=latest或者state=present安装

ansible host2 -m yum -a 'name="httpd" state=latest'  
//安装apache服务

ansible host2 -m yum -a 'name="httpd" state=absent'  
//卸载apache服务   state=absent或者state=removed卸载

4.服务模块

ansible-doc service

ansible host2 -m service -a 'name=httpd state=started'  
//启动httpd服务

ansible host2 -m service -a 'name=httpd state=started enabled=yes'   
//永久启动httpd服务

ansible host2 -m service -a 'name=httpd state=stopped' 
//关闭服务

ansible host2 -m service -a 'name=httpd state=restarted' 
//重启服务

ansible host2 -m service -a 'name=httpd state=started enabled=no'   
//启动服务,但是不开机自启

5.文件模块

ansible-doc file

ansible host1 -m file -a 'path=/tmp/88.txt mode=777 state=touch'  
 //在客户机的/tmp创建88.txt文件  并且权限为777

ansible host1 -m file -a 'path=/tmp/99 mode=777 state=directory' 
 //在客户机的/tmp创建99的目录  并且权限为777

ansible host1 -m file -a'src=/tmp/ceshi dest=/tmp/ceshi.txt state=link'  
 //给客户机的/tmp/ceshi文件创建软连接

ansible host1 -m file -a'src=/tmp/1 dest=/tmp/1.txt state=hard'  
 //给客户机的/tmp/ceshi文件创建硬连接

ansible host1 -m file -a'path=/tmp/1 modification_time=now  state=file'  
 //修改 文件修改 的时间

ansible host1 -m file -a'path=/tmp/md recurse=yes owner=yzp group=yzp state=directory' 
 //递归改变目录的所有权

ansible host1 -m file -a'path=/tmp/1/2  state=absent' 
 //删除文件或者目录

6.收集模块

ansible-doc steup

ansible host3 -m setup   
 //查询所有信息

ansible host3 -m setup -a 'filter=ansible_all_ipv4_addresses' 
 //查询客户机IP地址

7.shell模块

ansible-doc shell   //帮助

ansible webserver -m shell -a'hostname' -o     
//获取主机名

ansible webserver -m shell -a'hostname' -o -f2   
//获取主机名    -f2表示线程数

ansible webserver -m shell -a'yum -y install httpd' 
//部署apache服务

ansible webserver -m shell -a'uptime' -o 
//查询系统负载均衡

YAML-YAML Ain’t Markup Language-非标记语言

实战测试:通过YAML编写一个简单的剧本,完成web的部署,配置,启动的全过程。
注意格式

vim apache.yaml

- hosts: webserver
  tasks:
  - name: stop firewalld
    service: name=firewalld state=stopped
  - name: stop selinux
    shell: sed -i '7c\SELINUX=disabled'  /etc/selinux/config
  - name: install apache
    yum: name=httpd state=present
  - name: copy apache xonf
    copy: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf
    notify: systemctl restart httpd
  - name: apache running
    service: name=httpd state=started
  handlers:
  - name: systemctl restart httpd
    service: name=httpd state=restarted

notify设置触发器----引用处理程序,配置文件改变,重启服务
handlers中name: systemctl restart httpd----定义处理程序
其中notify: systemctl restart httpd与handlers中name: systemctl restart httpd必须一样

Role-角色扮演

简介:roles则是在ansible中,playbooks的目录组织结构。将代码或文件进行模块化,成为roles的文件目录组织结构,易读,代码可重用,层次清晰。

1.目录结构

在这里插入图片描述
nginx 角色名
files 普通文件
handlers 触发器程序
tasks 主任务
templates 金甲模板(有变量的文件)
vars 自定义变量

mkdir -p roles/nginx/{files,handlers,tasks,templates,vars} 
 //创建目录

touch roles/site.yaml  

touch roles/nginx/{handlers,tasks,vars}/main.yaml 
 //在目录下创建文件

echo 1234 > roles/nginx/files/index.html  
//简单的网页页面

yum install -y nginx       
//安装nginx

cp /etc/nginx/nginx.conf roles/nginx/templates/nginx.conf.j2  
//将配置文件复制到 templates金甲模板(有变量的文件)中

2.编写任务

vim roles/nginx/tasks/main.yaml

---
- name: install epel-release packge
  yum: name=epel-release state=latest

- name: install nginx packge
  yum: name=nginx  state=latest

- name: copy index.html
  copy: src=index.html dest=/usr/share/nginx/html/index.html

- name: copy nginx.conf template
  template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
  notify: restart nginx

- name: make sure nginx service running
  service: name=nginx state=started enabled=yes

3.准备配置文件

vim roles/nginx/templates/nginx.conf.j2

worker_processes  {{ ansible_processor_cores }};  
 //{{ x }}调用内部已知变量,参考收集模块中的"ansible host3 -m setup"

worker_connections {{ worker_connections }};  
 //{{ x }}自定义变量

4.编写变量

vim roles/nginx/vars/main.yaml

worker_connections: 10240     //给自定义变量赋值(即配置文件中的)

5.编写处理程序

vim roles/nginx/handlers/main.yaml

---
- name: restart nginx
  service: name=nginx state=restarted
                          //被触发后,执行的重启nginx

6.编写剧本

vim roles/site.yaml

- hosts: host4
  roles:
  - nginx
                //剧本

7.实施

cd roles  
 
ansible-playbook site.yaml --syntax-check   //检查是否存在语法错误

ansible-playbook site.yaml    //实施剧本
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值