前言

Ansible首次发布于2012年,是一款基于Python开发的自动化运维工具,核心是通过ssh将命令发送执行,它可以帮助管理员在多服务器上进行配置管理和部署。它的工作形式依托模块实现,自己没有批量部署的能力。真正具备批量部署的是ansible所运行的模块,ansible只是提供一种框架。

优点

  • 学习、配置及部署简单,学习速度快
  • 用户量庞大,经得起实际的是考验,自然,文档也会很多,降低学习难度
  • 模块繁多,可完成运维工程中绝大多数任务
  • 轻量级:无客户端程序,更新时,只需机器上进行一次更新
  • 定制化模块,更好的适配项目进度,加速工作效率
  • 多系统应用,不单单linux工作管理,还可以用在windo、交换机等
  • 配置文件中内置幂等性、并发度控制等功能,有效减少批量部署工作量
  • 集合了多种运维工具(puppet、cfengine、chef、func、fabric)优势的新兴高效运维工具

结构组成

  1. 链接插件connection plugins:负责和被监控端实现通讯;
  2. host inventory:主机(待执行任务)列表文件
  3. 模块:执行模块、管理模块、附属属性模块
  4. 插件完成记录日志邮件等功能
  5. playbook:剧本编排,实现多任务顺序执行

一、环境配置

# 配置ssh免密授权完成机器互通,形式公私钥

  • 假设从A(主)→→→连通→→→B(从)
# 服务器A上执行,假设B服务器的账户名、IP为userB、hostB
ssh-keygen -t rsa
ssh-copy-id userB@hostB

二、装包

当前主机A的linux版本是ubuntu

apt-get install ansible
# 查看安装的版本
ansible --version

三、配置主机清单inventory信息

  • ansible默认读取的配置文件为/etc/ansible/hosts,语法类型类INI形式
[servers]  # 定义ansible组
ansible_node1 ansible_host=121.47.45.83  # 配置从机域名及地址,此处变量为主机变量
[servers:vars]  # 组变量
ansible_ssh_user=zhangs  # ansible servers组机器被ssh时使用的账户
ansible_ssh_pass=zhangs@123  # ansible servers组机器被ssh时使用的密码
# 非root账户,提升至root需sudo,下面的参数就是提升权限所用,单词become换成sudo是一样的效果,可能版本升级,兼容原版本形式
ansible_become_pass=zhangs@123test_node 192.168.200.131 ansible_ssh_user=root ansible_ssh_pass='123456' # 配置域名+IP+user+pwd
# 定义组嵌套,可以实现组变量共享
[link:children]
servers
[link:vars]  # 组变量
server_name=www.example.com

四、基础模块的应用

ping yum copy fetch file template user group
service raw command shell script cron get_url synchronize
其它模块 附属属性

4.1、ping模块

  • 测试对从节点机器的连接是否可用
ansible all -m ping  # 对主机清单内的所有主机进行ping测试
ansible ansible_node1 -m ping  # 对主机ansible_node1进行ping测试

4.2、yum模块

  • 实现centos主机内的包安装
# 主机test_node安装pandas
ansible test_node -m yum -a 'name=pandas state=present'  # 可以不加state参数,默认值present
# 卸载pandas包
ansible test_node -m yum -a 'name=pandas state=absent'
参数名 是否必须 默认值 选项值 参数说明
conf_file no 设定远程yum执行时所依赖的yum配置文件
disable_gpg_check no no yes/no 在安装包前检查包,只会影响state参数为present或者latest的时候
list no 只能由ansible调用,不支持playbook
name yes 安装的包名,也能如此使用name=python=2.7安装python2.7
state no present present/latest/absent 用于描述安装包最终状态,present/latest用于安装包,absent用于remove安装包
update_cache no no yes/no 用于安装包前执行更新list,只会影响state参数为present/latest的时候

4.3、copy 模块

  • 复制(主机文件复制至从机)
