ansible的常见用法

目录

##编辑hosts文件

##copy模块##复制过去

##fetch模块##拉取

[##shell模块 ##好用](##shell模块 ##好用)

##command模块##

##file模块###

##cron模块###

[##crontab 计划任务](##crontab 计划任务)

##下载好时间插件

##script模块

##yum模块##

[##yum下载源配置文件 /etc/yum.repos.d/CentOS-Base.repo](##yum下载源配置文件 /etc/yum.repos.d/CentOS-Base.repo)

##service模块

##anisble-playbook##

##yaml格式

##template(模版)

##传递变量

##yaml文本里面传递

##直接接-e传递变量

##还可以在hosts文件里指定变量

##setup模块##

[##absible roles 模块##](##absible roles 模块##)

[##handlers和notify 的使用](##handlers和notify 的使用)

##yaml格式

##拓展:时间的命令


ansible的重要组成部分

Palybook --#剧本 ,让主机按照我给的剧本去完成一些事情

Module --#模块 去实现一个个功能的程序

Pluging --#插件 实现一些额外的小功能

ansible 的yum 安装

  1. ##安装 yum install epel-release -y

  2. yum install ansible -y

  3. ##配置文件

    /etc/ansible/ansible.cfg

    Ansible 的主配置文件,此文件主要定义了roles_path的路径

    /etc/ansible/hosts

    默认主机清单配置文件,可以通过ansible.cfg

    ##复制一个备份文件

    root@wangyalin-hunan-changsha ansible\]# cp hosts{,.bak} ##先备份 \[root@wangyalin-hunan-changsha ansible\]# ls ansible.cfg hosts hosts.bak roles \[root@wangyalin-hunan-changsha ansible\]# \>hosts #在清空文件

root@wangyalin-hunan-changsha ansible\]# vim hosts \[root@wangyalin-hunan-changsha ansible\]# cat hosts #将需要管理的主机添加到webser组 #默认端口号就是22 #如果通过ssh登录的端口不是22端口,就需要在配置文件里面指明端口号 \[web

192.168.47.196

192.168.47.200

all

192.168.47.196

192.168.47.200

192.168.47.205

除了以上两个重要的配置文件还有三个重要的可执行文件:

ansible ##主执行程序,一般用于命令行下的执行任务

ansible-palybook ##执行playbook中的任务,一般是配合yaml文件使用

ansible-doc ##获取各个模块的帮助信息

用法:

root@dhcp-clinet roles\]# ansible-doc copy \> COPY (/usr/lib/python2.7/site-packages/ansible/modules/files/copy.py) The \`copy' module copies a file from the local or remote machine to a location on the remote machine. Use the \[fetch\] module to copy files from remote locations to the local box. If you need variable interpolation in copied files, use the \[template\] module. Using a variable in the \`content' field will result in unpredictable output. For Windows targets, use the \[win_copy

module instead.

##copy模块##复制过去

#复制本台主机的信息到远程主机上

root@wangyalin-hunan-changsha ansible-test\]# ansible web -m copy -a "src=opt/ansible-test/ansible.txt dest=/opt/ansible.txt mode=755 owner=sc" ###创建过去文件,会把那边的文件进行覆盖 ### **##fetch模块##拉取** ##copy的功能很相似,就是把远程主机的信息复制到本台主机上 #不能指定属主和属组,以及模式权限 \[root@wangyalin-hunan-changsha ansible\]# ansible web -m fetch -a "src=/etc/passwd dest=/opt/ansible-test" 192.168.47.196 \| CHANGED =\> { "changed": true, "checksum": "297601f83a2a36e93e7992aeaf1f976d7199a433", "dest": "/opt/ansible-test/192.168.47.196/etc/passwd", "md5sum": "766c45a30bc8408edb2065ea3e9559b0", "remote_checksum": "297601f83a2a36e93e7992aeaf1f976d7199a433", "remote_md5sum": null } 192.168.47.200 \| CHANGED =\> { "changed": true, "checksum": "4d3bcc31d1f61f892f342f0a0e23823edbd73773", "dest": "/opt/ansible-test/192.168.47.200/etc/passwd", "md5sum": "d0af7a0f15cc1b9c76b47d4e38249bae", "remote_checksum": "4d3bcc31d1f61f892f342f0a0e23823edbd73773", "remote_md5sum": null } ### **##shell模块 ##好用** 在web的用户,进行shell编程,在各个用户下下面/tnp/sc 创建这个sc文件夹 \[root@wangyalin-hunan-changsha ansible\]# ansible web -m shell -a "mkdir /tmp/sc" \[WARNING\]: Consider using the file module with state=directory rather than running 'mkdir'. If you need to use command because file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message. 192.168.47.196 \| CHANGED \| rc=0 \>\> 192.168.47.200 \| CHANGED \| rc=0 \>\> ###效果,注意的是结果返回值是零表示成功,非零则为失败 \[root@wangyalin-hunan-changsha pssh-test\]# ls /tmp/ sc ### **##command模块##** #很像那个shell但是shell还是更加好用一点 \[root@wangyalin-hunan-changsha ssh\]# ansible web -m command -a "ip add \|gerp ens33" 192.168.47.196 \| FAILED \| rc=255 \>\> Command "\|gerp" is unknown, try "ip address help".non-zero return code 192.168.47.200 \| FAILED \| rc=255 \>\> Command "\|gerp" is unknown, try "ip address help".non-zero return code ### **##file模块###** ![](https://file.jishuzhan.net/article/1779309609408794626/b24726a9b623a8dc1021582308943f99.webp) State-- abset删除文件和目录 Direcstory 目录 Touch 创建空文件 Hard 建立硬链接 Link 建立软连接 ##创建文件效果 \[root@wangyalin-hunan-changsha ssh\]# ansible web -m file -a "path=/opt/aniblle-file-test state=directory" 192.168.47.200 \| CHANGED =\> { ###创建软连接 \[root@wangyalin-hunan-changsha ssh\]# ansible web -m file -a "path=/opt/ansible-link state=link src=/etc/passwd ##ip为192.168.47.200##效果如下 \[root@wangyalin opt\]# ls aniblle-file-test ansible-link ansible.txt pssh-test wangyalin.txt ### **##cron模块###** #计划任务 ![](https://file.jishuzhan.net/article/1779309609408794626/616d8aa40549f5d509e57f534e6d1634.webp) **##crontab 计划任务** ###编辑计划任务 #1.crontab -e #2.vim /etc/crontab \[root@wangyalin-hunan-changsha cron\]# pwd /var/spool/cron #查看当前用户有哪些的计划任务 Crontab -l \[root@wangyalin-hunan-changsha cron\]# crontab -l 0 2 \* \* \* bash /backup/backup_log.sh #\*/1 \* \* \* \* mkdir /backup/sc$RANDOM #Ansible: ansible-test \*/2 \* \* \* \* date \>\>/temp/time.txt ![](https://file.jishuzhan.net/article/1779309609408794626/20ca4eaadb528bbfe68b2af9cfc2ef4b.webp) ##每两分钟实现将date时间输出到/temp/time.txt文本里面 \[root@wangyalin-hunan-changsha etc\]# ansible web -m cron -a "minute=\*/2 job='date \>\>/temp/time.txt' name=ansible-test state=present" 192.168.47.200 \| CHANGED =\> { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "envs": \[\], "jobs": \[ "ansible-test"

}

