Ansible学习之ansible-pull命令

ops/2024/10/20 16:44:21/

想要知道ansible-pull是用来做什么的,就需要了解Ansible的工作模,Ansible的工作模式有两种:

  1. push模式
    push推送,这是Ansible的默认模式,在主控机上编排好playbook文件,push到远程主机上来执行。
  2. pull模式
    pull模式就是现在要介绍的ansible-pull命令来完成的功能,与push推送模式反向,pull模式是远程主机 拉去git仓库的playbook文件进行执行;这需要远程主机安装ansible和git。

pull模式的优点如下:
1. 斜体样式不需要主控机
2. 可以并行执行playbook任务,并且速度非常快
3. 连接有节点启动更加安全

pull 模式的工作流程:
图片来之网络
在这里插入图片描述

  1. 每台远程主机上都需要安装Ansible 和git(svn)
  2. 所有的配置及playbooks 都存放在git 仓库
  3. 远程主机的ansible-pull 计划任务会定期检查给定的git 的tag 或者分支
  4. ansible-pull 执行特定的playbooks 即local.yml 或者hostname.yml
  5. 每次更改playbooks 只需要提交到git 即可

实验测试:
在gitee上创建一个用于测试的库:pull-test

将这个库下载下来,并创建playbook剧本文件上传到pull-test库
#touchfile-playbook.yaml 内容如下

# cat touchfile-playbook.yaml
---
- hosts: localhostremote_user: rootgather_facts: notasks:- name: Create a filefile:path: /opt/script/ansible-pull-empty.txtstate:  touchmode: 0664
git clone https://gitee.com/crazy-stone-man/pull-test.git
cd pull-test/
git remote add ansible-pull https://gitee.com/crazy-stone-man/pull-test.git
git add touchfile-playbook.yaml
git commit -m "touch a file"
git push ansible-pull

可以在gitee仓库上查看文件是否被推送上去
在这里插入图片描述

# ansible-pull -U https://gitee.com/crazy-stone-man/pull-test.git touchfileplaybook.yaml
  • -U 指定playbook的URL。

  • -i: 指定inventory文件 我这里忽略了,默认是 /etc/ansible/hosts

    看看效果:

[root@localhost script]# ansible-pull -U https://gitee.com/crazy-stone-man/pull-test.git touchfileplaybook.yaml 
Starting Ansible Pull at 2024-09-30 05:43:15
/usr/bin/ansible-pull -U https://gitee.com/crazy-stone-man/pull-test.git touchfileplaybook.yaml
[WARNING]: Could not match supplied host pattern, ignoring: localhost.localdomain
localhost [WARNING]:| SUCCESS  Your git => {"version isafter": "2 too old t9daba82f0co fully sub39887ca15pport the eb0dce51bddepth arguad3c0066b"ment. Fall, "being back tfore": "29o full chedaba82f0cbckouts.
39887ca15eb0dce51bdad3c0066b", "changed": false, "remote_url_changed": false
}
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
[WARNING]: Could not match supplied host pattern, ignoring: localhost.localdomainPLAY [localhost] ********************************************************************************************************************************************TASK [Create a file] ****************************************************************************************************************************************
changed: [localhost]PLAY RECAP **************************************************************************************************************************************************
localhost                  : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   [root@localhost script]# ls
ansible-pull-empty.txt

执行成功了!

修改playbook文件重新上传,再次执行ansible-pull

---
- hosts: localhostremote_user: rootgather_facts: notasks:- name: Create a filefile:path: /opt/script/ansible-pull-empty.txtstate:  absent	#删除掉文件
[root@localhost script]# ansible-pull -U https://gitee.com/crazy-stone-man/pull-test.git touchfileplaybook.yaml 
Starting Ansible Pull at 2024-09-30 05:49:41
/usr/bin/ansible-pull -U https://gitee.com/crazy-stone-man/pull-test.git touchfileplaybook.yaml
[WARNING]: Could not match supplied host pattern, ignoring: localhost.localdomain
localhost [WARNING]:| CHANGED  Your git => {"version isafter": "d too old tbfda18ce36o fully su79910c8316pport the 5477f9dd84depth argu5e8f875aa"ment. Fall, "being back tfore": "29o full chedaba82f0cbckouts.
39887ca15eb0dce51bdad3c0066b", "changed": true, "remote_url_changed": false
}
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
[WARNING]: Could not match supplied host pattern, ignoring: localhost.localdomainPLAY [localhost] ********************************************************************************************************************************************TASK [Create a file] ****************************************************************************************************************************************
changed: [localhost]PLAY RECAP **************************************************************************************************************************************************
localhost                  : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   [root@localhost script]# ls
[root@localhost script]# 

