ad-hoc(点对点)常用模块

ad-hoc(点对点)常用模块

1. 设置主机清单

  • 定义hosts 有3类:

    • Ex 1:未分组的主机,在任何组头之前指定

    • Ex 2:有组的主机,一组属于"webservers"组的主机

    • Ex 3:和数据库有关的,"dbservers"组中的数据库服务器集合

    • 在最底部写入远程主机的IP地址:

2. ping 模块,主机连通性测试

[root@ansibale~]#ansible all -m ping  尝试ping对方主机地址,由于是基于key验证,此时已经不需要输入密码。
192.168.34.102 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.34.103 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
192.168.34.105 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

3. Command 模块

  • 介绍

    命令模块接受命令名称,后面是空格分隔的列表参数。给定的命令将在所有选定的节点上执行,(此时command模块是默认模块,可忽略-m选项)。

    不会通过shell进行处理,比如$HOME和操作如"小于"<",">", “|”, “;”,"&"’ 工作(需要使用(shell)模块实现这些功能)。

  • 选项

    chdir      # 在执行命令之前,先切换到该目录
    creates  # 一个文件名,当这个文件存在,则该命令不执行,可以用来做判断
    removes       # 一个文件名,这个文件不存在,则该命令不执行,与creates相反的判断
    executable   # 切换shell来执行命令,需要使用命令的绝对路径(不常用,常用下面shell 模块)
    free_form    # 要执行的Linux指令,一般使用Ansible的-a参数代替(不常用,常用下面shell 模块)
    
  • 实例

    • ansible webs -m command -a ‘chdir=/data ls’

      [root@ansibaledata]#ansible webs -m command -a "chdir=/data ls"  可以切换到data目录下执行ls命令
      192.168.34.103 | CHANGED | rc=0 >>
      f1
       
      192.168.34.102 | CHANGED | rc=0 >>
      f2
      lost+found
      
    • ansible webs -m command -a ‘creates=/data/f1 touch /data/f2’

      [root@ansibaledata]#ansible webs -m command -a "creates=/data/f1  touch /data/f1"  #在webs组主机里,如果两个主机data下都有f1,就不会在data下创建f1
       [WARNING]: Consider using the file module with state=touch rather than running 'touch'.  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.34.102 | CHANGED | rc=0 >>
       
       
      192.168.34.103 | SUCCESS | rc=0 >>
      skipped, since /data/f1 exists
       
      [root@ansibaledata]#ansible webs -m command -a "chdir=/data ls" #IP102的主机data下没有f1,就会创建f1.
      192.168.34.102 | CHANGED | rc=0 >>
      f1
      f2
      lost+found
       
      192.168.34.103 | CHANGED | rc=0 >>
      f1
      
    • ansible webs -m command -a ‘removes=/data/f1 touch /data/f2’ data下有f1文件就会创建f2文件

      [root@ansibaledata]#ansible webs  -a "removes=/data/f1 touch  /data/f2"  #如果有f1文件就会进行创建f2
       [WARNING]: Consider using the file module with state=touch rather than running 'touch'.  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.34.102 | CHANGED | rc=0 >>
       
      192.168.34.103 | CHANGED | rc=0 >>
       
      [root@ansibaledata]#ansible webs -a "chdir=/data/ ls"  查询之前有f1,此时已经查到创建f2的结果
      192.168.34.102 | CHANGED | rc=0 >>
      f1
      f2
       
      192.168.34.103 | CHANGED | rc=0 >>
      f1
      f2
      

4. shell 模块

  • shell模块在远程主机上调用shell解释器运行命令,支持shell的各种功能,例如管道、echo等

  • 在配置文件中将模块进行修改,就不需要输入-m shell选项,修改位置:

    $ vim /etc/ansible/ansible.cfg
     
    module_name = shell
    
  • 实例:

    • ansible webs -m shell -a ‘cat /etc/passwd |grep root’

      [root@ansibaledata]#ansible webs -m shell -a "getent passwd | grep root"
      192.168.34.102 | CHANGED | rc=0 >>
      root:x:0:0:root:/root:/bin/bash
      operator:x:11:0:operator:/root:/sbin/nologin
       
      192.168.34.103 | CHANGED | rc=0 >>
      root:x:0:0:root:/root:/bin/bash
      operator:x:11:0:operator:/root:/sbin/nologin
       
      [root@ansibaledata]#ansible webs  -a "getent passwd | grep root"  由于是默认的shell模块,不需要加-m shell
      192.168.34.102 | CHANGED | rc=0 >>
      root:x:0:0:root:/root:/bin/bash
      operator:x:11:0:operator:/root:/sbin/nologin
       
      192.168.34.103 | CHANGED | rc=0 >>
      root:x:0:0:root:/root:/bin/bash
      operator:x:11:0:operator:/root:/sbin/nologin
      

