ansible的模块的使用(二)

ansible的命令格式和ansible的doc命令格式

ansible的命令格式以及后面携带的参数

  -a MODULE_ARGS, #模块参数
  -C, --check # 干跑,白跑
  -f FORKS, --forks=FORKS #指定并发,默认5个
  --list-hosts #列出主机
  -m MODULE_NAME# 模块名称
  --syntax-check #检查语法
  -k #密码


ansible的doc命令格式 ansible-doc [-l|-F|-s] [options] [-t <plugin type> ] [plugin] -j #以json的方式返回数据 ansible-doc -j -l, --list #列出所有的模块 ansible-doc -l -s, --snippet #以片段式显示模块信息 ansible-doc -s ping #查看ping模块的部分信息 #直接查看完整信息 ansible-doc ping #查看ping模块的所有信息 ansible-doc -l|wc -l 查看ansible一共有多少模块

 

ansible后面加模块使用,需要携带参数-m

1.ping模块

    判断网络通讯是否正常

ansible的底层是是是协议,所以才用ansible的ping模块的时候需要加上密码


ping模块的第一种使用:使用ssh的密码
    ansible 192.168.8.139 -m -ping -k

ping模块的第二种使用,先生成ssh的密钥对,然后使用ssh密钥对
   ssh-keygen   #生成ssh的密钥对
    ssh-copy-id root@192.168.8.139    #将139的公钥copy给ssh
    copy完成后就可以直接通过ansible的平模块来判断网络通信,不需要加-k来输密码

ping模块对多台主机的通信判断
    1. ansible ip1,ip2,ip3..... -m ping   
    2.ansible all -m ping   #ping所有的主机
    3.按分组来ping
            ansible 组名 -m ping  比如ansible web -m ping
            ansible 组名1,组名2..... -m ping  比如ansible web,db -m ping 
&表示交集,!表示差集,,表示并集
    ansible web,db -m ping #多个组的并集
    ansible 'web:&db' -m ping #多个组的交集
    ansible 'web:!db' -m ping #多个组的差集,在前面但是不在后面  

 2.command模块

  是一个执行linux的命令模块  

command模块
  command -a 后面跟的参数是放在“”里面的linux命令
  命令中有"<"', `">"', `"|"', `";"' and `"&"时,不能使用command 参数: - chdir 改变目录在执行命令 - creates 如果存在就不执行 - removes 如果存在就执行 示例: ansible web -m command -a "pwd" #获取当前的工作目录 ansible web -m command -a "ls" #查看当前工作目录下的文件 ansible web -m command -a "chdir=/tmp pwd" #切换目录并执行命令 ansible web -m command -a "creates=/tmp pwd" #因为tmp目录存在,pwd不会执行 ansible web -m command -a "creates=/tmp2 pwd" #因为tmp2不存在,pwd执行 ansible web -m command -a "removes=/tmp2 pwd" #因为tmp2不存在pwd不执行 ansible web -m command -a "removes=/tmp pwd" #因为tmp目录存在,pwd会执行 echo "密码" |passwd --stdin 用户名 #设置用户的密码

3.shell模块

shell模块和command模块差不多,都是用来执行linux命令的,所使用的的参数都是一样的,
不一样的是command对命令中有"<"', `">"', `"|"', `";"' and `"&"时,不能使用command
shell也可以用来执行shell脚本,shell脚本的后缀以sh结尾,比如a.sh就是一个shell脚本
参数:
- chdir   改变目录在执行命令
- creates  如果存在就不执行
- removes  如果存在就执行
示例:
ansible web -m shell -a "pwd"   #获取当前的工作目录
ansible web -m command -a "chdir=/tmp pwd" #切换目录并执行命令

4.script模块

  执行本地文件的一个模块

script模块:用来执行管控机的文件的一个模块

参数:ansible-doc -s script

    - chdir   改变目录在执行命令
    - creates  如果被控机文件存在就不执行
    - removes  如果被控机文件存在就执行
    存在不存在判断的是被管控机上的文件,