可以看到已经被删除了!

学习更多ansible-pull功能使用

ansible-pull	--help

我创建的git仓库是公开的,在真实环境是不能公开的,而且最好是限制IP

加入定时任务:
定时去拉取playbook文件执行

*/5 * * * * ansible-pull -C master -d /tmp/ansible-pull -i /etc/ansible/hosts -U https://gitee.com/crazy-stone-man/pull-test.git  -o
  • -C 指定分支 checkout功能
  • -d Ansible Pull 将要使用的工作目录。它会将仓库克隆到这个目录,或者在该目录中查找已经克隆的仓库。
  • -i 指定 inventory 文件路径
  • -U 指定存储 playbook 的远程 Git 仓库的 URL。
  • -o 只有playbook 远程Git仓库更新才执行

参考连接:https://blog.csdn.net/yuezhilangniao/article/details/115799713


http://www.ppmy.cn/ops/122007.html

相关文章

6.MySQL基本查询

目录 表的增删查改Insert(插入)插入替换插入替换2 Retrieve(查找)SELECT 列全列查找指定列查询查询字段为表达式为查询结果指定别名结果去重 WHERE 条件order by子句筛选分页结果 Update(更新)delete&#…

【Docker从入门到进阶】03.进阶应用

3. 进阶应用 在本节中,我们将深入探讨Docker的高级应用,包括如何通过Dockerfile定义和构建镜像,数据管理的最佳实践,网络配置,以及如何使用Docker Compose来管理多容器应用。 基本指令详解 FROM: 可以使用特定版本的…

R包的安装、加载以及如何查看帮助文档

0x01 如何安装R包 一、通过R 内置函数安装(常用) 1.安装CRAN的R包 install.packages()是一个用于安装 R 包的重要函数。 语法:install.packages(pkgs, repos getOption("repos"),...) 其中: pkgs:要安…

Javascript数组研究02_手写实现_at_concat_copyWithin_entries_every

目录 1 Array.at() 1.1基本介绍 1.2 手写实现 2 Array.concat() 2.1 基本介绍 2.2 手写实现-获取构造函数与concat实现 3 Array.copyWithin() 3.1 基本介绍 3.2 手写实现 4 Array.entries() 4.1 基本介绍 4.2 手写实现 4.2.1 手写实现返回迭代器对象 4.2.2 使用ge…

深度剖析音频剪辑免费工具的特色与优势

是热爱生活的伙伴或者想要记录美好声音的普通用户,都可能会需要对音频进行剪辑处理。而幸运的是,现在有许多优秀的音频剪辑软件提供了免费版本,让我们能够轻松地施展音频剪辑的魔法。接下来,就让我们一同深入了解这些音频剪辑免费…

探索基于基于人工智能进行的漏洞评估的前景

根据2023年的一份报告 网络安全企业据估计,到 10.5 年,网络犯罪每年将给世界造成 2025 万亿美元的损失。每年记录在案的网络犯罪数量都会创下新高。这要求对传统的安全测试流程进行重大改变。这就是漏洞评估发挥作用的地方。 漏洞评估对于识别系统中的弱…

模拟实现消息队列(基于SpringBoot实现)

提要:此处的消息队列是仿照RabbitMQ实现(参数之类的),实现一些基本的操作:创建/销毁交互机(exchangeDeclare,exchangeDelete),队列(queueDeclare,…

GPT对话知识库——bootloader是什么?ymodel协议是什么?

目录 1,问: 1,答: Bootloader 的主要功能 Bootloader 的工作流程 Bootloader 的应用场景 典型 Bootloader 实现的例子 1. STM32 的 Bootloader 2. U-Boot (Universal Bootloader) 总结 2,问: 2&…