ansible模块临时命令

使用临时命令通过模块来执行任务
一、 查看系统上安装的所有模块
ansible-doc -l

查看ping模块帮助文档
ansible-doc ping

1、 ansible模块
文件模块:
copy:将本地文件复制到受控主机
file:设置文件的权限和其他属性
lineinfile:确保特定行是否在文件中,也就是说修改文件内容
synchronize:使用rsync同步内容

软件包模块
package:使用操作系统本机的自动检测软件包管理器管理软件包
yum:使用yum软件包管理器管理软件包
apt:使用apt软件包管理器管理软件包
dnf:使用dnf软件包管理器管理软件包
pip:从PyPI管理Python软件包

系统模块
firewalld:使用firewalld管理任意端口和服务
reboot:重新启动计算机
service:管理服务
user:添加、删除和管理用户账户

Net Tools模块
get_url:通过http、https或者ftp下载文件
nmcli:管理网络
uri:与WEB服务交互

语法:
ansible bgx -m command -a ‘df -h’
命令 主机名称 指定模块 模块名称 模块动作 具体命令

执行的状态返回信息:
绿色:执行成功并且不需要做改变的动作
黄色:执行成功并且对目标主机做变更
红色:执行失败

常用模块
案例1:user

临时命令使用user模块来确保newbie用户存在于node1.example.com上,并且其UID为4000
[student@node1 ~]$ ansible node1 -m user -a ‘name=newbie uid=4000 state=present’

创建用户并指定密码,如果该用户存在,仍然修改密码
[student@node1 ~]$ openssl passwd -1 linux
111bChlQ4jX$97x50MlATs0PA6UsObqN1.

[student@node1 ~]$ ansible all -m user -a ‘name=xuanning state=present password=“111bChlQ4jX$97x50MlATs0PA6UsObqN1.” update_password=always’

创建用户并指定密码,但是如果改用户存在,则不修改密码
[student@node1 ~]$ openssl passwd -1 redhat
$1zcVeWQiBzcVeWQiBzcVeWQiBdIsAdkcv91mTjrCaayN3F/

[student@node1 ~]$ ansible all -m user -a ‘name=xuanning12 state=present password=“111zcVeWQiB$dIsAdkcv91mTjrCaayN3F/” update_password=on_create’

案例2:shell
临时命令使用shell模块来删除node1.example.com节点中的用户newbie
ansible server1 -m shell -a ‘userdel -r newbie’

案例3:copy
ansible webserver -m copy -a ‘src=/etc/fstab dest=/var/tmp/fstab’

ansible webserver -m copy -a ‘src=/etc/fstab dest=/var/tmp/fstab group=chenyu owner=chenyu’

案例4:template模块—template模块用法和copy模块用法基本一致,它主要用于复制配置文件

ansible all -m template -a 'src=/usr/share/doc/httpd/httpd-vhosts.conf dest=/etc/httpd/conf.d/httpd-vhosts.conf group=root owner=root mode=0644 ’

案例5:file
修改文件的权限属性和context值
ansible webserver -m file -a ‘path=/var/tmp/fstab mode=g+w mode=o+w group=galaxy owner=galaxy setype=samba_share_t’

mode:设置权限可以是mode=g+w 也可以是mode=666
group:设置文件的所属组
owner:设置文件的所有者
setype:修改文件的context值

新建文件
ansible webserver -m file -a ‘path=/var/tmp/bbb state=touch’

新建目录
ansible webserver -m file -a ‘path=/var/tmp/cc state=directory’

删除文件或者目录
ansible webserver -m file -a ‘path=/var/tmp/cc state=absent’

创建软链接
ansible webserver -m file -a ‘dest=/var/tmp/chenyu src=/var/tmp/bbb state=link’

创建硬链接
ansible webserver -m file -a ‘dest=/var/tmp/chenyu1 src=/var/tmp/aaa state=hard’

案例6:lineinfile
把abc开头的一行换成 bbbbb
ansible webserver -m lineinfile -a ‘dest=/tmp/cy regexp=abc line=bbbbb’

在某一行前面插入一行新数据—insertbefore
ansible webserver -m lineinfile -a ‘dest=/tmp/cy insertbefore=“aa(.*)” line=chenyu’

