电商网站建设规划书,中小企业网站设计与开发目的,崂山区建设局网站,链接生成一、abstract简介 ansible是新出现的自动化运维工具#xff0c;基于Python开发#xff0c;集合了众多运维工具(puppet、cfengine、chef、func、fabric) 的优点#xff0c;实现了批量系统配置、批量程序部署、批量运行命令等功能.无客户端。我们要学一些Ansible的安装和一些基…一、abstract简介 ansible是新出现的自动化运维工具基于Python开发集合了众多运维工具(puppet、cfengine、chef、func、fabric) 的优点实现了批量系统配置、批量程序部署、批量运行命令等功能.无客户端。我们要学一些Ansible的安装和一些基本概念,然后我们会开始研究一些真正有意思的东西 - plavbook,配置管理,部署以及语法编排.我们将会学习如何使用/usr/bin/ansible执行ad-hoc并行命令,我们还会学习ansible的核心有什么样的模块可供使用.当然以后你也可以写你自己的模块。 二、install部署
1. dns resolve 环境 ansible服务器 192.168.64.2 ansible客户机 192.168.64.3 192.168.64.4 192.168.64.5 ansible服务器 域名解析 vim /etc/hosts 192.168.64.2 ansible 192.168.64.3 host1 192.168.64.4 host2 192.168.64.5 host3 ansible客户机 无需配置 IP YUM源 2.install ansible yum install -y epel-release 可配置阿里的 yum install -y ansible 检测是否安装成功 rpm -ql ansible 列出ansible所有文件 rpm -qc ansible 查看配置文件 ansible --help 查看ansible帮助 ansible-doc -l 看所有模块 ansible-doc -s yum 看yum模块 三、ssh-key可选
1.免密码ssh-key的方式
2.ssh-keygen的方式
3.ssh-copy-id IP地址 推送密钥
四、ansible基础
1.定义主机清单 vim /etc/ansible/hosts host1 host2 host3 没有host4注意 2.测试连通性 ansible localhost -m ping -m指定模块ping只是其中的一个模块还有shellyum等 3.简洁输出 ansible localhost -m ping -o 4.know_hosts ansible host1 -m ping 成功因为有免密 ansible host2 -m ping 失败没有免密 ansible host2 -m ping -u root -k -o 增加用户名选项和密码选项 去掉yes/no选项的询问 vim /etc/ssh/ssh_config StrictHostKeyChecking no systemctl restart sshd 5.错误示范 ansible host4 -m ping -u root -k -o 失败主机清单未配置主机 6.请注意ping和ssh ping ICMP网络消息管理协议 关闭host2主机的sshd进程进行ping连通性测试 再使用ansible对host2进行连通性测试却是失败的 结论ansible的ping是餐车ssh程序是否连接不是icmp协议 ansible host2 -m ping -u root -k -o 五、inventory主机清单
含义清查存货清单财产目录主机清单
1.增加主机组
官方链接 vim /etc/ansible/hosts [webserver] host1 host2 host3 host4 ansible webserver -m ping -o 验证组被调用 2.增加用户名密码 vim /etc/ansible/hosts #为了方便管理都是添加免密 [webserver] host1 ansible_ssh_userroot ansible_ssh_pass666666 host2 ansible_ssh_userroot ansible_ssh_pass666666 host3 ansible_ssh_userroot ansible_ssh_pass666666 host4 ansible_ssh_userroot ansible_ssh_pass666666 或 host[1:4] ansible_ssh_userroot ansible_ssh_pass666666 ansible webserver -m ping -o 验证 思考主机和用户名和密码不同怎么设置 分别设置
3.增加端口
将host4的sshd端口修改为2222 vim /etc/ssh/sshd_config Port 2222 (1024-65535)最好 systemctl restart sshd 登录的话需要加上端口 ssh root192.168.64.3 -p 2222 ansible webserver -m ping -o 验证失败因为端口已改 vim /etc/ansible/hosts [webserver] host[1:3] ansible_ssh_userroot ansible_ssh_pass666666 host4 ansible_ssh_userroot ansible_ssh_pass666666 ansible_ssh_port2222 liunx进行安全加固面试题
修改ssh默认端口号禁用超管修改yum源等等
4.组变量
ansible内部变量可以帮助我们简化主机清单的设置 vim /etc/ansible/hosts [webserver] host[1:3] host4 ansible_ssh_port2222 [webserver:vars] vars代表的是变量 ansible_ssh_userroot ansible_ssh_pass666666 常用变量 5.子分组
将不同的组进行组合 vim /etc/ansible/hosts [apache] host[1:2] [nginx] host[3:4] [webserver:children] children 子分组 apache nginx [webserver:vars] vars代表的是变量 ansible_ssh_userroot ansible_ssh_pass666666 6.自定义主机列表 vim hostlist [dockers] host[1:2] [dockers:vars] ansible_ssh_userroot ansible_ssh_pass666666 ansible -i hostlist dockers -m ping -o -i连接外部主机清单 六、Ad-Hoc点对点模式
简介临时的在ansible中是指需要快速执行的单条命令并且不需要保存的命令对于复杂的命令则为playbook
1.shell模块 ansible-doc shell ansible webserver -m shell -a hostname -o 调用hostname -o间接执行 ansible webserver -m shell -a ‘hostname’ -o -f 2 指定线程数 ansible webserver -m shell -a yum -y install httpd -o 安装程序 ansible webserver -m shell -a uptime 查询系统负载 2.复制模块 帮助 ansible-doc copy 案例 快速拷贝东西到别的机器上机器多人少 ansible webserver -m copy -a src/etc/hosts dest/tmp/2.txt ownerroot groupbin mode777 srcsource 资源 destdestination目标地 owner属主 group属组 mode权限 ansible webserver -m copy -a src/etc/hosts dest/tmp/2.txt ownerroot groupbin mode777 backupyes backup如果内容不同不覆盖重新建一个文件 3.用户模块 帮助 ansible-doc user 3.1创建用户 ansible webserver -m user -a nameqianfeng statepresent 创建present 3.2修改密码
1.生成加密密码 echo 512050951 | openssl passwd -1 -stdin 生成加密密码值 加密 密码 密码类型 标准输入输出不等用户回话 2.修改密码 ansible webserver -m user -a nameqianfeng password把上个命令生成密码放这 3.3修改shell ansible webserver -m user -a nameqianfeng shell/sbin/nologin appendyes 追加修改的意思 3.4删除用户 ansible webserver -m user -a nameqianfeng stateabsent 删除absent 4.软件包管理 帮助 ansible-doc yum ansible host1 -m yum -a name* statelatest 升级所有包 ansible host2 -m yum -a namehttp statelatest 安装apache 5.服务模块 ansible-doc service 帮助 ansible host2 -m service -a namehttpd statestarted 开启http服务 ansible host2 -m service -a namehttpd statestarted enabledyes 开机自启 ansible host2 -m service -a namehttpd statestopped 停止服务 ansible host2 -m service -a namehttpd staterestarted 重启 ansible host2 -m service -a namehttpd statestarted enabledno 关闭开机自启 6.文件模块 帮助 ansible-doc file ansible host1 -m file -a path/tmp/88.txt mode777 statetouch 创建文件 ansible host1 -m file -a path/tmp/99 mode777 statedirectory 创建目录 7.收集模块 帮助 ansible-doc setup ansible host3 -m setup ansible host3 -m setup -a filteransible_all_ipv4_addresses filter过滤 七、YMAL-YMAL Aintt Markup Language 非标记语言
语法 列表 fruits - Apple - Orange - pear 字典 martin name:Martin Devoper job:Developer skill:Elite 示例需求通过YAML编写一个简单的剧本完成web的配置部署启动的全过程 ansible服务器 1.准备工作 ansible all -m yum -a namehttpd stateremoved -o 卸载以免报错看不懂 yum install -y httpd 准备配置文件 mkdir apache cd apache cp -rf /etc/httpd/conf/httpd.conf . 考到当前目录下 grep ^Listen httpd.conf Listen 8080 2.编写剧本-----对齐很重要 vim apache.ymal - hosts: host2 减号后面一定要有空格冒号后边有空格 tasks: tasks任务只有一个的话在tasks后边写 - name: install apache packages name描述信息 yum: namehttpd statepresent - name: copy apache conf copy: src./httpd.conf dest/etc/httpd/conf/httpd.conf - name: ensure apache is running service: namehttpd statestarted enabledyes 3.测试 ansible-playbook apache.yaml --syntax-check 剧本apache.yml 语法测试校验 ansible-playbook apache.yaml --list -tasks 列出来都有什么任务 ansible-playbook apache.yaml --list -hosts 列出主机 ansible-playbook apache.yaml 执行剧本 检查一下 192.168.64.3:8080 4.handlers 如果配置发生变化httpd.connf系统需要重启 八、Role 角色扮演
简介roles是在ansible中playbooks的目录组织结构将代码或文件进行模块化成为roles的文件目录组织结构易读代码可重用层次清晰
目标通过role远程部署ngnix并配置
1.目录结构 tree roles/ 准备目录结构 mkdir roles/nginx/{files,handlers,tasks,templates,vars} -p touch roles/site.yaml roles/nginx/{handlers,tasks,vars}/main.yaml echo 1234roles/nginx/files/index.html yum install -y nginx cp /etc/nginx/nginx.conf roles/ngnix/templates/nginx.conf.j2 2.编写任务 vim roles/nginx/tasks/main.yaml --- - name: install epel-release package yum: nameepel-release statelatest - name: install nginx package yum: namenginx statelatest - name: copy index.html copy: srcindex.html dest/usr/share/nginx/html/index.html - name:copy nginx.conf template template: srcnginx.conf.j2 dest/etc/nginx/nginx.conf 看做copy notify: restart nginx - name: make sure nginx service running service: namenginx statstarted enabledyes 3.准备配置文件
4.编写变量
5.编写处理程序
6.编写剧本
7.实施
九、Homework