示例:
    ansible db -m script -a "/root/a.sh" #执行本地的文件,管控机的文件
    ansible db -m script -a "creates=/root/a.sh /root/a.sh" # 判断被控机上的文件是否存在,如果不存在,就执行,如果存在,就跳过
    ansible db -m script -a "creates=/tmp /root/a.sh" #判断被控机上的文件

5. copy模块

   用来将本地的文件copy到远程机器上面

copy模块:

参数:
    backup #创建一个备份文件,以时间戳结尾
    content # 直接往文件里面写内容
    group #文件的属组
    mode# 文件的权限  (读R4)(写W2)(执行x1)
    owner #文件的属主
    src #源地址()管控机的文件地址
    dest #copy的目标地址(必填)(被管控机的放文件的地址)

示例:
ansible web -m copy -a "src=/etc/fstab dest=/tmp/f" #复制本地文件到远程主机,并修改文件名,多次执行不会改变,因为checksum值是一样的
ansible web -m copy -a "src=a.sh dest=/tmp/a.sh backup=yes" #复制本地文件,并备份
ansible web -m copy -a "src=a.sh dest=/tmp/a.sh backup=yes group=libai mode=755"# 复制本地文件到远程主机,并指定属组和权限
ansible web -m copy -a "src=/etc/init.d dest=/tmp backup=yes group=libai mode=755" #复制本地的目录到远程主机,修改目录权限,则目录里面的文件也会跟着变更
ansible web -m copy -a "src=/etc/init.d/ dest=/tmp backup=yes group=libai mode=755" #复制本地目录下的所有文件,
ansible web -m copy -a "content='大弦嘈嘈如急雨,小弦切切如私语,嘈嘈切切错 杂弹,大珠小珠落玉盘' dest=/tmp/b" #直接往文件里面写内容,覆盖写,慎用

6. file模块

  对文件或者文件夹的操作

file模块
  对文件或者文件夹的操作
file的参数
    group # file的属组
    mode #file的权限
    owner #file的属主
    path #路径(必填)
	    state =link   #软连接
	    state =hard  #硬连接
    state #状态
	    directory #目录  state=directory 创建目录
	    file  无操作  state=file  时无操作
	    touch 空文件  state=touch时在path路径下创建空文件
	    absent 删除  #state=absent时   删除path路径的文件或者文件夹
	    link 软连接 创建软连接,需要由src属性来做软连接的路由
	    hard 硬链接   创建硬连接,需要由src属性来做软连接的路由
ansible web -m file -a "path=/libai state=directory owner=libai " #创建目录,并制定属主
ansible web -m file -a "path=/tmp/libai .txt state=touch mode=777" #创建文件,并指定权限
ansible web -m file -a "path=/tmp/cron src=/var/log/cron state=link" #创建软链接,链接的是自己的文件
ansible web -m file -a "path=/tmp/cron state=absent" # 删除软连接
ansible web -m file -a "path=/alex5 state=absent" #删除文件夹

7.fetch模块

   拉取远程主机的文件到管控机上面

fetch模块

参数
    dest 目标地址(必填)     管控机的地址
    src  源地址(必填)  被管控机的地址

示例:
ansible web -m fetch -a "src=/var/log/cron dest=/tmp" #拉取远程主机的文件,并以主机ip地址或者主机名为目录,并且保留了原来的目录结构

8.pip模块和yum模块

  pip模块是对python第三方软件的操作,域名模块是对linux的安装包的操作

查询ansible的yum模块的使用与参数
ansible-doc -s yum

查询ansible的pip模块的使用与参数
ansible-doc -s pip

yum模块
  对linux安装包的操作 参数: disablerepo #禁用某个源 enablerepo #启用某个源 name #包名 state #状态 install remove 示例: ansible web -m yum -a "name=python2-pip" #安装软件包 ansible web -m yum -a "name=python2-pip,redis" #安装多个包 ansible web -m yum -a "name='@Development Tools'" #安装包组 ansible web -m yum -a "name=nginx state=absent" #卸载 pip模块
  对python第三方包的操作 pip模块的参数 chdir #切换目录 name #包名 requirements #导出的文件 virtualenv #虚拟环境 state #状态 install remove

