一、abstract-简介

1.logo: 2.介绍: (1)ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点, 实现了批量系统配置、批量程序部署、批量运行命令等功能。 无客户端。 (2)我们要学一些Ansible的安装和一些基本概念,然后我们会开始研究一些真正有意思的东西 – playbook,配置管理,部署以及语法编排.我们将会学习如何使用/usr/bin/ansible执行ad-hoc并行命令,我们还会学习ansible的核心有什么样的模块可供使用.当然以后你也可以写你自己的模块,我们会在后期讲到. (3)工作原理:

二、instal-部署:

1.环境:ansible服务器 10.8.162.86 ansible客户机:10.8.162.60 2.ansible服务器:域名解析:

vim /etc/hosts 10.8.162.86 ansible 10.8.162.60 host1 注:anisble客户机只需准备好ip 和yum源环境即可

3.anisble服务器下载ansible及相关扩展包

yum install -y epel-relea 注:安装epel源,如果您在非学校环境,请使用下方阿里YUM (1):rm -rf /etc/yum.repos.d/* (2):wgrt -O /etc/yum.repos.d/epel.repohttp://mirrors.aliyun.com/repo/epel-7.repo (3)wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo 4.>yum install -y ansible (1)部署是否完成:

5.ansible服务器基础: (1)定义主机清单:

vim /etc/ansible/host host1

(2)测试连通性:ansible host1 -m ping

ansible host1 -m ping 结果失败了 (3)ansible host2 -m ping -u root -k -o 增加用户名选项 增加用户名选项 (4)去掉(yes/no)的询问 第一次连通会提示输入yes,由于我已经做过好几次了,所以没显示,请见谅! vim /etc/ssh/ssh_config StrictHostKeyChecking no 重启sshd:systemctl restart sshd 测试 (5)特殊情况:

(6)ssh-key(可选) 免密码ssh-key的方式。 ssh-keygen ssh-copy-id IP地址 推送公钥 ansible host1 -m ping -o 简介输出,直接能成功

三、Inventory -主机清单

1.含义:清查;存货清单;财产目录;主机清单 2.增加主机组:由于我的只有一台客户机,所以我就只把host1拉一个组 >vim /etc/ansible/hosts >[webserver] >host1

输出这个主机组就可以了。主机组可以有多个anisbile客户机 ansible webserver -m ping -u root -k -o

3.增加用户名,密码

(1)vim /etc/ansible/hosts 一台客户机这样写 [webserver] host1 ansible_ssh_user='root' ansible_ssh_pass='666666' 测试,不需要密码了

(2)假入有四台客户机这样写:

[webserver] host[1:4] ansible_ssh_user='root' ansible_ssh_pass='666666' 四台需密码一致,用户一致

(3)请思考主机和主机的用户名密码不同。如何设置? [webservers] host1 ansible_ssh_user='root' ansible_ssh_pass='777777' host[2:4] ansible_ssh_user='root' ansible_ssh_pass='666666'

4.增加端口: (1)请将host1的sshd程序端口修改为2222

vim /etc/ssh/sshd_config port 222 重启sshd systemctl restart sshd ansible webservers -m ping -o 失败,因为默认端口已更改 (2)解决办法 配置文件增加端口: [webserver] host1 ansible_ssh_user='root' ansible_ssh_pass='666666' ansible_ssh_port='2222' 测试: 成功 请将用户名密码和端口回复原状

5.组:变量 (1)ansible内部变量可以帮助我们简化主机清单的设置 >vim /etc/ansible/hosts 多台客户机情况 >[webserver] host[1:4] [webserver:vars] ansible_ssh_user='root' ansible_ssh_pass='666666'

常用变量:

6.子分组:在把多个组的组名合到一个组里面,更利于管理,用于本机硬件问题,就不演示了 ,写下来给你们看看

(1)vim /etc/ansible/hosts [apache] host[1:2] [nginx] host[3:4] [webserver:children] apache nginx [webserver:vars] ansible_ssh_user='root' ansible_ssh_pass='666666'

自定义主机列表: (2)>vim hostlist [dockers] host1 host2 [dockers:vars] ansible_ssh_user='root' ansible_ssh_pass='666666' ansible -i hostlist dockers -m ping -o

7.Ad-Hoc-点对点模式 (1)简介:临时的,在ansible中是指需要快速执行的单条命令,并且不需要保存的命令。对于复杂的命令则为 playbook。 (2)shell模块:

①:ansible webserver -m shell -a 'hostname' -o //获取主机名 ②:ansible webserver -m shell -a 'hostname' -o -f 2 //-f 2 指定线程数 -f FORKS, --forks=FORKS Ansible一次命令执行并发的线程数。NUM被指定为一个整数,默认是5 specify number of parallel processes to use (default=5) ③ansible host1 -m shell -a 'yum -y install httpd' -o //给host1部署apache ④ansible host1 -m shell -a 'uptime' -o //查看负载信息

(3)复制模块 :帮助:ansible-doc copy ①:案列1:ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777' //把服务器上的/etc/hosts里的文件拷贝到webserver组的所有客户端

客户端的文件 ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777' 组里的客户机收到的文件 ②案例:如果文件有多份,可以进行备份。 再次修改hosts文件 ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777 backup=yes' 查看客户机 (3)用户模式:帮助:ansible-doc user ①创建用户:ansible webserver -m user -a 'name=xiaowang123 state=present' 客户机验证 ②:修改密码:生成加密密码:echo '777777' | openssl passwd -1 -stdin 生成密码值:$1$ROZP4CHD$xz0E.6.d1H.8EUAag4JXR1 修改密码:ansible webserver -m user -a 'name=xiaowang123 password="$1$ROZP4CHD$xz0E.6.d1H.8EUAag4JXR1"' 客户机验证:登录成功 ③:修改shell ansible webserver -m user -a 'name=xiaowang123 shell=/sbin/nologin append=yes' 删除用户: ansible webserver -m user -a 'name=xiaowang123 state=absent' 客户机验证

(4)软件包管理:

①:帮助:ansible-doc yum ②:ansible host1 -m yum -a 'name="*" state=latest' //升级所有包 ③:ansible host1 -m yum -a 'name="httpd" state=latest'//安装apache

(5)服务模块 ①:ansible host1 -m service -a 'name=httpd state=started'//启动 ②:ansible host1 -m service -a 'name=httpd state=started enabled=yes'//开机自启 ③:ansible host1 -m service -a 'name=httpd state=stopped' //停止 ④ansible host1 -m service -a 'name=httpd state=restarted' //重启 ⑤:ansible host1 -m service -a 'name=httpd state=started enabled=no' //开机禁止启动

(6):文件模块: ①:帮助:ansible-doc file ②:ansible host1 -m file -a 'path=/tmp/88.txt mode=777 state=touch' //创建文件 ③:ansible host1 -m file -a 'path=/tmp/99 mode=777 state=directory' //创建目录

(7)收集模块 ①:帮助:ansible-doc file

②:ansible host1 -m setup //查询所有信息 ③:ansible host1 -m setup -a'filter=ansible_all_ipv4_addresses'

(8)YAML-YAML Ain’t Markup Language-非标记语言 ①:语法:列表: fruits: - Apple - Orange - Strawberry
- Mango ②:martin: name: Martin D'vloper job: Developer skill: Elite

③:实例: ansible服务器: 准备共作 ansible all -m yum -a 'name=httpd state=removed' -o //清理环境 yum install -y httpd //准备配置文件 mkdir apache cd apache cp -rf /etc/httpd/conf/httpd.conf . grep '^Listen' httpd.conf Listen 8080 修改配置,用作推送

编写剧本 vim apache.yaml

  • hosts: host1 tasks:
  • name: install apache packages yum: name=httpd state=present
  • name: copy apache conf copy: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf
  • name: ensure apache is running service: name=httpd state=started enabled=yes

注:里边的文件一定要照好,不行可以复制我的,然后修改里边的东西 ansible-playbook apache.yaml --syntax-check //检测语法 ansible-playbook apache.yaml --list-tasks //列出任务 ansible-playbook apache.yaml --list-tasks //列出主机 ansible-playbook apache.yaml 测试:http://10.8.162.62:8080 //http的测试页面

注:列出客户机为何起不来的httpd服务 查看端口:是否80端口和别的程序80端口冲突 netstat -tunlp |grep :80 //查看端口 ,如果端口冲突,修改端口 vim /etc/httpd/conf/httpd.conf //修改端口 systemctl start httpd //启动httpd systemctl stuts httpd //查看状态 ,

④:handlers:
下载

如果配置文件发生变化 :Listen 8888 [root@xiaoliu apache]# vim httpd.conf ansible-playbook apache.yaml //再次执行,命令成功,但配置未生效,所以要增加处理程序。设置触发器 需要在写入处理程序; vim apache.taml 如果配置文件再发生变化。 Listen 9080

[root@xiaoliu apache]# vim httpd.conf ansible-playbook apache.yaml :再次执行,配置生效,触发成功 测试成功:

(9):Role-角色扮演 yum -y install tree ①:简介:roles则是在ansible中,playbooks的目录组织结构。 将代码或文件进行模块化,成为roles的文件目录组织结构, 易读,代码可重用,层次清晰。 ②:目标:通过role远程部署nginx并配置 ③:目录结构: ④:准备目录结构

mkdir roles/nginx/{files,handlers,tasks,templates,vars} -p mkdir roles/nginx/{files,handlers,tasks,templates,vars} -p echo 1234 > roles/nginx/files/index.html yum install -y nginx && cp /etc/nginx/nginx.conf roles/nginx/templates/nginx.conf.j2 查看: tree roles/ ⑤:编写任务 :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 ⑥:准备配置文件: vim roles/nginx/templates/nginx.conf.j2 worker_processes {{ ansible_processor_cores }}; //调用内部已知变量 worker_connections {{ worker_connections }}; //自定义变量 修改前: 修改后: ⑦:编写变量:vim roles/nginx/vars/main.yaml //worker_connections: 10240 ⑧:编写处理程序:vim roles/nginx/handlers/main.yaml ’--- ‘- name: restart nginx service: name=nginx state=restarted‘ ⑨:编写剧本:vim roles/site.yaml - hosts: host4 roles: - nginx ⑩:实施: cd roles ansible-playbook site.yaml --syntax-check //测试 ansible-playbook site.yaml //实施剧本 验证host1