在某一行后面插入一行新数据—insertafter
ansible webserver -m lineinfile -a ‘dest=/tmp/cy insertafter=“aaaa(.*)” line=bbbb’

删除某一行
ansible webserver -m lineinfile -a ‘dest=/tmp/cy regexp=“aaa(.*)” state=absent’

案例7:yum_repository模块-----配置yum仓库
ansible webserver -m yum_repository -a ‘file=server name=baseos description=rhel8 baseurl=file:///mnt/BaseOS enabled=yes gpgcheck=no’

ansible webserver -m yum_repository -a ‘file=server name=appstream description=RHEL8 baseurl=file:///mnt/AppStream enabled=yes gpgcheck=no’

案例8:yum模块----yum安装与卸载
state:present、installed、latest安装
absent、removed卸载
ansible all -m yum -a ‘name=httpd state=installed’ ----------------安装

ansible all -m yum -a ‘name=httpd state=removed’ ----------------卸载

案例9:service模块

重启httpd服务并设置下次启动生效
ansible all -m service -a ‘name=httpd state=started enabled=yes’

案例10:fetch—拉取文件模块
和copy工作方式类似,只不过是从远程主机将文件拉取到本地端,存储时使用主机名作为目录树,且只能拉取文件,不能拉取目录

将远程主机的/etc/fstab文件拉取到本地来,存储的名字为/tmp/node1(node2)/etc/fstab
ansible all -m fetch -a ‘src=/etc/fstab dest=/tmp’

将某台远程主机的/etc/fstab文件拉取到本地来,存储的名字为/tmp/fstab
ansible node1 -m fetch -a ‘src=/etc/fstab dest=/tmp/ flat=yes’

将远程主机的/etc/fstab文件拉取到本地来,存储的名字为/tmp/fstab-node1(node2)
ansible all -m fetch -a ‘src=/etc/fstab dest=/tmp/fstab-{{inventory_hostname}} flat=yes’

案例11:firewalld模块
允许http流量的传入
ansible all -m firewalld -a ‘service=http permanent=yes state=enabled immediate=yes’

富规则 允许172.16.30.0/24主机http流量的传入
ansible all -m firewalld -a ‘zone=public rich_rule=“rule family=ipv4 source address=192.168.226.0/24 service name=http accept” permanent=yes state=enabled immediate=yes’

案例12:replace模块
replace模块可以根据我们指定的正则表达式替换文件中的字符串,文件中所有被匹配的字符串都会被替换
参数:
path参数:2.3版本之前只能用dest、destfile、name指定操作文件,2.4版本中仍然可以用这些参数名,也可以用path
regexp参数:必须参数,指定一个python正则表达式,文件中与正则匹配的字符串将会被替换
replace参数:指定最终要替换成的字符串
backup参数:是否在修改文件之前对文件进行备份,最好设置为yes。

将/tmp/cy文件中的“abc”替换成“yyy”
ansible all -m replace -a ‘path=/tmp/cy regexp=“abc” replace=“yyy”’

将/tmp/cy文件中的“yyy”替换成“iii”,且把替换前的/tmp/cy文件备份
ansible all -m replace -a ‘path=/tmp/cy regexp=“yyy” replace=“iii” backup=yes’

案例13:parted模块
新建扩展分区
ansible node1 -m parted -a ‘device=/dev/sda number=4 part_type=extended part_start=46GiB part_end=49.8GiB state=present’

新建逻辑分区ansible node1 -m parted -a ‘device=/dev/sda number=5 part_type=logical part_start=46.1GiB part_end=48.2GiB state=present’

案例14:filesystem—文件系统
ansible node1 -m filesystem -a ‘fstype=xfs dev=/dev/sda5’

案例15:mount—挂载
新建挂载点/common
ansible node1 -m file -a ‘path=/common state=directory’

查看/dev/sda5的UUID
ansible node1 -m shell -a ‘blkid /dev/sda5’

将分区/dev/sda5挂载到/common目录
ansible node1 -m mount -a ‘path=/common src=“UUID=d162b8b9-2326-4ee4-a559-80861461c4f0” fstype=xfs state=mounted’