5. script 模块

  • 在指定节点运行服务端的脚本

  • 示例,书写脚本:

    [root@Ansible ~]#vim test.sh
     
    #/bin/bash
     
    touch /data/test  #创建/data/test
    df -h  >> /data/test   #将df内容追加到/data/test文件中
    
  • 测试效果

    $ ansible webs -m script -a '/data/test.sh'             # 在远程被控制的机器执行脚本
    $ ansible webs -m command -a "chdir=/data ls"    # 查看文件生成
    $ ansible webs -m shell -a "cat /data/test              # 查看文件内容正确
    
  • 执行脚本:

    [root@ansibaledata]#ansible webs -m  script  -a "/data/test.sh" 通过脚本创建文件及追加文件内容
    192.168.34.102 | CHANGED => {
        "changed": true,
        "rc": 0,
        "stderr": "Shared connection to 192.168.34.102 closed.\r\n",
        "stderr_lines": [
            "Shared connection to 192.168.34.102 closed."
        ],
        "stdout": "",
        "stdout_lines": []
    }
    192.168.34.103 | CHANGED => {
        "changed": true,
        "rc": 0,
        "stderr": "Shared connection to 192.168.34.103 closed.\r\n",
        "stderr_lines": [
            "Shared connection to 192.168.34.103 closed."
        ],
        "stdout": "",
        "stdout_lines": []
    }
    [root@ansibaledata]#ansible webs -m command -a "chdir=/data/ cat test"  查询test文件内容
    192.168.34.102 | CHANGED | rc=0 >>
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/sda2        96G  913M   91G   1% /
    tmpfs           743M     0  743M   0% /dev/shm
    /dev/sda1       976M   33M  892M   4% /boot
    /dev/sda3        48G   52M   46G   1% /data
     
    192.168.34.103 | CHANGED | rc=0 >>
    Filesystem      Size  Used Avail Use% Mounted on
    /dev/sda2       100G  1.2G   99G   2% /
    devtmpfs        791M     0  791M   0% /dev
    tmpfs           802M     0  802M   0% /dev/shm
    tmpfs           802M  9.6M  792M   2% /run
    tmpfs           802M     0  802M   0% /sys/fs/cgroup
    /dev/sda3        50G   33M   50G   1% /data
    /dev/sda1      1014M  127M  888M  13% /boot
    tmpfs           161M     0  161M   0% /run/user/0
    