# 拷贝字符至目的地
ansible ansible_node1 -m copy -a 'content="Hello World\n" dest=/home/zhangs/1.txt'
# 拷贝文件至目的地,并配置文件属性
ansible ansible_node1 -m copy -a 'src=monitor dest=/home/zhangs/monitor owner=root group=root mode=0644'
参数名 是否必须 默认值 选项 说明
src no 用于定位ansible执行的机器上的文件,须要绝对路径。若是拷贝的是文件夹,那么文件夹会总体拷贝,若是结尾是”/”,那么只有文件夹内的东西被考过去。一切的感受很像rsync
content no 用来替代src,用于将指定文件的内容,拷贝到远程文件内
dest yes 用于定位远程节点上的文件,须要绝对路径。若是src指向的是文件夹,这个参数也必须是指向文件夹
backup no no yes/no 备份远程节点上的原始文件,在拷贝以前。若是发生什么意外,原始文件还能使用
directory_mode no 这个参数只能用于拷贝文件夹时候,这个设定后,文件夹内新建的文件会被拷贝。而老旧的不会被拷贝
follow no no yes/no 当拷贝的文件夹内有link存在的时候,那么拷贝过去的也会有link
force no yes yes/no 默认为yes,会覆盖远程的内容不同的文件(可能文件名同样)。若是是no,就不会拷贝文件,若是远程有这个文件
group no 设定一个群组拥有拷贝到远程节点的文件权限
mode no 等同于chmod,参数能够为“u+rwx or u=rw,g=r,o=r”
owner no 设定一个用户拥有拷贝到远程节点的文件权限

4.4、fetch模块

  • 复制(从机文件复制至主机),与copy模块作用相反
# 拉取从机文件到本机
ansible ansible_node1 -m fetch -a "src=/home/zhangs/123/1.txt dest=/usr/src"
参数 必填 默认值 选项 说明
dest yes 用来存放文件的目录,例如存放目录为backup,源文件名称为/etc/profile在主机pythonserver中,那么保存为/backup/pythonserver/etc/profile
fail_on_missing no no yes/no 当源文件不存在的时候,标识为失败
flat no 是否允许覆盖默认行为从hostname/path到/file的,若是dest以/结尾,它将使用源文件的基础名称
src yes 在远程拉取的文件,而且必须是一个file,不能是目录
validate_checksum no yes yes/no 当文件fetch以后进行md5检查

4.5、file模块

  • 文件目录的创建修改删除及属性变更,包含多个模块功能,如copy,assemble和template
# 创建指定目录文件并设定用户及用户组权限
ansible ansible_node1 -m file -a "path=/home/zhangs/2.txt owner=zhangs group=zhangs mode=0644"
# 建立软链接
ansible ansible_node1 -m file -a "src=/home/zhangs dest=/home/zhangs_link owner=zhangs state=link"
参数 必填 默认 选项 说明
follow no no yes/no 这个标识说明这是系统连接文件,若是存在,应该遵循
force no no yes/no 强制建立连接在两种状况下:源文件不存在(过会会存在);目标存在可是是文件(建立连接文件替代)
group no 文件所属用户组
mode no 文件所属权限
owner no 文件所属用户
path yes 要控制文件的路径
recurse no no yes/no 当文件为目录时,是否进行递归设置权限
src no 文件连接路径,只有状态为link的时候,才会设置,能够是绝对相对不存在的路径
state no file file/directory/link/touch/absent 若是是目录不存在,那么会建立目录;若是是文件不存在,那么会建立文件;若是是link,那么软连接会被建立或者修改;若是是absent,那么目录下的全部文件都会被删除,若是是touch,会建立不存在的目录和文件

4.6、template模块

  • 生成一个模板,将主机文件传输从机(类似与copy模块)
ansible all -m template -a 'src=files/CentOS-Base.repo dest=/etc/yum.repos.d/CentOS-Base.repo owner=root group=root mode=0644'

4.7、user模块

  • 实现账户管理
# 创建账户
ansible ansible_node1 -m user -a 'name=lis uid=666 system=yes create_home=no shell=/sbin/nologin state=present'
# 查看账户
id lis
# 修改账户
ansible ansible_node1 -m user -a 'name=lis uid=888'
# 删除账户
ansible ansible_node1 -m user -a 'name=lis state=absent remove=yes'
参数名 是否必须 默认值 选项 参数说明
home 指定用户的家目录,须要与createhome配合使
groups 指定用户的属组
uid 指定用的uid
password 指定用户的密码
name 指定用户名
createhome 是否建立家目录 yes
system 是否为系统用户
remove 当state=absent时,remove=yes则表示连同家目录一块儿删除,等价于userdel -r
state 是建立仍是删除
shell 指定用户的shell环境

