ansible使用和模块分析

上节我们了解到ansible的基本工作架构和原理,那么我们现在了解一些基本使用和常用模块

ansible系统命令

  • ansible系统命令
ansible   ansible-doc   ansible-playbook    ansible-vault
ansible-console    ansible-galaxy     ansible-pull
  • ansible-doc:显示模块帮助
 Usage: ansible-doc [-l|-F|-s] [options] [-t <plugin type> ] [plugin]
   -a               显示所有模块的文档
   -l、--list       列出可用模块
   -s、 --snippet   显示指定模块的playbook片段,以简单模式  
示例:
ansible-doc -l      列出所有模块
ansible-doc ping    查看指定模块帮助用法
ansible-doc -s  ping   查看指定模块帮助用法
  • ansible
ansible通过ssh实现配置管理、应用部署、任务执行等功能,建议配置ansible端基于秘钥认证的方式联系
各被管理节点
Usage: ansible <host-pattern> [options]
     --version   显示版本
	 -m  module    指定模块,默认为command
	 -v  详细过程  -vv   -vvv更详细
	 --list-hosts   显示主机列表,可简写-list
	 -k,--ask-pass     提示输入ssh连接密码,默认key验证
	 -K,--ask-become-pass    提示输入sudo时的口令
	 -C,--check  检查,并不执行
	 -T,--timeout=	TIMEOUT 执行命令的超时时间,默认为10s
	 -u,--user=REMOTE_USER  执行远程执行的用户
	 -b,--become    代替旧版的sudo切换
  • Ansible的Host-pattern
   匹配主机的列表
   ALL:显示所有的inventory的所有主机
       ansible  all  -m   ping
   通配符
       ansible   "*"  -m    ping
       ansible   10.0.194.*   -m   ping
       ansible   "web*"	   -m  ping
   或关系
       ansible   "webserver:dbserver"   -m  ping
       ansible   "10.0.194.81:10.0.194.83"	 -m  ping
   逻辑与
       ansible   "webserver:&dbserver"    -m  ping 
       在webserver组并且在dbserver组中的主机
   逻辑非
       ansible   'webserver:!dbserver'  -m  ping
       在webserver组,但不在dbserver组中的主机
       注意:此处为单引号
    正则表达式
        ansible  "webserver:&dbserver"	-m   ping

Ansible常用模块

  • Copy
Copy:从服务器复制文件到客户端
   ansible  webserver -m copy -a "src=/root/1.txt  dest=/tmp/2.txt  owner=root  mode=600 backup=yes"
   如果目标文件存在,默认覆盖,此处指定先备份
   ansible  webserver -m copy -a "content='test  content\n' dest=/tmp/2.txt "
   利用内容,直接生成目标文件
  • Fetch
Fetch:从客户端拉取文件至服务器端,copy相反,目录可先tar
   ansible  webserver -m fetch -a "src=/tmp/2.txt dest=root/2.txt 
  • File
File:设置文件属性
   group:定义文件/目录的属组 
   mode:定义文件/目录的权限
   owner:定义文件/目录的属主
   path:必选项,定义文件/目录的路径
   recurse:递归的设置文件的属性,只对目录有效
   src:要被链接的源文件的路径,只应用于state=link的情况
   dest:被链接到的路径,只应用于state=link的情况 
   state:  
   --directory:如果目录不存在,创建目录
   --file:即使文件不存在,也不会被创建
   --link:创建软链接
   --hard:创建硬链接
   --touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间
   absent:删除目录、文件或者取消链接文件
   ansible  webserver -m file -a "path=/tmp/2.txt owner=root   mode=755 "
   ansible   nginx  -m  file   -a "src=/etc/hosts  dest=/tmp/hosts_link   state=link"
   state	touch(创建文件)
   directory(创建目录)
   link(软连接)
   hard(硬链接)
   absent(删除)
   recurse	递归的设置文件的属性,只对目录有效,yes表示使用递归设置
   ansible test -m file -a "path=/tmp/ceshi owner=test group=test mode=644 recurse=yes" 递归修改权限
  • Hostname
Hostname:管理主机名
   ansible  webserver -m hostname -a  "name=node1"
   
  • Cron
支持时间:minute,hour,day,month,weekday
   ansible  webserver -m  cron -a  "minute=*/5  job='/usr/sbin/ntpdate 10.0.0.1'  &>/dev/null   name=Synctime"    创建任务
   ansible  webserver -m  cron -a  "state=absent  name=Synctime"          删除任务
   ansible  webserver -m  cron -a   "disabled=ture job='/usr/sbin/ntpdate 10.0.0.1'  name=Synctime"    如果不想要删除,我们可以注释掉(yes也可以)
   ansible  webserver -m  cron -a   "disabled=false  job='/usr/sbin/ntpdate 10.0.0.1'  name=Synctime"    当然,能禁用也能启用(no也行)
  • Yum