6. copy模块

  • copy:复制文件到远程主机,可以改权限等

    • 用法:

      • 复制文件
        • -a "src= dest= "
      • 给定内容生成文件
        • -a "content= dest= "
    • 相关选项如下:

      src:源,被复制到远程主机的本地文件,可以是绝对路径,也可以是相对路径。如果路径是一个目录,它将递归复制。在这种情况下,如果路径使用“/”来结尾,则只复制目录里的内容,如果没有使用“/”来结尾,则包含目录在内的整个内容全部复制,类似于rsync。
      dest:目标,必选项。要将源文件复制到的远程主机的绝对路径,如果源文件是一个目录,那么该路径也必须是个目录
      backup:被管理的远程主机已经有文件了,在覆盖之前,将源文件备份,备份文件包含时间信息。有两个选项:yes|no
      content:用于替代“src”,可以直接设定指定文件的值
      directory_mode:递归设定目录的权限,默认为系统默认权限
      force:如果目标主机包含该文件,但内容不同,如果设置为yes,则强制覆盖,如果为no,则只有当目标主机的目标位置不存在该文件时,才复制。默认为yes
      others:所有的file模块里的选项都可以在这里使用
      
    • 示例:

    [root@ansibaledata]#ansible all -m copy -a "src=/etc/issue dest=/data/fstab owner=nobody mode=600 backup=yes"  将src(源)/etc/issue文件复制到dest(目标)的data目录下,起名叫fstab,所有者为nobody,权限为600,如果有此文件,进行备份。
    192.168.34.102 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        },
        "backup_file": "/data/fstab.23704.2019-11-04@15:19:19~",
        "changed": true,
        "checksum": "5c76e3b565c91e21bee303f15c728c71e6b39540",
        "dest": "/data/fstab",
        "gid": 0,
        "group": "root",
        "md5sum": "f078fe086dfc22f64b5dca2e1b95de2c",
        "mode": "0600",
        "owner": "nobody",
        "size": 23,
        "src": "/root/.ansible/tmp/ansible-tmp-1572851957.62-86571596767189/source",
        "state": "file",
        "uid": 99
    }
    192.168.34.103 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        },
        "backup_file": "/data/fstab.21512.2019-11-04@23:19:19~",
        "changed": true,
        "checksum": "5c76e3b565c91e21bee303f15c728c71e6b39540",
        "dest": "/data/fstab",
        "gid": 0,
        "group": "root",
        "md5sum": "f078fe086dfc22f64b5dca2e1b95de2c",
        "mode": "0600",
        "owner": "nobody",
        "size": 23,
        "src": "/root/.ansible/tmp/ansible-tmp-1572851957.67-211672525698804/source",
        "state": "file",
        "uid": 99
    }
    192.168.34.105 | CHANGED => {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        },
        "backup_file": "/data/fstab.5430.2019-11-04@15:19:17~",
        "changed": true,
        "checksum": "5c76e3b565c91e21bee303f15c728c71e6b39540",
        "dest": "/data/fstab",
        "gid": 0,
        "group": "root",
        "md5sum": "f078fe086dfc22f64b5dca2e1b95de2c",
        "mode": "0600",
        "owner": "nobody",
        "size": 23,
        "src": "/root/.ansible/tmp/ansible-tmp-1572851957.69-65920362365645/source",
        "state": "file",
        "uid": 99
    }
    [root@ansibaledata]#ansible all -m shell -a "ls -l /data/"   查看复制后的结果。
    192.168.34.102 | CHANGED | rc=0 >>
    total 12
    -rw-r--r-- 1 root   root   0 Nov  4 14:35 f1
    -rw-r--r-- 1 root   root   0 Nov  4 14:35 f2
    -rw------- 1 nobody root  23 Nov  4 15:19 fstab
    -rw------- 1 nobody root 595 Nov  4 15:16 fstab.23704.2019-11-04@15:19:19~  此文件为备份文件
    -rw-r--r-- 1 root   root 224 Nov  4 14:58 test
     
    192.168.34.105 | CHANGED | rc=0 >>
    total 20
    -rw------- 1 nobody root   23 Nov  4 15:19 fstab
    -rw------- 1 nobody root  595 Nov  4 15:16 fstab.5430.2019-11-04@15:19:17~ 此文件为备份文件
    drwxr-xr-x 7 root   root 4096 Oct 29 15:51 fulliso
    drwxr-xr-x 4 root   root 4096 Oct 29 14:17 iso
    -rw-r--r-- 1 root   root 1400 Oct 30 20:14 ks7_mini.cfg
     
    192.168.34.103 | CHANGED | rc=0 >>
    total 12
    -rw-r--r-- 1 root   root   0 Nov  4 22:35 f1
    -rw-r--r-- 1 root   root   0 Nov  4 22:35 f2
    -rw------- 1 nobody root  23 Nov  4 23:19 fstab
    -rw------- 1 nobody root 595 Nov  4 23:16 fstab.21512.2019-11-04@23:19:19~ 此文件为备份文件
    -rw-r--r-- 1 root   root 413 Nov  4 22:58 test
    
    • copy复制目录:

      [root@ansibaledata]#ansible webs -m copy -a "src=/data dest=/data/newdata"  复制/data/下的目录,并起名叫newdata
      192.168.34.102 | CHANGED => {
          "changed": true,
          "dest": "/data/newdata/",
          "src": "/data"
      }
      192.168.34.103 | CHANGED => {
          "changed": true,
          "dest": "/data/newdata/",
          "src": "/data"
      }
      [root@ansibaledata]#ansible webs -m shell -a "ls -l /data/"
      192.168.34.102 | CHANGED | rc=0 >>
      total 16
      drwxr-xr-x 3 root   root 4096 Nov  4 15:27 newdata
      -rw-r--r-- 1 root   root  224 Nov  4 14:58 test
       
      192.168.34.103 | CHANGED | rc=0 >>
      total 12
      drwxr-xr-x 3 root   root  18 Nov  4 23:27 newdata
      -rw-r--r-- 1 root   root 413 Nov  4 22:58 test
      
    • content用法:

      [root@ansibaledata]#ansible all -m copy -a 'content="[test]\nbaseurl=file:///mnt\ngpgcheck=0" dest=/etc/yum.repos.d/test.repo' 对被控制端进行新建yum源,并起名叫test.repo
      [root@ansibaledata]#ansible all -m shell -a "ls /etc/yum.repos.d/" 查看新建的yum源文件
      192.168.34.103 | CHANGED | rc=0 >>
      test.repo
       
      192.168.34.102 | CHANGED | rc=0 >>
      test.repo
       
      192.168.34.105 | CHANGED | rc=0 >>
      base.repo
      test.repo
       
      [root@ansibaledata]#ansible all -m shell -a "cat /etc/yum.repos.d/test.repo"  查询新建的yum源文件内容
      192.168.34.102 | CHANGED | rc=0 >>
      [test]
      baseurl=file:///mnt
      gpgcheck=0
       
      192.168.34.103 | CHANGED | rc=0 >>
      [test]
      baseurl=file:///mnt
      gpgcheck=0
       
      192.168.34.105 | CHANGED | rc=0 >>
      [test]
      baseurl=file:///mnt
      gpgcheck=0
      