4.8、group模块

  • 实现组管理
# 创建组
ansible ansible_node1 -m group -a 'name=testG gid=666 state=present'
# 查看组
grep testG /etc/group
# 删除组
ansible ansible_node1 -m group -a 'name=testG gid=666 state=absent'

4.9、service模块

  • 对linux机器内服务进行管理
# 启动nginx服务
ansible ansible_node1 -m service -a 'name=nginx state=started'
# 查看nginx服务状态
ansible ansible_node1 -m shell -a 'systemctl is-active nginx'
# 查看nginx服务是否开启自启
ansible ansible_node1 -m shell -a 'systemctl is-enabled nginx'
# 设置nginx服务开机自启动
ansible ansible_node1 -m service -a 'name=nginx enabled=yes state=restarted'
# 停止nginx服务
ansible ansible_node1 -m service -a 'name=nginx state=stopped'
参数名 是否必须 默认值 选项 参数说明
enabled no yes/no 启动os后启动对应service的选项。使用service模块的时候,enabled和state至少要有一个被定义
name yes 须要进行操做的service名字
state no stared/stoped/restarted/reloaded service最终操做后的状态

4.10、raw模块

  • 实现linux执行命令,其支持管道符与重定向
  • 老版本python,需要用到raw,又或者是客户端是路由器,因为没有安装python模块,那就需要使用raw模块
  • 建议使用shell和command模块
# 重定向写入数据
ansible ansible_node1 -m raw -a 'echo "hello world" > /home/zhangs/test.txt'
# 管道符应用
ansible ansible_node1 -m raw -a 'cat /home/zhangs/test.txt|grep -Eo hello'
# 匹配正则文件
ansible ansible_node1 -m raw -a 'ls -lrth /home/zhangs/te*'
# 查看从机host信息
ansible ansible_node1 -m raw -a 'hostname|tree'

4.11、command模块(系统默认)

  • 实现linux执行命令,命令执行在ssh进程中,使用shell命令执行的会失败
  • 不支持管道符和重定向,包含"><&|"都会报错
  • 不能解析系统变量
# 查看日期
ansible ansible_node1 -m command -a 'date'
ansible ansible_node1 -a 'date'  # 因默认故省略
# 关机
ansible ansible_node1 -a '/sbin/shutdown -t now'
# 创建文件
ansible ansible_node1 -a 'touch /home/zhangs/demo.txt warn=false'
# 执行sh脚本,目录/usr/src/file不存在执行,否则跳过
ansible ansible_node1 -a '/usr/bin/scp_file.sh ag1 ag2 creates=/usr/src/file'
# 查看目录
ansible ansible_node1 -a 'ls /home/zhangs'
ansible ansible_node1 -a 'tree /home/zhangs'

4.12、shell模块

  • 实现linux执行命令,调用的是/bin/sh指令(意味着/bin/sh这个文件必须存在),基本涵盖所有命令
  • 支持管道和重定向
  • 不支持交互式命令,如vim top等
# 创建目录
ansible ansible_node1 -m shell -a "mkdir /home/zhangs/test"


ansible检测到语法使用某模块,而不是shell时,会报[WARNING], 可通过ansible.cfg中配置command_warnings=False或命令后面追加warn=false进行屏蔽

# 创建目录
ansible ansible_node1 -m shell -a "mkdir /home/zhangs/test1 warn=false"
# 查看nginx的进程信息
ansible ansible_node1 -m shell -a 'ps -ef|grep nginx'
# 输出到文件
ansible ansible_node1 -m shell -a 'echo "Hello World">>2.txt'
# 查看文件
ansible ansible_node1 -m shell -a 'cat /home/zhangs/2.txt'
# 执行shell脚本
ansible ansible_node1 -m shell -a '/bin/bash /scripts/test.sh'
# cd目录编译并安装
ansible ansible_node1 -m shell -a "./configure && make && make install" chdir=/xxx/yyy/
# 查看HOSTNAME内置变量值
ansible ansible_node1 -m shell -a 'echo ${HOSTNAME}'