Yum:管理包
   ansible  ceshi  -m  yum -a  'name=hattpd  state=latest'   安装
   ansible  ceshi  -m  yum -a  'name=hattpd  state=absent'   删除
   state参数:用于指定软件包的状态 ,默认值为。present,表示确保软件包已经安装;除了present,其他可用值有 installed、latest、absent、removed,
   其中 installed 与present 等效,latest 表示安装 yum 中最新的版本,absent 和 removed 等效,表示删除对应的软件包
   例:
   ansible  ceshi  -m  yum -a  'name=httpd,mysql,nginx'   安装多个
   ansible  ceshi  -m  yum -a  'name=httpd,mysql,nginx    state=absent'    卸载多个
   ansible all -m  yum -a "list=installed"   查看所有已安装的包
   当然也可以安装单个的rpm包
   首先先将本地的包copy到目的地,然后yum安装
   ansible  ceshi  -m  copy -a  "src=/root/xxxx    dest=/tmp/xxxx"
   ansible  ceshi  -m  yum -a  "name=/tmp/xxxx"
   ansible  ceshi  -m  yum -a  "name=/tmp/xxxx   disable_gpg_check=yes"    忽略key的检查
   ansible  ceshi  -m  yum -a   'update_cache=yes'   有时我们yum安装时会因为安装缓存报错,我们可以用次命令更新缓存


注:另外,如果我们如果写的事playbook的话,可以这样写;将所需要安装的包写成变量的形式
yum: name={{ item }} state=installed
  with_items:
    - gcc
    - gcc-c++
    - autoconf
    - automake
    - make
    - pcre-devel
    - openssl-devel
  • Service
Service:管理服务
   ansible  ceshi  -m  service -a  'name=httpd  state=stopped'
   ansible  ceshi  -m  service -a  'name=httpd  state=started'
   ansible  ceshi  -m  service -a  'name=httpd  state=reloaded'
   ansible  ceshi  -m  service -a  'name=httpd  state=restarted'
  • User
User:管理用户
   ansible  test  -m  user -a  'name=user1   comment="test  user" uid=2019 home=/app/user1  group=root'
   ansible  test  -m  user -a  'name=sysuser1   system=yes   home=/app/sysuser1'             system:标明这是一个系统账号
   ansible  test  -m  user -a  'name=nginx   shell=/sbin/nologin   system=yes  home=/var/nginx/  groups=root,bin uid=80 comment="nignx  service"'   groups:副组
   ansible  test  -a  'getent  passwd  nginx '     查看我们的设置
   ansible  test  -m  user -a  'name=user1  state=absent   remove=yes'   删除用户及家目录等数据
  • Group
Group:管理组
   ansible  test  -m  group -a  "name=ceshigroup   system=yes"
   ansible  test  -m  group -a  "name=ceshigroup   system=absent"   删除组
  • Script
Script:远端执行本地脚本,我们不需要先将脚本copy过去,然后再shell执行
   ansible  test  -m  script -a  "add.sh"
  • Unarchive
unarchive:用于解压文件,模块包含如下选项:
  copy:在解压文件之前,是否先将文件复制到远程主机,默认为yes。若为no,则要求目标主机上压缩包必须存 
  在。
  creates:指定一个文件名,当该文件存在时,则解压指令不执行
  dest:远程主机上的一个路径,即文件解压的路径 
  grop:解压后的目录或文件的属组
  list_files:如果为yes,则会列出压缩包里的文件,默认为no,2.0版本新增的选项
  mode:解决后文件的权限
  src:如果copy为yes,则需要指定压缩文件的源路径 
  owner:解压后文件或目录的属主
  • Command/Shell
Command:ansible的默认模块
   # default module name for /usr/bin/ansible
   #module_name = command
   我们一般使用command后接命令之类的,但是注意,使用 command 模块在远程主机中执行命令时,不会经过 
   远程主机的 shell 处理,在使用 command 模块时,如果需要执行的命令中含有重定向、管道符等操作时, 
   这些符号也会失效,比如”<”, “>”, “|”, “;” 和 “&” 这些符号
   ansible all -m command -a "hostname"
Shell:shell 模块可以帮助我们在远程主机上执行命令。与 command 模块不同的是,shell 模块在远程主机 
   中执行命令时,会经过远程主机上的 /bin/sh 程序处理,所以它可以支持特殊符号
   它可以理解为command的升级版
  • Setup
Setup:用于收集远程主机的一些基本信息
     filter参数:用于进行条件过滤。如果设置,仅返回匹配过滤条件的信息
     例如我们需要取得目标主机的磁盘格式化后的信息;cpu的信息等
     ansible ceshi -m setup -a "filter=ansible_memory_mb"
     常用:
     ansible_all_ipv4_addresses:仅显示ipv4的信息。
     ansible_devices:仅显示磁盘设备信息。
     ansible_distribution:显示是什么系统,例:centos,suse等。
     ansible_distribution_major_version:显示是系统主版本。
     ansible_distribution_version:仅显示系统版本。
     ansible_machine:显示系统类型,例:32位,还是64位。
     ansible_eth0:仅显示eth0的信息。
     ansible_hostname:仅显示主机名。
     ansible_kernel:仅显示内核版本。
     ansible_lvm:显示lvm相关信息。
     ansible_memtotal_mb:显示系统总内存。
     ansible_memfree_mb:显示可用系统内存。
     ansible_memory_mb:详细显示内存情况。
     ansible_swaptotal_mb:显示总的swap内存。
     ansible_swapfree_mb:显示swap内存的可用内存。
     ansible_mounts:显示系统磁盘挂载情况。
     ansible_processor:显示cpu个数(具体显示每个cpu的型号)。
     ansible_processor_vcpus:显示cpu个数(只显示总的个数)。
   

好了,后续我会继续更新模块

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值