7. fetch 模块

  • 介绍

    • 从远程某主机获取文件到本地

    • dest:用来存放文件的目录,例如存放目录为backup,源文件名称为/etc/profile

      在主机pythonserver中,那么保存为/backup/pythonserver/etc/profile

    • Src:在远程拉取的文件,并且必须是一个file,不能是目录

    • 注意:从远程获取到本地的文件,会保存到以远程主机的IP 为名的目录中,且保存目录结构

  • 示例:

    [root@ansibaledata]#ansible webs -m fetch -a "src=/etc/hosts  dest=/data"  将远处的/etc/hosts文件复制到本机的/data/目录下
    [root@ansibaledata]#ll 复制的文件目录以IP地址形式显示
    total 48
    -rw-r---w- 1 root root  90 Nov  3 22:05 1
    drwxr-xr-x 3 root root  17 Nov  4 15:43 192.168.34.102
    drwxr-xr-x 3 root root  17 Nov  4 15:43 192.168.34.103
    [root@ansibaledata]#tree  查看复制过来的文件
    .
    ├── 1
    ├── 192.168.34.102
    │   └── etc
    │       └── hosts
    ├── 192.168.34.103
    │   └── etc
    │       └── hosts
    
    [root@ansibaledata]#ansible webs -m shell -a "tar -cvf /root/data.tar /data"  可以将data目录下的文件打包到root下起名叫data.tar  打包之后就可以用fetch复制此打包文件。
     [WARNING]: Consider using the unarchive module rather than running 'tar'.  If you need to use command because
    unarchive 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.
     
    [root@ansibaledata]#ansible webs -m shell -a "ls -l /root/" 查看root下的打包文件
    192.168.34.102 | CHANGED | rc=0 >>
    total 68
    -rw-------. 1 root root  1125 Nov  2 23:04 anaconda-ks.cfg
    -rw-r--r--  1 root root 30720 Nov  4 15:50 data.tar
    -rw-r--r--. 1 root root 16911 Nov  2 23:04 install.log
    -rw-r--r--. 1 root root  5820 Nov  2 23:03 install.log.syslog
    -rw-r--r--  1 root root   420 Nov  4 14:56 test
     
    192.168.34.103 | CHANGED | rc=0 >>
    total 44
    -rw-------. 1 root root  1636 Nov  3 07:08 anaconda-ks.cfg
    -rw-r--r--  1 root root 30720 Nov  4 23:50 data.tar
    -rw-------. 1 root root  1385 Nov  3 07:08 original-ks.cfg
    -rw-r--r--  1 root root   740 Nov  4 22:56 test
    