4.13、script模块

  • 实现linux执行命令,从机上执行主机上的脚本(不仅限于sh类型脚本,其它类型须指明解释器)
# 执行主机sh脚本并将结果输出到从机文件
ansible ansible_node1 -m script -a "/home/zhuz/mk.sh & > /tmp/log"
# 查看从机结果
ansible ansible_node1 -m shell -a 'cat /tmp/log'

4.14、cron模块

  • cron模块用于管理计划任务
ansible ansible_node1 -m cron -a 'name="a job for reboot" special_time=reboot job="/some/job.sh"'ansible ansible_node1 -m cron -a 'name="yum autoupdate" weekday="2" minute=0 hour=12 user="rootansible ansible_node1 -m cron  -a 'backup="True" name="test" minute="0" hour="5,2" job="ls -alh > /dev/null"'ansilbe ansible_node1 -m cron -a 'cron_file=ansible_yum-autoupdate state=absent'
参数名 是否必须 默认值 选项 参数说明
backup 对远程主机上的原任务计划内容修改以前作备份
cron_file 若是指定该选项,则用该文件替换远程主机上的cron.d目录下的用户的任务计划
day 日(1-31,/2,……)
hour 小时(0-23,/2,……)
minute 分钟(0-59,/2,……)
month 月(1-12,/2,……)
weekday 周(0-7,*,……)
job 要执行的任务,依赖于state=present
name 该任务的描述
special_time 指定何时执行,参数:reboot,yearly,annually,monthly,weekly,daily,hourly
state 确认该任务计划是建立仍是删除
user 以哪一个用户的身份执行

4.15、get_url模块

  • 用于从http、ftp、https服务器上下载文件(相似于wget)
# 从web拉取文件到从机
ansible ansible_node1 -m get_url -a 'url= http://10.1.1.11/favicon.ico dest=/home/zhangs'
参数名 是否必须 默认值 选项 参数说明
sha256sum 下载完成后进行sha256 check
timeout 下载超时时间,默认10s
url 下载的URL
url_password、url_username 主要用于须要用户名密码进行验证的状况
use_proxy 是否使用代理,代理需事先在环境变动中定义

4.16、synchronize模块

  • 使用rsync同步文件
# 将主机file目录推送到从机的/home/zhangs目录下
ansible ansible_node1 -m synchronize -a 'src=file dest=/home/zhangs/ compress=yes'  # --exclude=.Git忽略同步.git结尾的文件
#
参数名 是否必须 默认值 选项 参数说明
archive 归档,至关于同时开启recursive(递归)、links、perms、times、owner、group、-D选项都为yes ,默认该项为开启
checksum 跳过检测sum值,默认关闭
compress 是否开启压缩,默认开启
copy_links 复制连接文件,默认为no ,注意后面还有一个links参数
delete 同步后保持主从文件一致,以发送方的文件作为参考,默认no
dest 目录路径
dest_port dest_port:默认目录主机上的端口 ,默认是22,走的ssh协议
dirs 传速目录不进行递归,默认为no,即进行目录递归
rsync_opts rsync参数部分
set_remote_user 主要用于/etc/ansible/hosts中定义或默认使用的用户与rsync使用的用户不一样的状况
mode push或pull 模块,push模式的话,通常用于从本机向远程主机上传文件,pull 模式用于从远程主机上取文件,默认push

4.17、其它模块

  • mount模块:配置挂载点
  • unarchive模块:解压文件模块
  • apt模块:ubuntu包安装
  • lineinfile模块:实现文件内容正则替换==Ctrl+R
  • systemd模块:服务管理模块
  • docker_image模块:docker镜像处理
  • docker_container模块:docker容器处理
  • docker_compose模块: docker服务编排管理
  • vault模块:加密,防止pwd明文存储到文件,可用此加密存储