卸载
ansible node1 -m mount -a ‘path=/common src=“UUID=d162b8b9-2326-4ee4-a559-80861461c4f0” fstype=xfs state=absent’

案例16:lvg—新建卷组
ansible node1 -m lvg -a ‘vg=vg0 pesize=16M pvs=/dev/sda5’

案例17:lvol—新建逻辑卷
ansible node1 -m lvol -a ‘lv=lv0 size=1000M vg=vg0’

在线扩容逻辑卷
ansible node1 -m lvol -a ‘lv=lv0 size=1600M vg=vg0 resizefs=yes’

案例18:sefcontext—修改context值
ansible node1 -m file -a ‘path=/share state=directory’

修改context值
ansible node1 -m sefcontext -a ‘target=“/share(/.*)?” setype=samba_shar state=present’
应用新的selinux 文件的context值
ansible node1 -m command -a ‘restorecon -irv /share’

案例19:debug
用户输出自定义的信息,类似于echo、print等输出命令。ansible中的debug主要用于输出变量值、表达式值,以及用于when条件判断时。使用方式非常简单

案例20:cron—计划任务模块

ansible node1 -m cron -a ‘name=“xuanning” job=“/bin/echo zxcv” user=root minute=0 hour=14 state=present’

案例21:get_url

语法:ansible node1 -m get_url -a ‘url=需要下载的文件 dest=存放的位置’

一、部署web服务器
1、部署yum仓库
2、安装httpd
3、讲/var/www/html目录做一个软链接,到/www
4、在/www中新建index.html,内容为my name is lihao
5、实现在ansible中能够使用http://node1访问到该网页内容
将镜像挂载上去

[student@ansible ansible]$ ansible node1 -m mount  -a 'src=/dev/cdrom path=/mnt fstype=iso9660 state=present'
node1 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"dump": "0","fstab": "/etc/fstab","fstype": "iso9660","name": "/mnt","opts": "defaults","passno": "0","src": "/dev/cdrom"
}

配置yum源仓库


[student@ansible ansible]$ ansible node1 -m file -a 'path=/etc/yum.repos.d/* state=absent'
[student@ansible ansible]$ ansible node1 -m yum_repository -a 'file=xuanning name=baseos description=rhel8 baseurl=file:///mnt/BaseOS enabled=yes gpgcheck=no'   node1 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"repo": "baseos","state": "present"
}
[student@ansible ansible]$ ansible node1 -m yum_repository -a 'file=xuanning name=AppStream description=rhel8 baseurl=file:///mnt/AppStream enabled=yes gpgcheck=no'
node1 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": true,"repo": "AppStream","state": "present"
}

安装httpd并设置为开机自启


[student@ansible ansible]$ ansible node1 -m yum -a 'name=httpd state=installed'
[student@ansible ansible]$ ansible node1 -m service -a 'name=httpd enabled=yes '

将/var/www/html目录做一个软链接,到/www

[student@ansible ansible]$ ansible node1 -m file -a 'src=/var/www/html dest=/www owner=apache group=apache state=link'

在/www中新建index.html,内容为my name is lihao,并设置context值


[student@ansible ansible]$ ansible node1 -m copy -a 'content="my name is lihao" dest=/www/index.html'
[student@ansible ansible]$ ansible node1 -m file -a 'path=/www/index.html owner=apache group=apache setype=httpd_sys_content_t'

配置防火墙规则

[student@ansible ansible]$ ansible node1 -m firewalld -a 'service=http   permanent=yes state=enabled immediate=yes'

重启httpd服务

[student@ansible ansible]$ ansible node1 -m service -a 'name=httpd state=restarted'

在ansible节点上访问

[student@ansible ansible]$ curl http://node1
my name is lihao

