1.ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点。
2.实现了批量系统配置、批量程序部署、批量运行命令等功能。
1.没有客户端,通过ssh跟其他人进行连接。
2.面对高并发,如何去对应众多客户主机(当管理的机器特别多,发出的信号容易拥塞,延迟丢包的时候)可以加上消息队列0mq。
3.可以通过定义主机群、主机组更好更方便的管理计算机集群。
4.可以通过各种模块实现一些较简单的功能。
5.通过playbooks实现一些复杂的功能。
ansible服务器:192.168.0.155
ansible客户机:192.168.0.104
192.168.0.105
域名解析 vim /etc/hosts
192.168.0.115 ansible
192.168.0.104 host1
192.168.0.105 host2
ansible客户机(IP、YUM源、关闭防火墙和selinux)
无需配置
ansible服务器:yum -y install ansible
检测部署是否完成:
rpm -qa 列出所有文件
rpm -qc ansible 查看配置文件
1.免密码ssh-key的方式
ssh-keygen
ssh-copy-id IP地址 推送公钥
2.如果没有提前做免密
(1)ansible host2 -m ping 输入yes确认连接,会显示红色不可达,失败了
(2)ansible host2 -m ping -u root -k -o
-u 增加用户名选项
-k 增加密码选项
(3)去掉是否连接(yes/no)的询问:
==vim /etc/ssh/ssh_config==
StrictHostKeyChecking no #进行修改
systemctl restart sshd
ansible host2 -m ping -u root -k -o 成功不提示是否连接
vim /etc/ansible/hosts
host1
host2
ansible host1 -m ping -o 测试host1连通性,显示绿字成功,红字失败
-m 指定模块。什么功能
ping 只是其中一个模块
-o 简洁输出
使用OpenSSH时,ssh会把访问过的每个计算机公钥都记录在~/.ssh/know_hosts
ping ICMP:网际消息管理协议(网络层)
ssh 应用层安全登录协议
ansible的ping,是探测ssh程序是否连接,不是icmp协议。
vim /etc/ansible/hosts
[webserver]
host1
host2
ansible webserver -m ping -o
关于返回信息与文件中的IP地址顺序不同,是因为网络延迟及主机处理的速度不同导致的。
vim /etc/ansible/hosts
[webserver]
host[1:2] ansible_ssh_user='root' ansible_ssh_pass='123' ansible_ssh_port='22'
vim /etc/ansible/hosts
[webserver]
host[1:2]
[webserver:vars] #var 变量
ansible_ssh_user='root'
ansible_ssh_pass='1'
vim /etc/ansible/hosts
[apache]
host1
[nginx]
host2
[webserver:children]
apache
nginx
简介:临时的,在ansible中是指需要快速执行的单条命令,并且不需要保存的命令。对于复杂的命令则为 playbook。
ansible-doc shell
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 host2 -m shell -a 'yum -y install httpd' -o
ansible host1 -m shell -a 'uptime' -o
ansible-doc copy
ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777'
src(source)资源
dest(destination)目的地
mode 模式,权限
若出现黄色字,为警告,可能权限不同
ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777 backup=yes'
ansible-doc user
ansible webserver -m user -a 'name=shichen state=present'
state=present 创建
1.生成加密密码
echo '512050951' | openssl passwd -1 -stdin
生成加密密码值:
$1 $XVzsJMDr$5wI4oUaQ.emxap6s.N272.
2.修改密码
ansible webserver -m user -a 'name=shichen password="$1 $XVzsJMDr$5wI4oUaQ.emxap6s.N272."'
ansible webserver -m user -a 'name=shichen shell=/sbin/nologin append=yes'
append 追加
ansible webserver -m user -a 'name=shichen state=absent'
state=absent 删除
ansible-doc yum
ansible host1 -m yum -a 'name="*" state=latest'
ansible host2 -m yum -a 'name="httpd" state=latest'
ansible host2 -m yum -a 'name="httpd" state=present'
ansible-doc service
ansible host2 -m service -a 'name=httpd state=started'
ansible host2 -m service -a 'name=httpd state=started enabled=yes'
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'
ansible-doc file
ansible host1 -m file -a 'path=/tmp/1.txt mode=777 state=touch'
ansible host1 -m file -a 'path=/tmp/2.txt mode=777 state=directory'
ansible-doc setup
ansible host1 -m setup
ansible host1 -m setup -a 'filter=ansible_all_ipv4_addresses'
fruits:
- Apple
- Orange
- Strawberry
- Mango
martin:
name: Martin D'vloper
job: Developer
skill: Elite
ansible all -m yum -a 'name=httpd state=removed' -o
1.yum -y install httpd
2.mkdir apache
3.cd apache
4.cp -rf /etc/httpd/conf/httpd.conf .
5.vim httpd.conf 将端口改为8080,用作推送
vim apache.yaml
- hosts: host2
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-hosts 列出主机
ansible-playbook apache.yaml
1.如果配置文件发生变化。
Listen 9000
2.再次执行,命令执行成功,但是配置未生效,所以要增加处理程序,设置触发器。
3.修改配置文件
vim apache.yaml
- name: copy apache conf
copy: src=./httpd.conf dest=/etc/httpd/conf/httpd.conf
notify: restart apache service
handlers:
- name: restart apache service
service: name=httpd state=restarted
再次执行,配置生效,触发成功
(1)特点:易读,代码可重用,层次清晰。
(2)目标:通过role远程部署nginx并部署。
nginx 角色名、files普通文件、handlers 触发器程序、tasks 主任务、templates 金甲模式(有变量的文件,系统自带变量和自定义变量)、vars 自定义变量
1.自建
(1)mkdir roles/nginx/{files,handlers,tasks,templates,vars} -p
(2)touch roles/site.yaml roles/nginx/{handlers,tasks,vars}/main.yaml
(3)echo 1234 > /roles/nginx/files/index.html
2.命令
ansible-galaxy init nginx
yum -y install nginx && cp /etc/nginx/nginx.conf roles/nginx/templates.nginx.conf.j2
vim roles/nginx/tasks/main.yaml
---
- name: install epel-release packages
yum: name=epel-release state=latest
- name: install nginx package
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_process {{ ansible_processer_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: host1
roles:
- nginx #文件夹的名称
cd roles
ansible-playbook site.yaml --syntax-check #测试语法
ansible-playbook site.yaml #实施剧本
验证host1,网页输入IP后显示1234,成功