4.18、附属属性

  • chdir=/xxx指定路径ansible test_node1 -m shell -a 'touch a.txt chdir=/root/test'
  • creates=1.txt判断指定文件是否存在,如果不存在,就执行命令,如果存在,命令将不再运行ansible test_node1 -m shell -a 'touch 1.txt chdir=/root/test creates=1.txt'
  • removes=1.txt判断指定文件是否存在,如果存在,执行后面的操作ansible test_node1 -m shell -a 'rm -f /root/test/1.txt removes=/root/test/1.txt'

五、核心模块的应用

如《第四章》不难看出,命令的执行时独立的,有没有将多个命令编排在一起的呢?
playbook可以定义为一些列任务的配置集合。也称为剧本,每一个playbook都包含一系列的任务,每个任务在Ansible中称为play。Playbook的写法采用缩排的方式呈现,结构通过缩进来表示,连续的项目通过减号 “-”来表示。一个name仅代表一个play,每个play下只能跟一个命令(如command),当存在多个命令时只会执行最后一个命令

5.1、playbook基本demo

  • yaml文件遵循2个空格缩进,编辑ansible_demo.yml
- name: Ansible Project Demo  # 剧本名,我觉得叫项目名更合适,随意起hosts: ansible_node1  # 被控端域名或IP,在inventory中需要声明tasks:- name: touch myfile  # 任务的名称,随意起- file: path=/home/zhangs/demo.txt state=touch mode=0755  # 利用file模块实现创建文件

小节拓展

如果不用file文件去创建文件,还有别的方式吗?command,但那需要两次command命令才能实现相同的功能,如下

- name: Ansible Project Demo  # 剧本名,我觉得叫项目名更合适,随意起hosts: ansible_node1  # 被控端域名或IP,在inventory中需要声明tasks:- name: create myfilecommand: touch /home/zhangs/demo.txt- name: change the modcommand: chmod 755 /home/zhangs/demo.txt
  • 选用合适的模块会事半功倍

5.2、校验配置yml文件合法性

ansible-playbook -i inventory --syntax-check ansible_demo.yml
如果ansible配置文件位置,选择的是系统默认,那么inventory=/etc/ansible/hosts,否则填写自定义位置

5.3、任务执行

ansible-playbook -i /etc/ansible/hosts ansible_demo.yml
①配置文件路径如果是默认,可不加参数-i
②如果想看过程输出,可以追加参数-verbose
③支持ansible从机正则匹配规则,如果从机数量过多,可用--list-host 来查询匹配到的主机汇总

5.4、提权become

- name: Ansible Project Demo  # 剧本名,我觉得叫项目名更合适,随意起hosts: ansible_node1  # 被控端域名或IP,在inventory中需要声明tasks:- name: create myfilecommand: touch /home/zhangs/demo.txt- name: change the modcommand: chmod 755 /home/zhangs/demo.txt- name: latest httpd version installedbecome: yes  # 安装包需要提权以便系统目录文件的写入,sudo升权密码已在Inventory标注yum:name: httpdstate: latest
  • 其它提权形式
  • ansible的become就是提权的意思。ansible.cfg中become通常设置为false,具体的task操作中再指定become: true。这样可以确保使用最小权限进行操作,以防误操作
  • 提权操作时ssh登陆帐号还是非root帐号(该帐号有sudo权限),特别注意become_user不能写原ssh登陆的非root用户,一定要写root,或者不写默认为root。这样才能执行成功
  • 如果主机清单hosts中没有指定become的密码,那么执行ansible-playbook install.yml加-K参数,手动输入ssh用户的sudo密码(不是root密码)
  • ssh登陆时可以使用密码,也可以使用基于密钥的登陆(免密)。但是执行特权命令时必须要提权,即执行命令时加入-K参数,输入ssh用户的提权密码

5.5、debug调试

5.6、playbook辅助参数