9.service模块

  操作服务的一个模块

service模块的参数:
    enabled #设置开机启动
    name #(必填)服务的名称
    state #状态 
        started  #开启
        stopped  #关闭
        restarted  #重启
        reloaded #平滑重启,只重启配置文件
    user #启动服务的用户
示例:
    ansible web -m service -a "name=redis state=started" #启动
    ansible web -m service -a "name=redis state=stopped" #关闭
    ansible web -m service -a "name=redis enabled=yes" #设置开机自启动
centos6和centos的一些操作

centos7启动
systemctl start redis
 
centos6启动
service redis start
 
centos7开机自启动
systemctl enable redis
 
centos6开机自启动
chkconfig redis on 


ps -ef|grep redis #查进程

进程查出来后,对应的数字介绍
    0 关机  1单用户 3命令行 5图形界面 6重启

ss -tunlp   #查端口
ss的参数介绍
    -t tcp
    -u udp
    -n 以端口形式显示
    -l 显示所有已经启动的端口
    -p 显示pid  

10.cron模块

  做计划任务的一个模块

cron模块:
    做计划任务的模块,添加时名字必须不同,不加名称为None
参数:
    day 天
    disabled 禁用crontab,相当于一个注释
    hour 小时
    job 任务
    minute 分钟
    month 月
    name 名字,描述信息
    user 用户
    weekday 周
添加时名字必须不同,不加名称为None

示例:

    *   *   *   *   *   job
    分      时     日       月     周       任务

    ansible web -m cron -a "minute=12 name=touchfile job='touch /tmp/xiaoqiang.txt'"# 创建  每天的第12分钟创建一个txt文件
    ansible web -m cron -a "name=touchfile state=absent" #删除
    ansible web -m cron -a "minute=12 name=touchfile2 job='touch /tmp/xiaoqiang.txt'disabled=yes" #注释
    ansible web -m cron -a "name=None state=absent" #删除名称为空的计划任务

11.user模块

user模块:
  对用户的操作
参数:
    group 属组
    groups 附加组
    home 设置家目录
    name 用户名
    remove 删除用户并删除用户的家目录
    shell 用户登录后的shell
    system 系统用户
    uid 用户的id
示例:
    ansible web -m user -a "name=libai shell=/sbin/nologin home=/opt/alex10 uid=3000 groups=root" #创建用户,并指定用户的shell,家目录,uid,以及附加组
    ansible web -m user -a "name=libai shell=/sbin/nologin home=/opt/alex11"
    ansible web -m user -a "name=libai system=yes" #创建系统用户
    ansible web -m user -a "name=libai state=absent" #删除用户,但不删除家目录
    ansible web -m user -a "name=libai state=absent remove=yes" # 删除用户并删除用户的家目录

12.group模块

  对用户组的一些操作

group模块
    对用户组的一些操作
参数:
    gid 组id
    system 系统组
    name 名称
    state
示例:
    ansible web -m group -a "name=libai system=yes gid=5000" 创建系统组
    ansible web -m group -a "name=libai" 创建普通的组
    ansible web -m group -a "name=libai state=absent" #删除组

13.setup模块

  用来收集远程主机的一些信息的模块

setup模块

参数 :

    filter  #过滤
        ansible web -m setup -a "filter =ansible_all_ipv4_addresses  " #只看远程ipv4 的信息
    
ansible web -m setup | more:可以查看的远程主机信息:

ansible_all_ipv4_addresses #所有的ipv4地址
ansible_all_ipv6_addresses #所有的ipv6地址
ansible_architecture #系统的架构
ansible_date_time #系统时间
ansible_default_ipv4 #默认的ipv4地址
	address ip地址
	alias 网卡名称
	broadcast 广播地址
	gateway 网关
	netmask 子网掩码
	network 网段
