linux建设门户网站,技术先进的网站设计制作,拨号地址怎么做网站,黔东南网站建设DevOps: 官网#xff1a;https://docs.ansible.com
自动化运维工具对比
C/S 架构:客户端/服务端
Puppet:基于 Ruby 开发,采用 C/S 架构,扩展性强,基于 SSL,远程命令执行相对较弱
SaltStack:基于 Python 开发,采用 C/S 架构,YAML使得配置脚本更简单.需要配置客户端及服务器…DevOps: 官网https://docs.ansible.com
自动化运维工具对比
C/S 架构:客户端/服务端
Puppet:基于 Ruby 开发,采用 C/S 架构,扩展性强,基于 SSL,远程命令执行相对较弱
SaltStack:基于 Python 开发,采用 C/S 架构,YAML使得配置脚本更简单.需要配置客户端及服务器端每台被控制节点需要安装agent
Ansible:基于 Python开发,分布式,无需客户端,轻量级,配置语法使用YAML语言,更强的远程命令执行操作 优点 Ansible简介是什么、做什么、特点
ansible是新出现的自动化运维工具基于Python开发分布式,无需客户端,轻量级实现了批量系统配置、批量程序部署、批量运行命令等功能ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架 一、安装
245ansible
vim /etc/hosts (做解析)
IP名字
配置ssh公钥认证控制节点需要发送ssh公钥给所有非被控制节点(做免密)
ssh-keygen yum list | grep ansible //有ansible 仓库
yum install -y ansible //下载ansible
ansible --version //查看版本、配置文件、python版本 ansible --help //帮助手册
二、主机清单inventory
vim /etc/ansible/hosts (要管理谁就写入谁)
web-1 //为主机
[db] 为主机组 查看组内主机列表
语法ansible 组名 --list-hosts
ansible web --list-hosts //查看组里的主机 [rootansible-server ~]# ansible -i /opt/hostlist all -m ping -o -i:指定清单文件
-m:调用模块
all:所有组
-o:改变输出格式 三点对点Ad-Hoc
ansible-doc -l //列出所有模块
ansible-doc -s yum //yum的使用方法 用户管理模块user
添加用户并设置密码
[rootansible-server ~]# ansible webservers1 -m user -a nameliudehua passwordecho 1234 | openssl passwd -1 -stdin -o name #如指定的用户名要安装的软件 -1 MD5加密算法
删除用户
[rootansible-server ~]# ansible webservers1 -m user -a nameliudehua stateabsent -o adsent #删除用户但是不会删除家目录 组管理模块group
gid为组设置的可选GID name要管理的组的名称 state该组是否应该存在于远程主机上absent不在/默认是present在 system如果是表示创建的组是系统组默认时no
[rootansible-server ~]# ansible all -m group -a namesomegroup statepresent
[rootansible-server ~]# ansible all -m group -a namesomegroup stateabsent //删除组 软件包管理模块yum
config_fileyum的配置文件 disable_gpg_check关闭gpg_check disablerepo不启用某个源 enablerepo启用某个源 name要进行操作的软件包的名字也可以传递一个url或者一个本地的rpm包的路径 state状态presentabsentlatest
[rootansible-server ~]# ansible webservers1 -m yum -a namehttpd statelatest -ostate #状态是什么干什么 stateabsent 用于remove安装包 statelatest 表示最新的 stateremoved 表示卸载
卸载软件
[rootansible-server ~]# ansible webservers1 -m yum -a namehttpd stateremoved -o 服务管理模块service
[rootansible-server ~]# ansible webservers1 -m service -a namehttpd statestarted #启动 [rootansible-server ~]# ansible webservers1 -m service -a namehttpd statestopped #停止 [rootansible-server ~]# ansible webservers1 -m service -a namehttpd staterestarted #重启 [rootansible-server ~]# ansible webservers1 -m service -a namehttpd statestarted enabledyes #开机启动 [rootansible-server ~]# ansible webservers1 -m service -a namehttpd statestarted enabledno #开机关闭 文件模块file
[rootansible-server ~]# ansible webservers1 -m file -a path/tmp/88.txt mode777 statetouch #创建一个文件 [rootansible-server ~]# ansible webservers1 -m file -a path/tmp/99 mode777 statedirectory #创建一个目录 收集信息模块setup
[rootansible-server ~]# ansible web1 -m setup -a filteransible_all_ipv4_addresses
#只查询ipv4的地址 filter:过滤
[rootansible-server ~]# ansible web1 -m setup -a filteransible_*_mb #内存的信息 [rootansible-server ~]# ansible -i /home/ansible/hostlist web -m setup -a filteransible_processor_cores #磁盘的信息 [rootansible-server ~]# ansible all -m setup --tree /tmp/facts 文件复制模块copy
[rootansible-server ~]# ansible test -m copy -a src/srv/myfiles/foo.conf dest/etc/foo.conf ownerfoo groupfoo mode0644 [rootansible-server ~]# ansible test -m copy -a src/mine/ntp.conf dest/etc/ntp.conf ownerroot grouproot mode644 backupyes 计划任务模块cron 获取每台主机的IP地址
[rootxingdian ~]# ansible -i /home/ansible/hostlist web -m shell -a ip a | grep eth0| awk NR2{print $2} -o a.txt cat a.txt |awk {print $9,$NF}
shell、command后跟在终端敲的命令
一个典型的例子就是 shell 和 command 模块. 这两个模块在很多情况下都能完成同样的工作 以下是两个模块之前的区别 command 模块命令将不会使用 shell 执行. 因此, 像 $HOME 这样的变量是不可用的。还有像 |, 都将不可用 shell 模块通过shell程序执行 默认是/bin/sh, , , |, ;, 可用 获取每台主机的内存
[rootxingdian ~]# ansible -i /home/ansible/hostlist web -m shell -a free -m | awk NR2 b.txt -o cat b.txt | awk {print $10} 四剧本Playbook 案列一 touch file.yaml vim file.yaml 检查语法错误 执行 ansible-playbook file.yaml
案列二 handlers由特定条件触发的Tasks 安装ftp vim ftp.yaml ansible-playbook --syntax-check ftp.yaml ansible-playbook ftp.yaml 删除ftp 案列三 [rootansible-server ansible]# vim /home/ansible/yum.yml --- - hosts: web user: root tasks: - name: install nginx yum: namenginx statelatest notify: diandian handlers: - name: diandian service: namenginx statestarted
ansible-playbook --syntax-check /home/ansible/yum.yml
ansible-playbook /home/ansible/yum.yml 案例四
循环迭代需要重复执行的任务
对迭代项的引用固定变量名为”item”使用with_items属性给定要迭代的元素
元素1.列表 2.字符串 3.字典
基于字符串列表元素实战
vim php.yaml
- hosts: db user: root tasks: - name: install packages yum: name{{ item }} statelatest #相当于for循环里面的i with_items: #取值 。但是不支持通配符 - httpd - php - php-mysql - php-mbstring - php-gd
验证 vim user.yaml 案列四
vim tags.yaml 上传植物僵尸
解压 unzip
vim apache.yaml ansible-playbook --syntax-check apache.yaml
ansible-playbook apache.yaml
apache 虚拟主机配置文件 vi jspvz.conf