# 自定义参数引用时,可以用关键字vars。在剧本级别中定义变量时,与hosts同级;在任务级别中定义,与tasks或command同级皆可
vars:custom_item: custom_value  # 引用形式双大括号为{{custom_item}},如在inventory文件中有过声明,可直接引用# 自定义yml文件引用,可以用关键字include,与task同级
include: other.yml  # 主要用于不同项目间的批量任务处理,每个项目都有自己专属的yaml文件# with_item应用,引用形式为{{item}},实现多个包的安装,多变量信息读取执行,与command、file等模块同级
with_items:-'docker-ce'-'nginx'-'git'# cd目录执行及指定解释器,与command、file等模块同级
args:chdir: /xxx/yyyexecutable: /bin/bash# 返回命令(shell、command等)执行的结果, 与command、file等模块同级
register: result# 根据命令执行结果决定任务执行情况,与command、file等模块同级
when: result|success           # 如果上面的result为success,则该任务执行

5.7、Handler

章节《5.1》中为playbook剧本任务的一种最常见形式task,下面我们需要了解另一种形式,也就是handler,有没有想到厨师这个职业,是的!就是炒菜,意味着只有买了原料之后才能炒菜,假设买每一种菜都当作一个任务,所有任务完成之后才能执行最终要做的炒菜任务。。。话不多说,为什么要有这么个东西?再举个简单例子"老师准备上课,但是她的课本进度适中,她考虑加快进度迎头赶上,进入教室例行学生的考验,哈哈,叫到了张三、李四、王五、赵六,回答的一塌糊涂,老师很生气,于是将课程加快这个想法放弃,发放试卷加深学生对课本知识的汲取,以防学生们考得太差。。真难啊,老师心想,走出了教室,不久下课铃响了。。。"在老师考校几个学生都分别当作一个任务,当所有任务失败后,讲课的任务被迫放弃,反言之,只有所有任务成功才能继续学习新内容。我想你大概理解了这段话的意思!所有任务完成→→最终任务

  • 执行单个handler任务样例
- name: Ansible Project Demo  # 剧本名,我觉得叫项目名更合适,随意起hosts: ansible_node1  # 被控端域名或IP,在inventory中需要声明tasks:- name: create myfilecommand: touch /home/zhangs/demo.txt- name: change the modcommand: chmod 755 /home/zhangs/demo.txtnotify: rm myfile  # 任务完成执行handler任务handlers:- name: rm myfilecommand: rm /home/zhangs/demo.txt

上面的例子只是执行了一个handler,有没有方法执行多个handler?这就需要一个另一个关键字listen,你可以将之理解成组名

  • 执行多个handler任务样例
- name: Ansible Project Demo  # 剧本名,我觉得叫项目名更合适,随意起hosts: ansible_node1  # 被控端域名或IP,在inventory中需要声明tasks:- name: create myfilecommand: touch /home/zhangs/demo.txt- name: change the modcommand: chmod 755 /home/zhangs/demo.txtnotify: handler group  # 任务完成执行handler任务组handlers:- name: rm myfilelisten: handler groupcommand: rm /home/zhangs/demo.txt- name: create myfile againlisten: handler groupcommand: touch /home/zhangs/demo.txt

在playbook中,只要有task执行失败,则playbook终止,即使是与handler关联的task在失败的task之前运行成功了,handler也不会被执行,那么如何保证多任务执行时handler任务不会受其它任务影响?关键字force_handlers派上用场

  • 任务互不干扰
 - name: Ansible Project Demo  # 剧本名,我觉得叫项目名更合适,随意起hosts: ansible_node1  # 被控端域名或IP,在inventory中需要声明force_handlers: yes  # 保证任务成功执行的handler任务也可以被执行tasks:- name: create myfilecommand: touch /home/zhangs/demo.txt- name: change the modcommand: chmod 755 /home/zhangs/demo.txtnotify: handler group  # 任务完成执行handler任务组- name: a task which fails because the package doesn't exist  # 创建一个必定失败的项目验证任务间互不干扰yum:name: notapkgstate: latesthandlers:- name: rm myfilelisten: handler groupcommand: rm /home/zhangs/demo.txt- name: create myfile againlisten: handler groupcommand: touch /home/zhangs/demo.txt

##如果与handler关联的task还未执行,在其前的task已经失败,整个play终止,则handler未被触发,也不会执行。
在《5.7 Handler》介绍中,有得到剧本任务执行顺序:所有任务完成→→最终任务,那么有没有方式不等到所有任务完成就去执行最终任务?关键字meta可以实现

  • 立即执行handler任务