192.168.47.196 | CHANGED => {

"ansible_facts": {

"discovered_interpreter_python": "/usr/bin/python"

},

"changed": true,

"envs": [],

"jobs": [

"ansible-test"

]

}

##下载好时间插件

root@wangyalin opt\]# yum install chrony -y ### **##script模块** 把本地的脚本到远端执行;前提是到远端可以执行,不要把linux下的脚本同步到Windows下执行 #只能在远端服务器执行脚本,不上传脚本到远端服务器 \[root@sanchuang-wangyalin test\]# ansible web -m script -a "./script_model_test.sh" ### **##yum模块##** ##ansible使用yum模块 安装wget模块 \[root@sanchuang-wangyalin ansible\]# ansible web -m yum -a "name=wget state=present" \[root@sanchuang-wangyalin ansible\]# ansible web -m yum -a "name=wget state=installed" 卸载wget模块 \[root@sanchuang-wangyalin ansible\]# ansible web -m yum -a "name=wget state=absent" \[root@sanchuang-wangyalin ansible\]# ansible web -m yum -a "name=wget state=removed" ##安装vsftpd 和nginx \[root@sanchuang-wangyalin ansible\]# ansible web -m yum -a "name=vsftpd state=present" 用于安全快速传输文件服务器 \[root@sanchuang-wangyalin ansible\]# ansible web -m yum -a "name=nginx state=present" 用于是一个高性能的开源 Web 服务器软件,也可以用作反向代理服务器、负载均衡器和 HTTP 缓存等 ![](https://file.jishuzhan.net/article/1779309609408794626/34476e0884b089d48e24ad9a9f3b89c2.webp) ##### **##yum下载源配置文件 /etc/yum.repos.d/CentOS-Base.repo** ##eqel--拓展源 \[root@sanchuang-wangyalin yum.repos.d\]# yum install eqel-release -y \[base

name=CentOS-$releasever - Base

mirrorlist=http://mirrorlist.centos.org/?release=$releasever\&arch=$basearch\&repo=os\&infra=$infra

#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/

gpgcheck=1 #为1开启检验

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

enabled=0 #是否启动当前源 0表示禁用

#released updates

##service模块

服务管理参数

常用参数

name服务名

state:服务状态 started 启动 stopped 关闭

enabled 是否开启启动 truefalse