ansible_default_ipv6 #默认的ipv6地址
ansible_device_links #系统的磁盘信息
ansible_distribution #系统名称
ansible_distribution_file_variety #系统的基于公司
ansible_distribution_major_version #系统的主版本
ansible_distribution_version #系统的全部版本
ansible_dns #系统的dns 默认udp 端口53
ansible_domain #系统的域 ldap
ipv4 #ipv4地址
ansible_env #系统的环境
ansible_fqdn #系统的完整主机名
ansible_hostname #系统的简写主机名
ansible_kernel #系统的内核版本
ansible_machine #系统的架构
ansible_memtotal_mb #系统的内存
ansible_memory_mb #系统的内存使用情况
ansible_mounts #系统的挂载信息
ansible_os_family #系统家族
ansible_pkg_mgr #系统的包管理工具
ansible_processor #系统的cpu
ansible_processor_cores #每颗cpu的核数
ansible_processor_count #cpu的颗数
ansible_processor_vcpus #cpu的个数=cpu的颗数*每颗cpu的核数
ansible_python #系统python信息
ansible_python_version #系统python的版本
ansible_system #系统名字    

14.tags

  用来给任务打标签用的

tags
    用来给playbook任务打标签,tags打完标签后可以根据tags对应的name,进行tags对应的任务启动,而不会启动其他任务

示例:
- hosts: web
  tasks:
  - name: install
    yum: name=redis
  - name: copyfile
    copy: dest=/etc/redis.conf src=/etc/redis.conf
    tags: copy
  - name: start
    service: name=redis state=started
启动:
    ansible-playbook -t copy p7.yml  #只启动name:start的任务

15.handlers

handlers
  notify:handlers的name名字 notify用来触发handlers

  可以把handlers理解成另一种tasks,handlers是另一种’任务列表’,handlers中的任务会被tasks中的任务进行”调用”,

  但是,被”调用”并不意味着一定会执行,只有当tasks中的任务”真正执行”以后(真正的进行实际操作,造成了实际的改变),

  handlers中被调用的任务才会执行,如果tasks中的任务并没有做出任何实际的操作,那么handlers中的任务即使被’调用’,也并不会执行。

- hosts: web
  tasks:
  - name: install
    yum: name=redis
  - name: copyfile
    copy: dest=/etc/redis.conf src=/etc/redis.conf
    tags: copy
    notify: restart
  - name: start
    service: name=redis state=started
  handlers:
  - name: restart
    service: name=redis state=restarted
启动
ansible-playbook p7.yml  #启动任务后会有handlers操作,可以配合tags来操作

16.template

  动态的进行数据copy

在hosts文件中写入一个动态的信息,比如bind {{ansible_default_ipv4.address}}  动态监听ipv4 的ip地址

在playbook中使用copy模块的时候,会被替换成bind {{ansible_default_ipv4.address}}  而不会有具体的ip这时就可以用template模块,template模块就可以动态的获取ip地址


template模块和copy模块的用法一样,参数也一样
参数:
    backup #创建一个备份文件,以时间戳结尾
    content # 直接往文件里面写内容
    group #文件的属组
    mode# 文件的权限  (读R4)(写W2)(执行x1)
    owner #文件的属主
    src #源地址()管控机的文件地址
    dest #copy的目标地址(必填)(被管控机的放文件的地址)
示例:
- hosts: web
  tasks:
  - name: install
    yum: name=redis
  - name: copyfile
    template: dest=/etc/redis.conf src=/etc/redis.conf
    tags: copy
    notify: restart
  - name: start
    service: name=redis state=started
  handlers:
  - name: restart
    service: name=redis state=restarted


template的路径可以用绝对路径,也可以用相对路径

  

  

  

  

  

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

 

posted @ 2022-05-24 18:19  QV  阅读(229)  评论(0编辑  收藏  举报