ansible是自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,
实现了批量系统配置、批量程序部署、批量运行命令等功能。无客户端。
playbook,配置管理,部署以及语法编排.如何使用/usr/bin/ansible执行ad-hoc并行命令,
ansible的核心有什么样的模块可供使用.当然以后你也可以写你自己的模块.
ansible服务器
域名解析
vim /etc/hosts
192.168.184.165 ansible
192.168.184.166 host1
192.168.184.167 host2
yum install -y ansible
检测部署是否完成 rpm -qc ansible
免密码ssh-key的方式。
ssh-keygen
ssh-copy-id IP地址 推送公钥
登录:
增加用户名选项,增加密码选项
ansible host2 -m ping -u root -k -o
去掉(yes/no)的询问
vim /etc/ssh/ssh_config
StrictHostKeyChecking no
systemctl restart sshd
定义主机清单:
vim /etc/ansible/host
host1 ansible_ssh_user='root' ansible_ssh_pass='666666' ansible_ssh_port='2222' 可以单独写一个主机并添加端口。
[webserver] 使用主机组方式
host[1:2] 使用正则表达式
host1
host2 单独写1-2
[webserver:vars] 组变量
ansible_ssh_user='root'
ansible_ssh_pass='666666'
[apache]
host1
[nginx]
host2
[webserver:children] 将不同的分组进行组合
apache
nginx
[webserver:vars]
ansible_ssh_user='root'
ansible_ssh_pass='666666'
临时的,在ansible中是指需要快速执行的单条命令,并且不需要保存的命令。对于复杂的命令则为 playbook。
常用模块
1.shell模块
帮助 ansible-doc shell
ansible webserver -m shell -a 'hostname' -o 获取主机名
ansible host2 -m shell -a 'yum -y install httpd' -o 部署apache
2.复制模块
帮助 ansible-doc copy
ansible webserver -m copy -a 'src=/etc/hosts dest=/tmp/2.txt owner=root group=bin mode=777'
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=qianfeng state=present'
修改密码
生成加密密码
echo '512050951' | openssl passwd -1 -stdin
生成加密密码值
$1$XVzsJMDr$5wI4oUaQ.emxap6s.N272.
修改密码
ansible webserver -m user -a 'name=qianfeng password="$1$XVzsJMDr$5wI4oUaQ.emxap6s.N272."'
修改shell ansible webserver -m user -a 'name=qianfeng shell=/sbin/nologin append=yes'
删除用户 ansible webserver -m user -a 'name=qianfeng state=absent'
4.软件包管理
帮助 ansible-doc yum
ansible host1 -m yum -a 'name="*" state=latest' 升级所有包
ansible host2 -m yum -a 'name="httpd" state=latest' 安装apache
5.服务模块
帮助 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' 开机禁止启动
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 setup
ansible host3 -m setup 查询所有信息
ansible host3 -m setup -a 'filter=ansible_all_ipv4_addresses' 查询特定信息
通过YAML编写一个简单的剧本,完成web的部署,配置,启动的全过程。
准备工作
yum install -y nginx 准备配置文件
mkdir nginx
cd nginx
cp -rf /usr/share/nginx/html/index.html .
vim /usr/share/nginx/html/index.html aaa 修改配置,用作推送
编写剧本
vim nginx.yaml
- hosts: webserver
tasks:
- name: install epel-release packge 下载yum源
yum: name=epel-release state=latest
- name: install nginx packge 下载nginx
yum: name=nginx state=latest
- name: copy index.html 拷贝文件
copy: src=./nginx.conf dest=/usr/share/nginx/html/index.html
notify: restart nginx service
- name: ensure nginx is running 启动nginx
service: name=nginx state=started enabled=yes
- handlers: 如果配置文件发生变化,则执行
- name: restart nginx service
service: name=nginx state=restarted
测试:
ansible-playbook apache.yaml --syntax-check 检验语法
ansible-playbook apache.yaml --list-tasks 列出任务
ansible-playbook apache.yaml --list-hosts 列出主机
ansible-playbook apache.yaml 执行
http://192.168.184.166/167 aaa
配置生效 需求完成。
简介:
roles则是在ansible中,playbooks的目录组织结构。
将代码或文件进行模块化,成为roles的文件目录组织结构,易读,代码可重用,层次清晰。
目标
通过role远程部署源码nginx并配置
目录结构
nginx 角色名
files 普通文件
handlers 触发器程序
tasks 主任务
templates 金甲模板(有变量的文件)
vars 自定义变量
准备目录结构
mkdir roles/nginx/{files,handlers,tasks,templates,vars} -p
touch roles/site.yaml roles/nginx/{handlers,tasks,vars}/main.yaml
echo 1234 > roles/nginx/files/index.html
准备源码包/服务包/html
cp nginx-1.18.0.tar.gz /root/roles/nginx/files
vim /root/roles/nginx/files/index.html
aaa
vim /root/roles/nginx/files/nginx.server
[Unit]
Description=nginx -high performance web server
Documentation=http://nginx.org/en/docs
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
WorkingDirectory=/usr/local/nginx
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
private Tmp=true
[Install]
WantedBy=multi-user.target
本机源码安装一下,找到配置文件然后放在templates里
cp /usr/local/nginx/conf/nginx.conf /root/roles/nginx/templates/nginx.conf.j2
编写任务
vim roles/nginx/tasks/main.yaml
---
- name: install nginx packge
yum: name=gcc,gcc-c++,pcre,pcre-devel,zlib,zlib-devel,openssl,openssl-devel state=latest
- name: copy nginx
copy: src=nginx-1.18.0.tar.gz dest=/root
- name: nginx
shell: "tar -xzf /root/nginx-1.18.0.tar.gz && cd /root/nginx-1.18.0/ && useradd -s /sbin/nologin nginx -M && mkdir -p /var/tmp/nginx/client/ && ./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre && make && make install"
- name: copy index.html
copy: src=index.html dest=/usr/local/nginx/html/index.html
- name: copy nginx.conf template
template: src=nginx.conf.j2 dest=/usr/local/nginx/conf/nginx.conf
notify: restart nginx
- name: copy systemctl
copy: src=nginx.service dest=/usr/lib/systemd/system/nginx.service
- name: chmod start
shell: "chmod +x /usr/lib/systemd/system/nginx.service && systemctl daemon-reload && systemctl enable nginx.service && systemctl start nginx"
3.准备配置文件
vim roles/nginx/templates/nginx.conf.j2
worker_processes {{ ansible_processor_cores }};调用内部已知变量
worker_connections {{ worker_connections }};自定义变量
4.编写变量
vim roles/nginx/vars/main.yaml
worker_connections: 65535
5.编写处理程序
vim roles/nginx/handlers/main.yaml
---
- name: restart nginx
shell: "systemctl stop nginx && systemctl start nginx"
6.编写剧本
vim roles/site.yaml
- hosts: webserver
roles:
- nginx
7.实施
cd roles
ansible-playbook site.yaml --syntax-check 测试
ansible-playbook site.yaml 实施剧本
验证webserver (http://192....)
ansible 源码部署nginx并设置服务,设置html,优化,完成。