root@sanchuang-wangyalin etc\]# ansible web -m service -a "name=nginx state=started" runevel:启动级别(systemctl方式忽略) ## **##anisble-playbook##** 剧本--》连续性的要求--》按照步骤走 #语法按照yaml格式 1. **playbook的核心元素** hosts:playbook配置文件作用的主机 tasks:任务列表 variables: 变量 templates:包含模块语法的文本文件 handlers :有特定条件出发的任务 roles:用于层次性,结构化地组织playbook roles能根据层次型结构自动装载变量文件、tasks以及handles等 #### **##yaml格式** 缩进必须对齐空格,不然会乱 \[root@sanchuang-wangyalin ansible-playbook\]# cat playbook01.yaml --- - hosts: 192.168.47.196 ##定义远程执行主机 remote_user: root ##定义远程用户 tasks: ##指定任务,可以执行很多个 - name: create user ##任务名字 user: name=pb ##任务执行的模块,和参数 - name: cpoy file copy: src=/opt/ansible-test/index.html dest=/opt/index.html name:sc2 ##层级关系 hunan: shengfen: changsha hupo: dongtinghu #列表 classes: -yingboshuyuan -yuelushuyuan #### **##template(模版)** ##遵循jinja2模版语法 大部分内容是一样的 结合变量一起使用以 .j2结尾 {{}}双大括号里面放变量 #### **##传递变量** ##变量的不同的指定方式 #写在playbook文件中--vars指令 ##### **##yaml文本里面传递** \[root@sanchuang-wangyalin ansible-playbook\]# cat playbook03.yaml --- - hosts: web vars: - file_name: test.txt tasks: - name: create file file: path: /tmp/{{file_name}} state: touch ##### **##直接接-e传递变量** \[root@sanchuang-wangyalin ansible-playbook\]# ansible-playbook playbook03.yaml -e "file_name=test01.txt" ##### **##还可以在hosts文件里指定变量** ##运行 \[root@sanchuang-wangyalin ansible-playbook\]# ansible-playbook playbook03.yaml ##查看 \[root@sanchuang-wangyalin ansible-playbook\]# cat /etc/ansible/hosts #将需要管理的主机添加到webser组 #默认端口号就是22 #如果通过ssh登录的端口不是22端口,就需要在配置文件里面指明端口号 \[web

192.168.47.196 file_name=web1.txt

192.168.47.200 file_name=web2.txt

all

192.168.47.196

192.168.47.200

192.168.47.205

##setup模块##

Ansible 中的一个特殊模块,用于收集主机的详细信息

##absible roles 模块##

分块管理目录

  1. 建立目录结构
  2. 编写nginx配置文件模版

##handlers和notify 的使用

详见Ansible--Ansible之Playbook - 别来无恙- - 博客园 (cnblogs.com)

##yaml格式

  1. playbook的运作方式

##变量的不同的指定方式

#写在playbook文件中--vars指令

##拓展:时间的命令

  • %Y:4位数的年份(例如:2022)
  • %y:2位数的年份(例如:22)
  • %m:2位数的月份(例如:01到12)
  • %B:完整的月份名称(例如:January、February)
  • %b:缩写的月份名称(例如:Jan、Feb)
  • %d:2位数的日期(例如:01到31)
  • %A:完整的星期几名称(例如:Sunday、Monday)
  • %a:缩写的星期几名称(例如:Sun、Mon)
  • %H:24小时制的小时数(例如:00到23)
  • %I:12小时制的小时数(例如:01到12)
  • %M:分钟数(例如:00到59)
  • %S:秒数(例如:00到59)
  • %p:AM或PM
  • %Z:时区

##这里是之前ansible的补充内容

相关推荐
曹瑞曹瑞几秒前
VMware导入vmdk文件
linux·运维·服务器
Johny_Zhao4 分钟前
2025年6月Docker镜像加速失效终极解决方案
linux·网络·网络安全·docker·信息安全·kubernetes·云计算·containerd·yum源·系统运维
hello kitty w30 分钟前
Python学习(7) ----- Python起源
linux·python·学习
十年磨一剑~40 分钟前
centos查看开启关闭防火墙状态
linux·运维·centos
无效的名字1 小时前
向日葵远程控制debian无法进入控制画面的解决方法
运维·debian
行云流水剑1 小时前
【学习记录】在 Ubuntu 中将新硬盘挂载到 /home 目录的完整指南
服务器·学习·ubuntu
藥瓿亭1 小时前
K8S认证|CKS题库+答案| 7. Dockerfile 检测
运维·ubuntu·docker·云原生·容器·kubernetes·cks
搬码临时工2 小时前
如何把本地服务器变成公网服务器?内网ip网址转换到外网连接访问
运维·服务器·网络·tcp/ip·智能路由器·远程工作·访问公司内网
vortex52 小时前
探索 Shell:选择适合你的命令行利器 bash, zsh, fish, dash, sh...
linux·开发语言·bash·shell·dash
GalaxyPokemon2 小时前
LeetCode - 148. 排序链表
linux·算法·leetcode