- name: Ansible Project Demo  # 剧本名,我觉得叫项目名更合适,随意起hosts: ansible_node1  # 被控端域名或IP,在inventory中需要声明force_handlers: yes  # 保证任务成功执行的handler任务也可以被执行tasks:- name: create myfilecommand: touch /home/zhangs/demo.txt- name: change the modcommand: chmod 755 /home/zhangs/demo.txtnotify: handler group  # 任务完成执行handler任务组- meta: flush_handlers  # change the mod这个任务一经完成(无关所有任务完不完成),立即执行handler任务- name: a task which fails because the package doesn't exist  # 创建一个必定失败的项目验证任务间互不干扰yum:name: notapkgstate: latesthandlers:- name: rm myfilelisten: handler groupcommand: rm /home/zhangs/demo.txt- name: create myfile againlisten: handler groupcommand: touch /home/zhangs/demo.txt

5.8、最终版

需要什么取什么,可能不一定满足需要哈,仅供参考!!

 - name: play to setup web serverhosts: all  # 如果是主机,可以是localhosttasks:# 任务一:安装httpd包- name: latest httpd version installedyum:name: httpdstate: latest# 任务二:复制文件  - name: correct index.html is presentcopy: src: files/index.htmldest: /var/www/html/index.html# 任务三: 开启httpd服务并配置开机自启     - name: start httpd serviceservice:name: httpdstate: startedenabled: true# 任务四: 文件内容替换- name: 'change sshd_config'lineinfile:dest: /etc/ssh/sshd_configregexp: "{{ item.regexp }}"line: "{{ item.line }}"state: presentwith_items:#- regexp: "^#PermitRootLogin yes"#  line: "PermitRootLogin no"# Both methods have the same effect- { regexp: "PermitRootLogin yes",line: "PermitRootLogin no" }# 任务五: 安装Docker- name: Install Dockerapt:name: docker.iostate: present# 任务六: 启动Docker服务并设置开机自启- name: start docker servicesystemd:                         centos7开启服务,添加到启动项name: dockerstate: startedenabled: true# 任务七: 拉取nginx镜像- name: Pull Docker imagedocker_image:name: nginxstate: present# 任务八: 启动由nginx镜像生成的服务- name: Run Docker containerdocker_container:name: mynginximage: nginxstate: startedports:- "80:80"volumes:- "/var/www/html:/usr/share/nginx/html"# 任务九: docker配置文件文件变更,触发docker服务重启- name: provide docker-ce configfiletemplate:src: daemon.json.j2                 提供配置文件dest: /etc/docker/daemon.jsonnotify: restart docker                  配置文件修改了触发通知机制,提醒handler# 任务十: 关停项目,清除遗留,开启项目- name: project restartbecome: yesshell: /home/zhangs/miniconda3/envs/web_server/bin/supervisorctl -c server.conf stop all && lsof -i:4057|awk 'NR>1 {print $2}'|xargs -i kill {}|/home/zhangs/miniconda3/envs/web_server/bin/supervisorctl -c server.conf reloadargs:chdir:/home/zhangs/web_server# 任务十一: 重启docker- name: docker restartcommand: docker-compose -f xxx.yaml restart

六、引申

  • 其它相关资料

七、结束!