8. file 模块

  • 设置文件属性

    • 创建目录:-a “path= state=directory
    • 创建链接文件:-a “path= src= state=link
    • 删除文件:-a “path= state=absent
  • 实例:

    • ansible webs -m file -a “path=/data/f4 state=directory” 在被控制端,创建f4 目录

      force:需要在两种情况下强制创建软链接,一种是源文件不存在,但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no
      group:定义文件/目录的属组 mode:定义文件/目录的权限
      owner:定义文件/目录的属主 path:必选项,定义文件/目录的路径
      recurse:递归设置文件的属性,只对目录有效 src:被链接的源文件路径,只应用于state=link的情况
      dest:被链接到的路径,只应用于state=link的情况
      state=:
       directory:如果目录不存在,就创建目录
       file:即使文件不存在,也不会被创建
       link:创建软链接
       hard:创建硬链接
       touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间
       absent:删除目录、文件或者取消链接文件
      
    • ansible webs -m command -a “chdir=/data ls” 查看/data 目录

      [root@ansibaledata]#ansible webs -m file -a "path=/data/f4 state=directory"  在data目录下新建f4目录
      [root@ansibaledata]#ansible webs -m command -a "chdir=/data ls -l"  查询新建的结果
      192.168.34.102 | CHANGED | rc=0 >>
      total 20
      -rw-r--r-- 1 root   root    0 Nov  4 14:35 f1
      -rw-r--r-- 1 root   root    0 Nov  4 14:35 f2
      drwxr-xr-x 2 root   root 4096 Nov  4 16:12 f4   新建的f4目录
       
       
      192.168.34.103 | CHANGED | rc=0 >>
      total 12
      -rw-r--r-- 1 root   root   0 Nov  4 22:35 f1
      -rw-r--r-- 1 root   root   0 Nov  4 22:35 f2
      drwxr-xr-x 2 root   root   6 Nov  5 00:12 f4  新建的f4目录
      
  • 示例:

    • 删除目录下的文件

      • ansible webs -m file -a “path=/data/f4 state=absent” 删除data目录下的f4

      • ansible webs -m file -a “path=/data/ state=absent” 删除data目录下的文件

        [root@ansibaledata]#ansible webs -m file -a "path=/data/f4 state=absent"  删除data目录下的f4
        [root@ansibaledata]#ansible webs -m command -a "chdir=/data ls -l"  查看此时data目录下已经没有f4目录
        192.168.34.102 | CHANGED | rc=0 >>
        total 16
        -rw-r--r-- 1 root   root    0 Nov  4 14:35 f1
        -rw-r--r-- 1 root   root    0 Nov  4 14:35 f2
         
        192.168.34.103 | CHANGED | rc=0 >>
        total 12
        -rw-r--r-- 1 root   root   0 Nov  4 22:35 f1
        -rw-r--r-- 1 root   root   0 Nov  4 22:35 f2
        
  • 示例;

    • 创建软链接和硬链接

      • ansible webs -m file -a “src=/data/fstab path=/data/fstab.link state=link” 将/data/fstab 创建软链接为/data/fstab.link

      • ansible webs -m file -a “src=/data/fstab path=/data/fstab1.link state=hard” 将/data/fstab创建硬链接为/data/fstab1.link

        • 1、创建软链接:

          [root@ansibaledata]#ansible webs -m file -a "src=/data/fstab path=/data/fstab.link state=link" 
          192.168.34.102 | CHANGED => {
          [root@ansibaledata]#ansible webs -m shell -a "ls -l /data/"
          192.168.34.102 | CHANGED | rc=0 >>
          total 16
           
          lrwxrwxrwx 1 root   root   11 Nov  4 16:24 fstab.link -> /data/fstab  创建成功的软链接
           
          192.168.34.103 | CHANGED | rc=0 >>
          total 12
           
          lrwxrwxrwx 1 root   root  11 Nov  5 00:24 fstab.link -> /data/fstab  创建成功的软链接
          
        • 2、创建硬链接:

          [root@ansibaledata]#ansible webs -m file -a "src=/data/fstab path=/data/fstab1.link state=hard"
          [root@ansibaledata]#ansible webs -m shell -a "ls -l /data/"
          192.168.34.102 | CHANGED | rc=0 >>
          total 20
          -rw------- 2 nobody root   23 Nov  4 15:19 fstab
          -rw------- 2 nobody root   23 Nov  4 15:19 fstab1.link
           
          192.168.34.103 | CHANGED | rc=0 >>
          total 16
          -rw------- 2 nobody root  23 Nov  4 23:19 fstab
          -rw------- 2 nobody root  23 Nov  4 23:19 fstab1.link
          

9. hostname:管理主机名

  • 示例:

    • ansible 192.168.34.102 -m hostname -a “name=centos102” 修改为centos102

    • ansible 192.168.34.102 -a “hostname” 查看当前修改后的主机名

      [root@ansibaledata]#ansible 192.168.34.102  -m hostname -a "name=centos102"
      192.168.34.102 | CHANGED => {
          "ansible_facts": {
              "ansible_domain": "",
              "ansible_fqdn": "centos102",
              "ansible_hostname": "centos102",
              "ansible_nodename": "centos102",
              "discovered_interpreter_python": "/usr/bin/python"
          },
          "changed": true,
          "name": "centos102"
      }
      [root@ansibaledata]#ansible 192.168.34.102 -a "hostname"
      192.168.34.102 | CHANGED | rc=0 >>
      centos102
      

10. cron 模块

  • 管理cron计划任务;-a “”: 设置管理节点生成定时任务

    • 选项:

      ① action:
      cron backup=   #如果设置,创建一个crontab备份 【yes|no】
      cron_file=    #如果指定, 使用这个文件cron.d,而不是单个用户crontab
        day=     #日应该运行的工作( 1-31, *, */2, )
        hour=   # 小时 ( 0-23, *, */2, )
        minute=    #分钟( 0-59, *, */2, )
        month=     #月( 1-12, *, /2, )
        weekday   # 周 ( 0-6 for Sunday-Saturday,, )
        job=       #指明运行的命令是什么
        name=   #定时任务描述
        reboot    # 任务在重启时运行,不建议使用,建议使用special_time
        special_time   #特殊的时间范围,参数:reboot(重启时),annually(每年),monthly(每月),weekly(每周),daily(每天),hourly(每小时)
        state   #指定状态,present表示添加定时任务,也是默认设置,absent表示删除定时任务
        user    #以哪个用户的身份执行 
      
    • 示例:

      • 开启计划任务

        [root@ansibaledata]#ansible webs -m  cron -a  "minute=*/5 weekday=0,6 job="/usr/sbin/update 192.168.34.101 &> /dev/null" name=synctime"  只在周六周日每五分钟将时间与192.168.34.101进行同步,起名叫synctime
        [root@ansibaledata]#ansible webs -a "crontab -l"查询当前执行的任务结果。
        
      • 禁用计划任务:

        [root@ansibaledata]#ansible webs -m  cron -a  "minute=*/5 weekday=0,6 job="/usr/sbin/update 192.168.34.101 &> /dev/null" name=synctime disabled=ture"  只在周六周日每五分钟将时间与192.168.34.101进行同步计划任务进行禁用
        [root@ansibaledata]#ansible webs -a "crontab -l"查询当前执行的任务结果。
        
    • 示例:

      • 在远程主机上,定义每5分钟,清空一次防火墙

        $ ansible web -m cron -a "name='Clear the iptable' minute=*/5 job='/sbin/iptables -F'"
        
      • ansible web -m shell -a “crontab -l” 查看

        [root@ansibaledata]#ansible webs -m cron -a "name='Clean the iptable' minute=*/5 job='/sbin/iptables -F &> /dev/full'"
        192.168.34.102 | CHANGED => {
            "ansible_facts": {
                "discovered_interpreter_python": "/usr/bin/python"
            },
            "changed": true,
            "envs": [],
            "jobs": [
                "Clean the iptable"
            ]
        }
        192.168.34.103 | CHANGED => {
            "ansible_facts": {
                "discovered_interpreter_python": "/usr/bin/python"
            },
            "changed": true,
            "envs": [],
            "jobs": [
                "Clean the iptable"
            ]
        }
        [root@ansibaledata]#ansible webs -a "crontab -l"
        192.168.34.102 | CHANGED | rc=0 >>
        #Ansible: Clean the iptable
        */5 * * * * /sbin/iptables -F
         
        192.168.34.103 | CHANGED | rc=0 >>
        #Ansible: Clean the iptable
        */5 * * * * /sbin/iptables -F
        
      • 启动当前的计划任务

        [root@ansibaledata]#ansible webs -m cron -a "name='Clean the iptable' minute=*/5 job='/sbin/iptables -F disabled=false'" 开启当前的计划任务,起名叫Clean the  iptable
        192.168.34.102 | CHANGED => {
            "ansible_facts": {
                "discovered_interpreter_python": "/usr/bin/python"
            },
            "changed": true,
            "envs": [],
            "jobs": [
                "Clean the iptable"
            ]
        }
        192.168.34.103 | CHANGED => {
            "ansible_facts": {
                "discovered_interpreter_python": "/usr/bin/python"
            },
            "changed": true,
            "envs": [],
            "jobs": [
                "Clean the iptable"
            ]
        }
        [root@ansibaledata]#ansible webs -a "crontab -l"
        192.168.34.102 | CHANGED | rc=0 >>
        #Ansible: Clean the iptable
        */5 * * * * /sbin/iptables -F disabled=false  开启结果
         
        192.168.34.103 | CHANGED | rc=0 >>
        #Ansible: Clean the iptable
        */5 * * * * /sbin/iptables -F disabled=false
        
      • 删除计划任务:

        [root@ansibaledata]#ansible webs -m cron -a "name='Clean the iptable' state=absent"  删除计划任务,只需要将对应的计划任务名称删掉即可。
        192.168.34.102 | CHANGED => {
            "ansible_facts": {
                "discovered_interpreter_python": "/usr/bin/python"
            },
            "changed": true,
            "envs": [],
            "jobs": []
        }
        192.168.34.103 | CHANGED => {
            "ansible_facts": {
                "discovered_interpreter_python": "/usr/bin/python"
            },
            "changed": true,
            "envs": [],
            "jobs": []
        }
        [root@ansibaledata]#ansible webs -a "crontab -l"  查询结果,此时已经没有计划任务
        192.168.34.102 | CHANGED | rc=0 >>
         
         
        192.168.34.103 | CHANGED | rc=0 >>
        

11. yum 模块

  • 选项

    conf_file    #设定远程yum安装时所依赖的配置文件。如配置文件没有在默认的位置。
    disable_gpg_check   #是否禁止GPG checking,只用于`present‘ or `latest’。
    disablerepo   #临时禁止使用yum库。 只用于安装或更新时。
    enablerepo   #临时使用的yum库。只用于安装或更新时。
    name=    #所安装的包的名称
    state=     #present安装, latest安装最新的, absent 卸载软件。
    update_cache    #强制更新yum的缓存。
    
  • 示例

    • 安装dstat 包,忽略gpg_check

      $ ansible webs -m yum -a "name=dstat "
      
    • 卸载dstat 包

      $ ansible webs -m yum -a "name=dstat state=absent"
      
    • 安装多个包

      $ ansible webs  -m yum -a "name=httpd,vsftpd,memacahe"
      
    • 卸载多个包;

      $ ansible webs  -m yum -a "name=httpd,vsftpd,memacahe,state=absent"
      

12. service 模块

  • 服务程序管理

    • 选项

      arguments   #命令行提供额外的参数
      enabled   #设置开机启动。
      name=     #服务名称
      runlevel    #开机启动的级别,一般不用指定。
      sleep    #在重启服务的过程中,是否等待。如在服务关闭以后等待2秒再启动。
      state     #started启动服务, stopped停止服务, restarted重启服务, reloaded重载配置
      
    • 实例

      • 远程安装httpd服务

        $ ansible all -m yum -a "name=httpd"
        
      • 远程开启httpd服务:

        [root@ansibaledata]#ansible all -m yum -a "name=httpd"  安装http服务
        [root@ansibaledata]#ansible all -m service -a "name=httpd state=started"  打开http服务端口
        [root@ansibaledata]#ansible all -a "ss -nlt"  查看当前的服务端口
        192.168.34.102 | CHANGED | rc=0 >>
        State      Recv-Q Send-Q        Local Address:Port          Peer Address:Port
        <strong>LISTEN     0      128                      :::80                      :::*    
        </strong>LISTEN     0      128                      :::22                      :::*    
        LISTEN     0      128                       *:22                       *:*    
        LISTEN     0      100                     ::1:25                      :::*    
        LISTEN     0      100               127.0.0.1:25                       *:*    
         
        192.168.34.103 | CHANGED | rc=0 >>
        State      Recv-Q Send-Q Local Address:Port               Peer Address:Port             
        LISTEN     0      128          *:22                       *:*                 
        LISTEN     0      100    127.0.0.1:25                       *:*                 
        <strong>LISTEN     0      128         :::80                      :::* </strong>                
        LISTEN     0      128         :::22                      :::*                 
        LISTEN     0      100        ::1:25                      :::*                 
         
        192.168.34.105 | CHANGED | rc=0 >>
        State      Recv-Q Send-Q        Local Address:Port          Peer Address:Port
        LISTEN     0      128               127.0.0.1:6010                     *:*    
        LISTEN     0      128                     ::1:6010                    :::*    
        LISTEN     0      128                      :::111                     :::*    
        LISTEN     0      128                       *:111                      *:*    
        <strong>LISTEN     0      128                      :::80                      :::*   </strong> 
        LISTEN     0      128                       *:44404                    *:*    
        LISTEN     0      128                      :::58453                   :::*    
        LISTEN     0      128                      :::22                      :::*    
        LISTEN     0      128                       *:22                       *:*    
        LISTEN     0      64                       :::23                      :::*    
        LISTEN     0      128               127.0.0.1:631                      *:*    
        LISTEN     0      128                     ::1:631                     :::*    
        LISTEN     0      100                     ::1:25                      :::*    
        LISTEN     0      100               127.0.0.1:25                       *:* 
        
      • 远程关闭服务:

        [root@ansibaledata]#ansible all -m service -a "name=httpd state=stopped"  关闭http服务
        [root@ansibaledata]#ansible all -a "ss -nlt"  查此时的80端口已经没有了
        192.168.34.102 | CHANGED | rc=0 >>
        State      Recv-Q Send-Q        Local Address:Port          Peer Address:Port
        LISTEN     0      128                      :::22                      :::*    
        LISTEN     0      128                       *:22                       *:*    
        LISTEN     0      100                     ::1:25                      :::*    
        LISTEN     0      100               127.0.0.1:25                       *:*    
         
        192.168.34.103 | CHANGED | rc=0 >>
        State      Recv-Q Send-Q Local Address:Port               Peer Address:Port             
        LISTEN     0      128          *:22                       *:*                 
        LISTEN     0      100    127.0.0.1:25                       *:*                 
        LISTEN     0      128         :::22                      :::*                 
        LISTEN     0      100        ::1:25                      :::*                 
         
        192.168.34.105 | CHANGED | rc=0 >>
        State      Recv-Q Send-Q        Local Address:Port          Peer Address:Port
        LISTEN     0      128               127.0.0.1:6010                     *:*    
        LISTEN     0      128                     ::1:6010                    :::*    
        LISTEN     0      128                      :::111                     :::*    
        LISTEN     0      128                       *:111                      *:*    
        LISTEN     0      128                       *:44404                    *:*    
        LISTEN     0      128                      :::58453                   :::*    
        LISTEN     0      128                      :::22                      :::*    
        LISTEN     0      128                       *:22                       *:*    
        LISTEN     0      64                       :::23                      :::*    
        LISTEN     0      128               127.0.0.1:631                      *:*    
        LISTEN     0      128                     ::1:631                     :::*    
        LISTEN     0      100                     ::1:25                      :::*    
        LISTEN     0      100               127.0.0.1:25                       *:*    
        

13. user 模块

  • 用户模块,管理用户帐号

    • 选项

      comment        # 用户的描述信息
      createhome    # 是否创建家目录
      force      # 在使用state=absent是, 行为与userdel -force一致.
      group     # 指定基本组
      groups   # 指定附加组,如果指定为(groups=)表示删除所有组
      home     # 指定用户家目录
      move_home    # 如果设置为home=时, 试图将用户主目录移动到指定的目录
      name     # 指定用户名
      non_unique     # 该选项允许改变非唯一的用户ID值
      password       # 指定用户密码,若指定的是明文密码,是不能用的,需用md5加密过后的密码
      remove   # 在使用state=absent时, 行为是与userdel -remove一致
      shell      # 指定默认shell
      state      # 设置帐号状态,不指定为创建,指定值为absent表示删除
      system  # 当创建一个用户,设置这个用户是系统用户。这个设置不能更改现有用户
      uid     # 指定用户的uid
      update_password    # 更新用户密码
      
    • 示例:

      $ ansible all -m user -a 'name=test comment="test user" uid=200 home=/data/testhome group=root groups=bin,nobody shell=/sbin/nologin'  创建一个test用户的详细信息
      $ ansible all -m user -a "name=test state=absent" 删除用户信息,但是不会删除用户家目录的文件信息
      $ ansible all -a "getent passwd test"  查看当前创建的用户信息
      $ ansiable all -a "name=test state=absent remove=yes"  跟上后面的remove=yes就会删除家目录信息
      
    • 测试效果

      [root@ansibaledata]#ansible all -m user -a 'name=test comment="test user" uid=200 home=/data/testhome group=root groups=bin,nobody shell=/sbin/nologin'  创建一个test用户的详细信息
       
      [root@ansibaledata]#ansible all -a "getent passwd test"
      192.168.34.102 | CHANGED | rc=0 >>
      test:x:200:0:test user:/data/testhome:/sbin/nologin
       
      192.168.34.103 | CHANGED | rc=0 >>
      test:x:200:0:test user:/data/testhome:/sbin/nologin
       
      192.168.34.105 | CHANGED | rc=0 >>
      test:x:200:0:test user:/data/testhome:/sbin/nologin
       
      [root@ansibaledata]#ansible all -m user -a "name=test state=absent"  删除用户文件信息
      [root@ansibaledata]#ansible all -a "getent passwd test"   查看用户文件信息
      192.168.34.102 | FAILED | rc=2 >>
      non-zero return code
       
      192.168.34.103 | FAILED | rc=2 >>
      non-zero return code
       
      192.168.34.105 | FAILED | rc=2 >>
      non-zero return code
      

14. group模块

  • 选项

    gid         # 设置组的GID号
    name=  # 管理组的名称
    state     # 指定组状态,默认为创建,设置值为absent为删除
    system  # 设置值为yes,表示为创建系统组
    
  • 示例:

     $ ansible webs -m group -a "name=testgroup system=yes“  创建系统组
     $ ansible webs -m group -a "name=testgroup state=absent"  删除组
    

15. setup 模块

  • 查机器的所有facts信息

  • 介绍

    • facts 组件是Ansible 用于采集被管机器设备信息的一个功能,我们可以使用setup 模块查机器的所有facts信息,可以使用filter来查看指定信息。整个facts信息被包装在一个JSON格式的数据结构中,ansible_facts是最上层的值。
    • acts就是变量,内建变量每个主机的各种信息,cpu颗数、内存大小等。会存在facts中的某个变量中。调用后返回很多对应主机的信息,在后面的操作中可以根据不同的信息来做不同的操作。如redhat系列用yum安装,而debian系列用apt来安装软件。
    • setup模块,主要用于获取主机信息,在playbooks里经常会用到的一个参数gather_facts就与该模块相关。
    • setup模块下经常使用的一个参数是filter 参数,查询的是全部信息,很多,filter 相当于匹配筛选。
  • 示例:

    • 查询当前机器的全部信息:

      $ ansible  192.168.34.103  -m setup
      

    • 查看当前主机的总内存大小:

      $ ansible all -m setup  -a "filter=ansible_memtotal_mb"  其中filter是过滤的含义
      

    • 查看每个主机的centos版本号

      $ ansible all -m setup -a "filter=ansible_distribution_major_version"
      

me=testgroup state=absent" 删除组




### 15. setup 模块

- **查机器的所有facts信息**

- 介绍

- facts 组件是Ansible 用于采集**被管机器设备信息**的一个功能,我们可以使用**setup 模块查机器的所有facts信息**,可以使用filter来查看指定信息。整个facts信息被包装在一个JSON格式的数据结构中,ansible_facts是最上层的值。
- acts就是变量,**内建变量** 。**每个主机的各种信息,cpu颗数、内存大小等**。会存在facts中的某个变量中。调用后返回很多对应主机的信息,在后面的操作中可以根据不同的信息来做不同的操作。如redhat系列用yum安装,而debian系列用apt来安装软件。
- setup模块,主要用于**获取主机信息**,在playbooks里经常会用到的一个参数gather_facts就与该模块相关。
- setup模块下经常使用的一个参数是**filter 参数**,查询的是全部信息,很多,filter 相当于匹配筛选。

- 示例:

- 查询当前机器的全部信息:

  ```shell
  $ ansible  192.168.34.103  -m setup
  ```

  [外链图片转存中...(img-P3uezMNo-1625467423229)]

- 查看当前主机的总内存大小:

  ```shell
  $ ansible all -m setup  -a "filter=ansible_memtotal_mb"  其中filter是过滤的含义
  ```

  [外链图片转存中...(img-KPpNgwzl-1625467423233)]

- 查看每个主机的centos版本号

  ```shell
  $ ansible all -m setup -a "filter=ansible_distribution_major_version"
  ```

  [外链图片转存中...(img-wZMgY6zW-1625467423237)]





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值