首页
视频
资源
登录
原
Ansible Playbook的用法
3880
人阅读
2021/3/25 21:50
总访问:
2658871
评论:
0
收藏:
0
手机
分类:
Ansible
![ansible](https://img.tnblog.net/arcimg/hb/e283c36cf43847558bcb2c8461e20850.jpg "ansible") ># Ansible Playbook的用法 [TOC] Ansible-galaxy ------------ <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 它可以连接 https://galaxy.ansible.com 下载相应的roles </p> ### 相关命令 ```bash # 列出所有已安装的galaxy ansible-galaxy list # 安装galaxy ansible-galaxy install geerlingguy.redis # 删除galaxy ansible-galaxy remove geerlingguy.redis ``` ### 示例 <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 去该网上下载: https://galaxy.ansible.com/geerlingguy/nginx 然后如下图所示我们找到对应的安装方式 下载完成后我们发现它下载到了:`/root/.ansible/roles/geerlingguy.nginx` 目录下 </p> ![](https://img.tnblog.net/arcimg/hb/8bcc72790a824e619c388b630bfb5d8e.png) ```bash # ansible-galaxy install geerlingguy.nginx Starting galaxy role install process - downloading role 'nginx', owned by geerlingguy - downloading role from https://github.com/geerlingguy/ansible-role-nginx/archive/2.8.0.tar.gz - extracting geerlingguy.nginx to /root/.ansible/roles/geerlingguy.nginx - geerlingguy.nginx (2.8.0) was installed successfully ``` ```bash # tree /root/.ansible/roles/geerlingguy.nginx /root/.ansible/roles/geerlingguy.nginx ├── defaults │ └── main.yml ├── handlers │ └── main.yml ├── LICENSE ├── meta │ └── main.yml ├── molecule │ └── default │ ├── converge.yml │ └── molecule.yml ├── README.md ├── tasks │ ├── main.yml │ ├── setup-Archlinux.yml │ ├── setup-Debian.yml │ ├── setup-FreeBSD.yml │ ├── setup-OpenBSD.yml │ ├── setup-RedHat.yml │ ├── setup-Ubuntu.yml │ └── vhosts.yml ├── templates │ ├── nginx.conf.j2 │ ├── nginx.repo.j2 │ └── vhost.j2 └── vars ├── Archlinux.yml ├── Debian.yml ├── FreeBSD.yml ├── OpenBSD.yml └── RedHat.yml ``` Ansible-pull ------------ <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 推送命令至远程,效率无限提升,对运维要求较高。更多请参考命令(`ansible-pull -h`) </p> Ansible-playbook ------------ <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 运行Ansible playbooks,在目标主机上执行自定义的任务。 那么我们就需要自定义自己的任务 </p> ### hello.yml示例 ```bash --- - hosts: webserver remote_user: root tasks: - name: hello command: hostname ``` <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 首先开头一般需要三个`-`,然后其他参数大家可以参考下列列表,为了方便大家编写可以在这上面写:https://www.bejson.com/validators/yaml_editor/ </p> | 参数 | 描述 | | ------------ | ------------ | | (老版本是:host) hosts | 指定执行任务组。这里我们指定的是`webserver` | | remote_user | 指远程任务用什么用户执行。 | | tasks | 任务列表 | | name | 任务名称 | | command | 执行的命令 | <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 最后我们保存好`hello.yaml`文件,然后下列命令对其子机执行任务,但并没有显示执行过程。 </p> ```bash ansible-playbook hello.yml ``` ![](https://img.tnblog.net/arcimg/hb/b4eeb27702024f41951f17dda3fc5df0.png) Ansible-vault加密工具 ------------ <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 管理加密解密yml文件的工具。 </p> ### 示例 ```bash # 输入加密hello.yml文件 ansible-vault encrypt hello.yml # 查看加密文件,我们会发现它是 AES256 进行加密的 cat hello.yml # 查看原文 ansible-vault view hello.yml # 编辑加密文件 ansible-vault edit hello.yml # 修改密码 ansible-vault rekey hello.yml # 解密hello.yml文件 ansible-vault decrypt hello.yml # 创建新文件 ansible-vault create hello.yml ``` Ansible-console交互式命令 ------------ <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 可交互式执行命令工具 </p> ```bash # 运行该工具 ansible-console # 我们会看到如下内容 root@all (2)[f:5]$ ``` <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 关于该格式的内容可以做如下解释: 执行用户@当前操作的主机组(当前组的主机数量)[f:并发的数量] $ </p> ### 设置10并发数 ```bash forks 10 ``` ### 切换到webserver组 ```bash cd webserver ``` ### 查看该组下的主机地址集合 ```bash list ``` ### 列出所有内置命令 ```bash ? 或 help ``` ### 执行模块 ```bash # [模块名] 参数 # 举例 shell ls ``` YAML介绍 ------------ <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> YAML是一个可读性高的用来表达资料序列的格式。YAML参考了其他很多种语言,包括:XML、C语言、Python、Perl以及电子邮件格式RFC2822等。 Yaml (Ait't Markup Language),即YAML不是XML。不过,在开发的这种语言时,YAML的意思其实是:`Yet Another Markup Language`(仍然是一种标记语言) </p> ### 特性 >- YAML的可读性好 - YAML与脚本语言的交互性好 - YAML使用实现语言的数据类型 - YAML有一个一致的信息模型 - YAML易于实现 - YAML可以基于流来处理 - YAML表达能力强,扩展性好 更多请参考: http://www.yaml.org ### YAML简介 >- 在单一档案中,可用连续三个连字号(---)区分多个档案。另外,还有选择性的连续三个点(...)用来表示档案结尾 - 次行开始正常写Playbook的内容,一般建议写明该Playbook的功能 - 使用#号注释代码 - 缩进必须是统一的,不能空格和tab混用 - 缩进的级别也必须是一致的,同样的缩进代表同样的级别,程序判别配置的级别是通过缩进结合换行来实现的 - YAML文件内容和Linux系统大小写判断方式保持一致,是区别大小的,k/v的值均需大小写敏感 - k/v的值可同行写也可以换行写。同行使用:分隔 - v可是个字符串,也可是另一个列表 - 一个完整的代码块功能需最少元素需包括 name: task - 一个name只能包括一个task - YAML文件扩展名通常为yml或yaml ### Yaml List集合 <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> List: 列表,其所有元素均使用 `-` 打头 示例: </p> ```yaml # A list of tasty fruits - Apple - Orange - Strawberry - Mango ``` ### Yaml Dictionary集合 <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> Dictionary: 字典,通常由多个key与value构成 示例: </p> ```yaml --- # An employee record name: Example Developer job: Developer skill: Elite ``` <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 也可以将 key:value 放置于{}中进行表示,用逗号(`,`)分隔多个 key:value 示例: </p> ```yaml --- # An employee record { name: Example Developer,job: Developer,skill: Elite } ``` <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> YAML的语法和其他高阶语言类似,并且可用简单表达清单、散列表,标量等数据结构。其(Structure)通过空格来展示,序列(Sequence)里的项用`-`来代表,Map里的键值对用`:`分隔 示例: </p> ```yaml name: John Smith age: 41 gender: Male spouse: name: Jane Smith age: 37 gender: Female children: - name: Jimmy Smith age: 17 gender: Male - name: Bob He age: 19 gender: Female ``` Playbook ------------ <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> - playbook是由一个或多个`play`组成的列表。 - play的主要功能在于将事先归并为一组的主机装扮成事先通过ansible中的task定义好的角色。从根本上来讲,所谓task无非是调用ansible的一个module。将多个play组织在一个playbook中,即可以让它们联同起来按事先编排的机制同唱一台大戏 - Playbook采用Yaml语言编写 </p> ###原理讲解 ![](https://img.tnblog.net/arcimg/hb/b7952376e32242b3ba6f0fb0ee00a56e.png) <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 1. 首先用户创建很多个`play`列出一个yaml清单。 2. 然后通过`ansible-playbook`进行执行先每一个`play` 3. 每一个`play`里面又找到多个`task`任务 4. 然后从`task`任务中又找到相对应的模块 5. 最后从主机清单中找到需要执行的主机列表 6. 然后通过SSH进行连接 7. 最后在各个主机上执行 </p> ###核心元素 | 组件名称 | 描述 | | ------------ | ------------ | | Hosts | 执行任务远程主机列表 | | Tasks | 任务集 | | Varniables | 内置变量或自定义变量在playbook中调用 | | Templates | 模板,可替换模板文件中的变量并实现一些简单逻辑的文件 | | Handlers | 与notity结合使用,由特定的条件触发操作,满足条件方才执行,否则不执行 | | tags | 标签指定某任务执行,用于选择运行playbook中的部分代码。ansible具有幂等性,因此会自动跳过没有变化的部分,即便如此,有些代码为测试其确实没有发生变化的时间以然会非常地长。此时,如果确信其没有变化,就可以通过tags跳过此些代码片段(`ansible-playbook -t tagsname useradd.yml`) | ### Hosts <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> playbook中的每一个play的目的都是为了让某个或某些主机指定的用户身份执行任务。host用于指定要执行任务的主机,须事先定义在主机清单中 可以是如下形式: one.example.com one.example.com:two.example.com 192.168.1.50 192.168.1.* webserver:dbserver 两个组的并集 webserver:&dbserver 两个组的交集 webserver:!phoenix 在webserver组,但不在dbserver组 </p> ```bash # 示例 - hosts: webserver:dbserver ``` ### 示例 <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 我们将原有的`httpd`停掉,然后将其卸载,最后我们将在本地进行安装一个`httpd`然后对`index.html`做出一定的修改,进行分发到不同的主机上。 </p> ```bash # 停止该服务 ansible all -m shell -a 'systemctl stop httpd' # 卸载软件 ansible all -m yum -a 'name=httpd state=absent' # 在本机安装httpd yum install httpd # 创建/var/www/html/index.html echo 'aidasi apache' > /var/www/html/index.html ``` <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 创建`file.yaml`文件,添加如下任务将会进行创建`/data/newfile`文件方法,创建创建test2用户方法,并安装`httpd`应用等功能方法 </p> ```yaml --- - hosts: webserver remote_user: root tasks: - name: 创建文件 file: name=/data/newfile state=touch - name: 创建test2用户 user: name=test2 system=yes shell=/sbin/nologin - name: 安装httpd包 yum: name=httpd - name: 复制网页 copy: src=/var/www/html/index.html dest=/var/www/html - name: 启动服务 service: name=httpd state=started enabled=yes ``` <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 我们先测试一下我们等yaml有没有问题,然后再进行执行。 </p> ```bash # 测试yaml执行 ansible-playbook -C file.yaml # 执行yaml ansible-playbook file.yaml ``` ![](https://img.tnblog.net/arcimg/hb/2643c6535c0f48349b265c9fbc6dbd2b.png) <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 最后我们到实体机上测试任务是否全部完成。 </p> ```bash ansible all -a 'ls /data' ansible all -a 'getent passwd test2' ansible all -a 'ss -lp src :80' # 或 ansible all -m shell -a 'ss -tln|grep :80' ``` ![](https://img.tnblog.net/arcimg/hb/02b412aab03d4274bad097b233d446d1.png) ### Remote_user <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 可用于Host和task中。也可以通过指定其通过sudo的方式主机上执行任务,其可用于`play`全局或某任务;此外,甚至可以在`sudo`时使用`sudo_user`指定`sudo`时切换的用户 </p> ```yaml --- - hosts: webserver remote_user: root tasks: - name: test connection ping: remote_user: aidasi # 远程连接的用户 sudo: yes # 默认为root sudo_user: testuser # sudo为testuser ``` task列表和action ------------ <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> play的主体部分是task list。task list 中各任务按次序逐个在hosts中指定的所有主机上执行,即在所有主机上完成第一个任务后再完成第二个。在运行自下而下某playbook时,如果中途发生错误,所有已执行任务都将回滚,因此,在更正playbook后重新执行一次即可。 task的目的是使用指定的参数执行模块,而在模块参数中可以使用变量。模块执行是冥等的,这意味着多次执行是安全的,因为其结果均一致 每个task都应该有其name,用于playbook的执行输出,建议其内容尽可能的清晰的描述任务执行步骤。如果未提供name,则action的结果将用于输出 </p> ###关于中途任务执行报错的解决方案 <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 如果命令或脚本的退出码不为零,可以使用如下方式替代。 </p> ```yaml task: - name: xxxxx shell: 你的命令 || /bin/true ``` <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 或者使用ignore_errors来忽略错误信息。 </p> ```yaml task: - name: xxxxx shell: 你的命令 ignore_errors: True ``` 运行playbook ------------ <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 运行playbook的方式 </p> ```bash ansible-playbook <filename.yaml> [options] ``` <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 常见选项 </p> | 参数 | 描述 | | ------------ | ------------ | | `--check` | 只检测可能发生的改变,但不真正执行操作 | | `--list-hosts` | 列出运行任务但主机 | | `--limit` | 主机列表 只针对主机列表中但主机执行 | | `-v` | 显示过程,`-vv`或`-vvv`更加详细 | <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 示例 </p> ```bash ansible-playbook file.yaml --check #只检测 ansible-playbook file.yaml ansible-playbook file.yaml --limit webserver #只执行webserver组下面的 ansible-playbook file.yaml --list-tags # 查看标记 ``` handlers和notify结合使用触发条件 ------------ ### Handlers <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 是task列表,这些task与前述的task并没有本质上的不同,用于当前关注的支援发生变化时,才会采取一定的操作。 </p> ### Notify <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> Notify此action可用于在每个play的最后被触发,这样可避免多次有改变发生时每次都执行指定的操作,仅在所有的变化发生完成最后一次性地执行指定操作。在notify中列出的操作称为handler,也即notify中调用handler中定义的操作。 </p> ### 示例 <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 当我们对index.html的内容做了一些修改的时候,或者我们对阿帕奇服务器的配置文件做出一些调整的时候,我就需要重启一下服务,那么我们可以在复制完网页后执行重启事件。如果我们在重启后还需要对其中的内容进行查看我们还可以添加一个事件 </p> ```bash --- - hosts: webserver remote_user: root tasks: - name: 安装httpd包 yum: name=httpd - name: 复制网页 copy: src=/var/www/html/index.html dest=/var/www/html backup=yes notify: restart service - name: 启动服务 service: name=httpd state=started enabled=yes handlers: - name: restart service service: name=httpd state=restarted ``` ```bash --- - hosts: webserver remote_user: root tasks: - name: 安装httpd包 yum: name=httpd - name: 复制网页 copy: src=/var/www/html/index.html dest=/var/www/html backup=yes notify: - restart service - view content - name: 启动服务 service: name=httpd state=started enabled=yes handlers: - name: restart service service: name=httpd state=restarted - name: view content shell: cat /var/www/html/index.html ``` Tags的应用 ------------ <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 在每个action上标记tags后,可以只执行其中的一个或多个标记上的action。 </p> ### 示例 <p style=" font-weight: 400; line-height: 1.5; color: #212529; -webkit-tap-highlight-color: transparent; box-sizing: border-box; padding: 0px 20px 20px 20px; border: 1px solid #e9ecef; border-left-width: .25rem; border-radius: .25rem; display: block; border-left-color: #5bc0de;"> 这里我们在安装httpd包设置inshttpd标签,复制网页并重启我们也设置 thrshttpd 标签,然后我们只需要执行这两个标签时就可以通过下面的命令进行执行。 </p> ```yaml --- - hosts: webserver remote_user: root tasks: - name: 安装httpd包 yum: name=httpd tags: inshttpd - name: 复制网页 copy: src=/var/www/html/index.html dest=/var/www/html backup=yes notify: - restart service - view content tags: thrshttpd - name: 启动服务 service: name=httpd state=started enabled=yes handlers: - name: restart service service: name=httpd state=restarted - name: view content shell: cat /var/www/html/index.html ``` ```bash # t 参数 ansible-playbook -t inshttpd,thrshttpd file2.yaml ``` ![](https://img.tnblog.net/arcimg/hb/c8a6180d1d63417aada5fec095136e29.png)
欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739
👈{{preArticle.title}}
👉{{nextArticle.title}}
评价
{{titleitem}}
{{titleitem}}
{{item.content}}
{{titleitem}}
{{titleitem}}
{{item.content}}
尘叶心繁
这一世以无限游戏为使命!
博主信息
排名
6
文章
6
粉丝
16
评论
8
文章类别
.net后台框架
171篇
linux
17篇
linux中cve
1篇
windows中cve
0篇
资源分享
10篇
Win32
3篇
前端
28篇
传说中的c
4篇
Xamarin
9篇
docker
15篇
容器编排
101篇
grpc
4篇
Go
15篇
yaml模板
1篇
理论
2篇
更多
Sqlserver
4篇
云产品
39篇
git
3篇
Unity
1篇
考证
2篇
RabbitMq
23篇
Harbor
1篇
Ansible
8篇
Jenkins
17篇
Vue
1篇
Ids4
18篇
istio
1篇
架构
2篇
网络
7篇
windbg
4篇
AI
18篇
threejs
2篇
人物
1篇
嵌入式
4篇
python
13篇
HuggingFace
8篇
pytorch
9篇
opencv
6篇
Halcon
5篇
最新文章
最新评价
{{item.articleTitle}}
{{item.blogName}}
:
{{item.content}}
关于我们
ICP备案 :
渝ICP备18016597号-1
网站信息:
2018-2024
TNBLOG.NET
技术交流:
群号656732739
联系我们:
contact@tnblog.net
欢迎加群
欢迎加群交流技术