一篇教你学会Ansible相关推荐

  1. good 第一篇、教你学会看电路图轻松修手机

    第一篇.教你学会看电路图轻松修手机           第一节 了解电路图 一.一套完整的主板电路图,是由主板原理图和主板元件位置图组成的. 1.主板原理图,如图: 2.主板元件位置图,如图: 主板元 ...

  2. 一篇文章教你学会使用SpringBatch 监听器Listener

    文章目录 一.SpringBatch监听器 二.搭建SpringBatch开发环境 三.监听器详细介绍 1.JobExecutionListener 2.StepExecutionListener 3 ...

  3. 一篇文章教你学会使用SpringBoot实现文件上传和下载

    文章目录 一.搭建SpringBoot开发环境 1.创建项目 2.配置application.properties参数 3.实体响应类和异常信息类 4.创建FileController 二.接口测试 ...

  4. 不求人,手把手教你学会微信WIFI!

    前言:好吧,这是作者开博的第一篇文章,在如今朋友圈文章泛滥的时代,再看过了那么多"心灵鸡汤"."技术神贴"之后,作者也在思考,自己能不能写点什么,也许过上一百年 ...

  5. 21天教你学会C++

    21天教你学会C++" 2010年3月30日 陈皓 发表评论 阅读评论 31,048 次点击     下面是一个<Teach Yourself  C++ in 21 Days>的 ...

  6. matlab 画海面图,原来学画画这么简单?4步就能教你学会波浪、海面的画法

    海浪怎么画?波浪怎么画?水面怎么画?学习绘画难吗?怎样才能学好绘画?想必这些都是绘画初学者们经常在想的问题吧,就是不知道如何才能学习好绘画波涛汹涌的海面,比如说波浪的绘画技巧,根本不知道怎么画才好 那 ...

  7. matlab如何画趋势线,【高清图解】手把手,教你学会画趋势线!

    原标题:[高清图解]手把手,教你学会画趋势线! 点击标题下「每天一个K线炒股技巧」可快速关注 大家好,这里是老司机原创的股票知识:每天一个K线炒股技巧.今天的两篇文章为前面讲过的基础知识,今天来做个复 ...

  8. Playmaker Input篇教程之PlayMaker菜单概述

    Playmaker Input篇教程之PlayMaker菜单概述 ​Playmaker InputPlayMaker菜单概述 Playmaker插件被导入游戏项目以后,会自动为Unity编辑器添加一个 ...

  9. Playmaker Input篇教程之Playmaker购买下载和导入

    Playmaker Input篇教程之Playmaker购买下载和导入 Playmaker Input篇认识Playmaker Playmaker是Unity的插件,其标志如图1-1所示.开发者使用它 ...

  10. DeepMind新语言模型SUNDAE:教自动编码器学会「自我纠正」,WMT14英德互译任务获SOTA...

    丰色 发自 凹非寺 量子位 报道 | 公众号 QbitAI 一直以来,自回归语言模型(Autoregressive model,AR)在文本生成任务中表现都相当出色. 现在,DeepMind通过教自动 ...

最新文章

  1. 2021年春季学期-信号与系统-第十三次作业参考答案-第二小题
  2. 洛谷P2766-最长递增子序列问题
  3. 2018年企业运维开发经典面试题
  4. 人人可以理解的区块链100问——区块链记录哪些信息
  5. 叮!快收好这份Android网络性能监控方案
  6. 【转载】广告系统架构解密
  7. jsoup的Elements类
  8. 从结构体、内存池初始化到申请释放,详细解读鸿蒙轻内核的动态内存管理
  9. 第五十六题(最长公共子串)
  10. WAPPUSH 原理 基于短信网关WAP推送的实现
  11. 72+常用Axure交互原型免费下载
  12. 专利服务器拒收 文件异常解压,电子申请专利常见错误总结.pdf
  13. k 均值算法(k-means)
  14. 世界著名的四大检索工具
  15. 基于爬取百合网的数据,用matplotlib生成图表
  16. 办公技巧——PPT添加页码
  17. 用joern画AST、CFG、CDG、DDG、PDG、CPG
  18. 麦芒7能升级鸿蒙,华为鸿蒙系统升级名单
  19. 3.2.2对中文的考察2
  20. 帝国建站php,帝国cms支持php吗

热门文章

  1. C2 CompilerThread11引起的CPU较高分析
  2. 男人一生最该去的12个地方
  3. 利用机器学习之决策树来预测员工离职概率
  4. JVM 系列(三) --- 详解Java垃圾回收(GC)
  5. Day1 - Python基础介绍
  6. 默认浏览器被360浏览器拦截解决方法
  7. TortoiseSVN日常使用指南
  8. 2020-11-19 英语笔记
  9. scipy.io.matlab.miobase.MatWriteError: Matrix too large to save with Matlab 5 format
  10. 请问电脑配置可以优化吗