• Ansible Module


    Ansible Ad-hoc模式常用模块

    ansible-doc 常用命令

      1 # ansible-doc -h
      2 Usage: ansible-doc [-l|-F|-s] [options] [-t <plugin type> ] [plugin]
      3 
      4 -j  以json格式显示所有模块信息
      5 -l  列出所有的模块
      6 -s  查看模块常用参数
      7 # 直接跟模块名,显示模块所有信息
      8 
      9 [root@ansible ~]# ansible-doc -j
     10 [root@ansible ~]# ansible-doc -l
     11 [root@ansible ~]# ansible-doc -l |wc -l   #统计所有模块个数,ansible2.8共计2834个模块

    命令相关的模块:command

    ansible默认的模块,执行命令,注意:shell中的"<", ">", "|", ";", "&","$"等特殊字符不能在command模块中使用,如果需要使用,则用shell模块

      1 # 查看模块参数
      2 [root@ansible ~]# ansible-doc -s command
      3 
      4 # 在192.168.1.31服务器上面执行ls命令,默认是在当前用户的家目录/root
      5 [root@ansible ~]# ansible 192.168.1.31 -a 'ls'
      6 
      7 # chdir  先切换工作目录,再执行后面的命令,一般情况下在编译时候使用
      8 [root@ansible ~]# ansible 192.168.1.31 -a 'chdir=/tmp pwd'
      9 192.168.1.31 | CHANGED | rc=0 >>
     10 /tmp
     11 
     12 # creates  如果creates的文件存在,则执行后面的操作
     13 [root@ansible ~]# ansible 192.168.1.31 -a 'creates=/tmp ls /etc/passwd'    #tmp目录存在,则不执行后面的ls命令
     14 192.168.1.31 | SUCCESS | rc=0 >>
     15 skipped, since /tmp exists
     16 [root@ansible ~]# ansible 192.168.1.31 -a 'creates=/tmp11 ls /etc/passwd'    # tmp11文件不存在,则执行后面的ls命令
     17 192.168.1.31 | CHANGED | rc=0 >>
     18 /etc/passwd
     19 
     20 # removes  和creates相反,如果removes的文件存在,才执行后面的操作
     21 [root@ansible ~]# ansible 192.168.1.31 -a 'removes=/tmp ls /etc/passwd'    #tmp文件存在,则执行了后面的ls命令
     22 192.168.1.31 | CHANGED | rc=0 >>
     23 /etc/passwd
     24 [root@ansible ~]# ansible 192.168.1.31 -a 'removes=/tmp11 ls /etc/passwd'  #tmp11文件不存在,则没有执行后面的ls命令
     25 192.168.1.31 | SUCCESS | rc=0 >>
     26 skipped, since /tmp11 does not exist

    命令相关的模块:shell

    专门用来执行shell命令的模块,和command模块一样,参数基本一样,都有chdir,creates,removes等参数

      1 # 查看模块参数
      2 [root@ansible ~]# ansible-doc -s shell
      3 
      4 [root@ansible ~]# ansible 192.168.1.31 -m shell -a 'mkdir /tmp/test'
      5 [root@ansible ~]# ansible 192.168.1.31 -m shell -a 'ls /tmp'
      6 
      7 #执行下面这条命令,每次执行都会更新文件的时间戳
      8 [root@ansible ~]# ansible 192.168.1.31 -m shell -a 'cd /tmp/test && touch 1.txt && ls'
      9 192.168.1.31 | CHANGED | rc=0 >>
     10 1.txt
     11 
     12 # 由于有时候不想更新文件的创建时间戳,则如果存在就不执行creates
     13 [root@ansible ~]# ansible 192.168.1.31 -m shell -a 'creates=/tmp/test/1.txt cd /tmp/test && touch 1.txt && ls'
     14 192.168.1.31 | SUCCESS | rc=0 >>
     15 skipped, since /tmp/test/1.txt exists

    命令相关的模块:script

    用于在被管理机器上面执行shell脚本的模块,脚本无需在被管理机器上面存在

      1 # 查看模块参数
      2 [root@ansible ~]# ansible-doc -s script
      3 
      4 # 编写shell脚本
      5 [root@ansible ~]# vim ansible_test.sh
      6 #!/bin/bash
      7 echo `hostname`
      8 
      9 # 在所有被管理机器上执行该脚本
     10 [root@ansible ~]# ansible all -m script -a '/root/ansible_test.sh'
     11 192.168.1.32 | CHANGED => {
     12     "changed": true,
     13     "rc": 0,
     14     "stderr": "Shared connection to 192.168.1.32 closed.
    ",
     15     "stderr_lines": [
     16         "Shared connection to 192.168.1.32 closed."
     17     ],
     18     "stdout": "linux.node02.com
    ",
     19     "stdout_lines": [
     20         "linux.node02.com"
     21     ]
     22 }
     23 ......

    文件相关的模块:file

    用于对文件的处理,创建,删除,权限控制等

      1 # 查看模块参数
      2 [root@ansible ~]# ansible-doc -s file
      3 path     #要管理的文件路径
      4 recurse  #递归
      5 state:
      6      directory  #创建目录,如果目标不存在则创建目录及其子目录
      7      touch      #创建文件,如果文件存在,则修改文件 属性
      8 
      9      absent     #删除文件或目录
     10      mode       #设置文件或目录权限
     11      owner      #设置文件或目录属主信息
     12      group      #设置文件或目录属组信息
     13      link       #创建软连接,需要和src配合使用
     14      hard       #创建硬连接,需要和src配合使用
     15 
     16 # 创建目录
     17 [root@ansible ~]# ansible 192.168.1.31 -m file -a 'path=/tmp/test1 state=directory'
     18 
     19 # 创建文件
     20 [root@ansible ~]# ansible 192.168.1.31 -m file -a 'path=/tmp/test2 state=touch'
     21 
     22 # 建立软链接(src表示源文件,path表示目标文件)
     23 [root@ansible ~]# ansible 192.168.1.31 -m file -a 'src=/tmp/test1 path=/tmp/test3 state=link'
     24 
     25 # 删除文件
     26 [root@ansible ~]# ansible 192.168.1.31 -m file -a 'path=/tmp/test2 state=absent'
     27 
     28 # 创建文件时同时设置权限等信息
     29 [root@ansible ~]# ansible 192.168.1.31 -m file -a 'path=/tmp/test4 state=directory mode=775 owner=root group=root'

    文件相关的模块:copy

    用于管理端复制文件到远程主机,并可以设置权限,属组,属主等

      1 # 查看模块参数
      2 [root@ansible ~]# ansible-doc -s copy
      3 src      #需要copy的文件的源路径
      4 dest     #需要copy的文件的目标路径
      5 backup   #对copy的文件进行备份
      6 content  #直接在远程主机被管理文件中添加内容,会覆盖原文件内容
      7 mode     #对copy到远端的文件设置权限
      8 owner    #对copy到远端的文件设置属主
      9 group    #对copy到远端文件设置属组
     10 
     11 
     12 # 复制文件到远程主机并改名
     13 [root@ansible ~]# ansible 192.168.1.31 -m copy -a 'dest=/tmp/a.sh src=/root/ansible_test.sh'
     14 
     15 # 复制文件到远程主机,并备份远程文件,安装时间信息备份文件(当更新文件内容后,重新copy时用到)
     16 [root@ansible ~]# ansible 192.168.1.31 -m copy -a 'dest=/tmp/a.sh src=/root/ansible_test.sh backup=yes'
     17 
     18 # 直接在远程主机a.sh中添加内容
     19 [root@ansible ~]# ansible 192.168.1.31 -m copy -a 'dest=/tmp/a.sh content="#!/bin/bash
     echo `uptime`"'
     20 
     21 # 复制文件到远程主机,并设置权限及属主与属组
     22 [root@ansible ~]# ansible 192.168.1.31 -m copy -a 'dest=/tmp/passwd src=/etc/passwd mode=700 owner=root group=root'

    文件相关的模块:fetch

    用于从被管理机器上面拉取文件,拉取下来的内容会保留目录结构,一般情况用在收集被管理机器的日志文件等

      1 # 查看模块参数
      2 [root@ansible ~]# ansible-doc -s fetch
      3 src      #指定需要从远端机器拉取的文件路径
      4 dest     #指定从远端机器拉取下来的文件存放路径
      5 
      6 # 从被管理机器上拉取cron日志文件,默认会已管理节点地址创建一个目录,并存放在内
      7 [root@ansible ~]# ansible 192.168.1.31 -m fetch -a 'dest=/tmp src=/var/log/cron'
      8 
      9 [root@ansible ~]# tree /tmp/192.168.1.31/
     10 /tmp/192.168.1.31/
     11 └── var
     12     └── log
     13         └── cron
     14 directories, 1 file

    用户相关的模块:user

    用于对系统用户的管理,用户的创建、删除、家目录、属组等设置

      1 # 查看模块参数
      2 [root@ansible ~]# ansible-doc -s user
      3 name        #指定用户的名字
      4 home        #指定用户的家目录
      5 uid         #指定用户的uid
      6 group       #指定用户的用户组
      7 groups      #指定用户的附加组
      8 password    #指定用户的密码
      9 shell       #指定用户的登录shell
     10 create_home #是否创建用户家目录,默认是yes
     11 remove      #删除用户时,指定是否删除家目录
     12 state:
     13       absent    #删除用户
     14 
     15 
     16 # 创建用户名指定家目录,指定uid及组
     17 [root@ansible ~]# ansible 192.168.1.31 -m user -a 'name=mysql home=/opt/mysql uid=1002 group=root'
     18 [root@ansible ~]# ansible 192.168.1.31 -m shell  -a 'id mysql && ls -l /opt'
     19 192.168.1.31 | CHANGED | rc=0 >>
     20 uid=1002(mysql) gid=0(root) 组=0(root)
     21 总用量 0
     22 drwx------  3 mysql root 78 5月  27 18:13 mysql
     23 
     24 # 创建用户,不创建家目录,并且不能登录
     25 [root@ansible ~]# ansible 192.168.1.31 -m user -a 'name=apache shell=/bin/nologin uid=1003 create_home=no'
     26 [root@ansible ~]# ansible 192.168.1.31 -m shell  -a 'id apache && tail -1 /etc/passwd'
     27 192.168.1.31 | CHANGED | rc=0 >>
     28 uid=1003(apache) gid=1003(apache) 组=1003(apache)
     29 apache:x:1003:1003::/home/apache:/bin/nologin
     30 
     31 # 删除用户
     32 [root@ansible ~]# ansible 192.168.1.31 -m user -a 'name=apache state=absent'
     33 
     34 # 删除用户并删除家目录
     35 [root@ansible ~]# ansible 192.168.1.31 -m user -a 'name=mysql state=absent remove=yes'

    用户相关的模块: group

    用于创建组,当创建用户时如果需要指定组,组不存在的话就可以通过group先创建组

      1 # 查看模块参数
      2 [root@ansible ~]# ansible-doc -s group
      3 name     #指定组的名字
      4 gid      #指定组的gid
      5 state:
      6      absent   #删除组
      7      present  #创建组(默认的状态)
      8 
      9 # 创建组
     10 [root@ansible ~]# ansible 192.168.1.31 -m group -a 'name=www'
     11 
     12 # 创建组并指定gid
     13 [root@ansible ~]# ansible 192.168.1.31 -m group -a 'name=www1 gid=1005'
     14 
     15 # 删除组
     16 [root@ansible ~]# ansible 192.168.1.31 -m group -a 'name=www1 state=absent'

    软件包相关的模块: yum

    用于对软件包的管理,下载、安装、卸载、升级等操作

      1 # 查看模块参数
      2 [root@ansible ~]# ansible-doc -s yum
      3 name            #指定要操作的软件包名字
      4 download_dir    #指定下载软件包的存放路径,需要配合download_only一起使用
      5 download_only   #只下载软件包,而不进行安装,和yum --downloadonly一样
      6 list:
      7     installed   #列出所有已安装的软件包
      8     updates     #列出所有可以更新的软件包
      9     repos       #列出所有的yum仓库
     10 state:
     11     installed, present   #安装软件包(两者任选其一都可以)
     12     removed, absent      #卸载软件包
     13     latest      #安装最新软件包
     14 
     15 # 列出所有已安装的软件包
     16 [root@ansible ~]# ansible 192.168.1.31 -m yum -a 'list=installed'
     17 
     18 # 列出所有可更新的软件包
     19 [root@ansible ~]# ansible 192.168.1.31 -m yum -a 'list=updates'
     20 
     21 #列出所有的yum仓库
     22 [root@ansible ~]# ansible 192.168.1.31 -m yum -a 'list=repos'
     23 
     24 #只下载软件包并到指定目录下
     25 [root@ansible ~]# ansible 192.168.1.31 -m yum -a 'name=httpd download_only=yes download_dir=/tmp'
     26 
     27 #安装软件包
     28 [root@ansible ~]# ansible 192.168.1.31 -m yum -a 'name=httpd state=installed'
     29 
     30 #卸载软件包
     31 [root@ansible ~]# ansible 192.168.1.31 -m yum -a 'name=httpd state=removed'
     32 
     33 #安装包组,类似yum groupinstall 'Development Tools'
     34 [root@ansible ~]# ansible 192.168.1.31 -m yum -a 'name="@Development Tools" state=installed'
    软件包相关的模块:service

    服务模块,用于对服务进行管理,服务的启动、关闭、开机自启等

      1 # 查看模块参数
      2 [root@ansible ~]# ansible-doc -s service
      3 name       #指定需要管理的服务名
      4 enabled    #指定是否开机自启动
      5 state:     #指定服务状态
      6     started    #启动服务
      7     stopped    #停止服务
      8     restarted  #重启服务
      9     reloaded   #重载服务
     10 
     11 # 启动服务,并设置开机自启动
     12 [root@ansible ~]# ansible 192.168.1.31 -m service -a 'name=crond state=started enabled=yes'

    计划任务相关的模块:cron

    用于指定计划任务,和crontab -e一样

      1 # 查看模块参数
      2 [root@ansible ~]# ansible-doc -s cron
      3 job     #指定需要执行的任务
      4 minute   #分钟
      5 hour     #小时
      6 day      #天
      7 month    #月
      8 weekday  #周
      9 name     #对计划任务进行描述
     10 state:
     11     absetn   #删除计划任务
     12 
     13 
     14 # 创建一个计划任务,并描述是干嘛用的
     15 [root@ansible ~]# ansible 192.168.1.31 -m cron -a "name='这是一个测试的计划任务' minute=* hour=* day=* month=* weekday=* job='/bin/bash /root/test.sh'"
     16 [root@ansible ~]# ansible 192.168.1.31 -m shell -a 'crontab -l'
     17 192.168.1.31 | CHANGED | rc=0 >>
     18 #Ansible: 这是一个测试的计划任务
     19 * * * * * /bin/bash /root/test.sh
     20 
     21 # 创建一个没有带描述的计划任务
     22 [root@ansible ~]# ansible 192.168.1.31 -m cron -a "job='/bin/sh /root/test.sh'"
     23 
     24 # 删除计划任务
     25 [root@ansible ~]# ansible 192.168.1.31 -m cron -a "name='None' job='/bin/sh /root/test.sh' state=absent"

    系统信息相关的模块 : setup

    用于获取系统信息的一个模块

      1 # 查看模块参数
      2 [root@ansible ~]# ansible-doc -s setup
      3 
      4 # 查看系统所有信息
      5 [root@ansible ~]# ansible 192.168.1.31 -m setup
      6 
      7 # filter 对系统信息进行过滤
      8 [root@ansible ~]# ansible 192.168.1.31 -m setup -a 'filter=ansible_all_ipv4_addresses'
      9 
     10 # 常用的过滤选项
     11 ansible_all_ipv4_addresses          所有的ipv4地址
     12 ansible_all_ipv6_addresses          所有的ipv6地址
     13 ansible_architecture                系统的架构
     14 ansible_date_time                   系统时间
     15 ansible_default_ipv4                系统的默认ipv4地址
     16 ansible_distribution                系统名称
     17 ansible_distribution_file_variety   系统的家族
     18 ansible_distribution_major_version  系统的版本
     19 ansible_domain                      系统所在的域
     20 ansible_fqdn                        系统的主机名
     21 ansible_hostname                    系统的主机名,简写
     22 ansible_os_family                   系统的家族
     23 ansible_processor_cores             cpu的核数
     24 ansible_processor_count             cpu的颗数
     25 ansible_processor_vcpus             cpu的个数




    归类 : 自动化运维&服务相关

  • 相关阅读:
    pytorch torchversion标准化数据
    pytorch 中HWC转CHW
    pytorch torchversion自带的数据集
    pytorch Dataset数据集和Dataloader迭代数据集
    pytorch Model Linear实现线性回归CUDA版本
    pytorch实现手动线性回归
    pytorch中的前项计算和反向传播
    pytorch中CUDA类型的转换
    pytorch中tensor的属性 类型转换 形状变换 转置 最大值
    LightOJ 1074 spfa判断负环
  • 原文地址:https://www.cnblogs.com/lz1996/p/12704876.html
Copyright © 2020-2023  润新知