Ansible学习之ansible-pull命令

server/2024/10/21 9:47:14/

想要知道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/server/127941.html

相关文章

第二届两岸新经济产业发展研讨会闭幕,爱迪斯通董事长发表演讲

9月29日,第二届两岸新经济产业发展研讨会在福州高新区圆满落幕。此次研讨会由清华大学两岸发展研究院主办,福州市招商行动领导小组办公室、福州高新区承办,汇聚了两岸的专家学者及企业家代表近200人,共同探讨新质生产力的发展与两…

04.useTitle

在 React 应用中,动态更新页面标题是提升用户体验的一个重要方面。它可以让用户更清楚地知道当前页面的内容或状态,特别是在单页应用(SPA)中。useTitle 钩子提供了一种简单而有效的方式来管理文档标题。以下是如何实现和使用这个自定义钩子: const useTitle = title =>…

useEffect 与 useLayoutEffect 的区别

useEffect 与 useLayoutEffect 的区别 useEffect和useLayoutEffect是处理副作用的React钩子函数,有以下区别1. 执行时机不同2. 对性能影响不同3. 对渲染的影响不同:4. 使用场景不同 使用建议 useEffect和useLayoutEffect是处理副作用的React钩子函数&…

Art. 1 | 信号、信息与消息的区别及其在通信中的应用

信号、信息与消息的区别及其在通信中的应用 通信技术是现代社会的基石,其广泛应用于日常生活的各个方面。从手机、互联网到企业信息管理,通信系统无处不在。在这一技术领域中,信号、信息和消息是三大基础概念,支撑着整个通信系统…

SM2无证书及隐式证书公钥机制签名和加密过程详解(二)

前面对非显式证书公钥机制(无证书和隐式证书)的密钥生成过程进行了描述(SM2无证书及隐式证书公钥机制签名和加密过程详解(一)_sm2加密解密过程-CSDN博客),这里接着对隐式证书ASN.1模板和生成过程进行说明。 &#xff…

go+redis基于tcp实现聊天室

goredis实现聊天室 基于tcp连接通过redis实现了消息的广播,命令改名,查询在线人数,查询用户活跃度 serverclinet server 聊天室服务端的流程可以分为几个主要部分:初始化、监听连接、处理每个连接以及消息的处理和转发 1. 初…

【WRF数据准备】MODIS静态地理数据下载及制备

【WRF数据准备】MODIS静态地理数据下载及制备 MODIS数据介绍数据下载数据拼接MRT工具介绍基于MRT软件完成数据拼接 格式转换:tif文件转二进制格式编写INDEX修改GEOGRID.TBL以及namelist.wps修改GEOGRID.TBL修改namelist.wps 参考 MODIS数据介绍 MODIS-MCD12Q1 v061…

数字通信中不同信道类型对通信系统性能影响matlab仿真分析,对比AWGN,BEC,BSC以及多径信道

目录 1.算法运行效果图预览 2.算法运行软件版本 3.部分核心程序 4.算法理论概述 5.算法完整程序工程 1.算法运行效果图预览 (完整程序运行后无水印) 2.算法运行软件版本 matlab2022a 3.部分核心程序 (完整版代码包含详细中文注释和操作步骤视频&#xff09…