ansible模块临时命令相关推荐

  1. 清单的用法、配置文件的配置、临时命令的用法

    清单的用法.配置文件的配置.临时命令的用法 构建Ansible清单 定义清单 清单定义Ansible将要管理的一批主机.这些主机也可以分配到组中,以进行集中管理.组可以包含子组,主机也可以是多个组的成 ...

  2. ansible模块command、shell、raw、script

    环境: ansible端: ip:192.168.100.129 hostname:node1.lansgg.com client端: ip:192.168.100.131 hostname:v2.l ...

  3. python ansible模块_ansible常用模块

    一.ansible常用模块 模块是Ansible执行的最小单位,可以是由Python编写,也可以是Shell编写,也可以是由其他语言编写. 一.ping模块 测试连接可通性,没有参数.通的话返回pon ...

  4. ansible 模块_您需要了解的Ansible模块

    ansible 模块 Ansible通过连接到节点并发送称为模块的小型程序来远程执行工作. 这使得它成为一个推式架构,其中配置从Ansible推到没有代理的服务器,这与基于代理的配置管理系统中通常会拉 ...

  5. ansible模块管理与主机清单配置

    文章目录 一. ansible简介 二 . Ansible 部署 及 模块篇实操 2.1 ansible 软件部署 2.2 设置代理登录,免去密码交互 2.3 各模块详解 ------command模 ...

  6. 3.2.1 运维自动化之ansible模块

    Ansible 原理 使用者使用Ansible或Ansible-playbooks时,在服务器终端输入Ansible的Ad-Hoc命令集或palybook后,Ansible会遵循预先编排的规则将Pla ...

  7. ansible模块:stat用法

    ansible模块:stat用法 ansible命令格式: ansible [-m module_name] [-a args] ansible后接主机名称:localhost,all,bigdata ...

  8. python os模块 常用命令

    os 模块用法示例 python编程时,经常和文件.目录打交道,这是就离不了os模块.os模块包含普遍的操作系统功能,与具体的平台无关.以下列举常用的命令 1. os.name()--判断现在正在实用 ...

  9. H3C光模块相关命令和检测方法

    当光模块不亮时首先确定对端有光过来,因为有光过来则光模块会亮,如果确定对端有光过来(见下面的命令),则调整两端的双工和速率,如果还是不亮则用以下方法:用一根好的尾纤自环后发现灯不亮则说明模块坏了 H3 ...

最新文章

  1. 全球智能驾驶产业全景图
  2. (已解决)(Jupyter notebook)中的tqdm进度条不是在一行更新并显示而是一直滚动往下输出
  3. SAP Spartacus central configuration - B2cStorefrontModule
  4. CQYZOJ P1392 拔河问题
  5. SQLServer数据库文件组相关知识笔记
  6. 什么是容器服务_【小牛云计算课堂】什么是容器镜像服务
  7. 一、安装Windows 2012域控(For SQLServer 2014 AlwaysOn)
  8. android 应用选择器的使用
  9. asp.net的10个提升性能或扩展性的秘密(二)
  10. node-gyp 出错
  11. sql server 2005 链接服务器:未将服务器 配置为用于 RPC
  12. startallback怎么用_startallback使用教程
  13. 安卓版有道词典的离线词库-《21世纪大英汉词典》等_我是亲民_新浪博客
  14. 记一次 Win 10 下的用户文件夹迁移
  15. 摄影系列:单反相机入门知识
  16. 背包客旅行札记-html
  17. php saas,saas系统是什么
  18. regopenkeyexfailed什么意思_外置网卡驱动安装出现RegOpenKeyEx Failed,怎么回事?
  19. dwcs6中php为什么浏览不了,dreamweaver CS6编辑浏览器列表无法添加浏览器的解决处理...
  20. PHP内核-Apache2的SAPI

热门文章

  1. FFplay文档解读-45-视频过滤器二十
  2. 阴天(唐伯虎点秋香版)3铃声 阴天(唐伯虎点秋香版)3手机铃声免...
  3. “上升”华为碰撞“下降”联想
  4. 使用Python爬取马蜂窝网站的游记和照片
  5. K-邻近算法及距离计算公式
  6. 成了!刚刚登顶全球首富的他,花440亿美元将推特买下 | 美通社头条
  7. 浏览器遇到找不到“www.xxxx.com”等的服务器的IP地址怎么解决
  8. 智慧城市与数字政府、城市大脑、一网统管之间什么关系
  9. Apple Watch如何重新配对
  10. 七脉轮位置_能量疗愈 | 七个脉轮的位置与功能