Ansible (二) 常用moudle

Ansible 的Module简介

module简介

  模块是Ansible执行特定任务的代码块。比如:添加用户,上传文件和对客户机执行ping操作等。Ansible现在默认自带450多个模块,,Ansible Galaxy公共存储库则包含大约1600个模块。

ansible-doc模块功能查询

  我们可以通过ansible-doc命令来查看共有多少个模块. 如:

# ansible-doc -h
Usage: ansible-doc [-l|-F|-s] [options] [-t <plugin type> ] [plugin]
-l  列出所有的模块
-s  查看模块常用参数
# 直接跟模块名,显示模块所有信息

[root@wallace~]# ansible-doc -l
[root@wallace~]# ansible-doc -l |wc -l   #统计所有模块个数
[root@wallace ~]# ansible-doc -s command # 查看command模块的参数
- name: Execute commands on targets
  command:
      argv:                  # Passes the command as a list rather than a string. 
                               name"). Only the string or the list form can be provided, not both.  One or the other must be provided.
      chdir:                 # Change into this directory before running the command.
      cmd:                   # The command to run.
      creates:               # A filename or (since 2.0) glob pattern. If it already exists, this step *won't* be run.
      free_form:             # The command module takes a free form command to run. There is no actual parameter named 'free form'.
      removes:               # A filename or (since 2.0) glob pattern. If it already exists, this step *will* be run.
      stdin:                 # Set the stdin of the command directly to the specified value.
      stdin_add_newline:     # If set to `yes', append a newline to stdin data.
      strip_empty_ends:      # Strip empty lines from the end of stdout/stderr in result.
      warn:                  # Enable or disable task warnings.

Ansible 常用Module

命令相关模块

 command

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

# 查看模块参数
[root@wallace~]# ansible-doc -s command

# 在192.168.100.12服务器上面执行ls命令,默认是在当前用户的家目录/root
[root@wallace~]# ansible 192.168.1.31 -a 'ls'

# chdir  先切换工作目录,再执行后面的命令,一般情况下在编译时候使用
[root@wallace~]# ansible 192.168.100.12 -a 'chdir=/tmp pwd'
192.168.100.12 | CHANGED | rc=0 >>
/tmp

# creates  如果creates的文件存在,则执行后面的操作
[root@wallace~]# ansible 192.168.100.12 -a 'creates=/tmp ls /etc/passwd'    #tmp目录存在,则不执行后面的ls命令
192.168.100.12 | SUCCESS | rc=0 >>
skipped, since /tmp exists
[root@wallace~]# ansible 192.168.100.12 -a 'creates=/tmp11 ls /etc/passwd'    # tmp11文件不存在,则执行后面的ls命令
192.168.100.12 | CHANGED | rc=0 >>
/etc/passwd

# removes  和creates相反,如果removes的文件存在,才执行后面的操作
[root@wallace~]# ansible 192.168.100.12 -a 'removes=/tmp ls /etc/passwd'    #tmp文件存在,则执行了后面的ls命令
192.168.100.12 | CHANGED | rc=0 >>
/etc/passwd
[root@wallace~]# ansible 192.168.100.12 -a 'removes=/tmp11 ls /etc/passwd'  #tmp11文件不存在,则没有执行后面的ls命令
192.168.100.12 | SUCCESS | rc=0 >>
skipped, since /tmp11 does not exist
 shell

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

参数:
chdir    no    Change into this directory before running the command.    在执行命令之前对目录进行切换
creates    no    If it already exists, this step won't be run    如果文件存在了,不执行命令操作
removes    no     If it already exists, this step will be run     如果文件存在了, 这个步骤将执行
[root@wallace ~]# ansible 192.168.100.12 -m shell -a "mkdir /tmp/test"
[WARNING]: Consider using the file module with state=directory rather than running 'mkdir'. 
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.100.12 | CHANGED | rc=0 >> [root@wallace ~]# ansible 192.168.100.12 -m shell -a "chdir=/tmp ls" # 注意这里有警告,是希望我们用file模块创建 192.168.100.12 | CHANGED | rc=0 >> ansible_command_payload_vPJZqM systemd-private-1b6c407b52644ed5b7acd482f3802d83-chronyd.service-Dq4GY1 systemd-private-ba1a4561dfee4b93b0d8fbb7f460ea4d-chronyd.service-xvsugR test vmware-root # 则如果存在就不执行creates [root@wallace~]# ansible 192.168.100.12 -m shell -a 'creates=/tmp/test/1.txt cd /tmp/test && touch 1.txt && ls' 192.168.100.12 | SUCCESS | rc=0 >> skipped, since /tmp/test/1.txt exists
 script

  用于在被管理机器上面执行shell脚本的模块,注意这里的脚本是指在管理机上的脚本,并不是被管理机上.

参数: 
chdir    no    Change into this directory before running the command.    在执行命令之前对目录进行切换
creates    no    If it already exists, this step won't be run    如果文件存在了,不执行命令操作
removes    no     If it already exists, this step will be run     如果文件存在了, 这个步骤将执行
[root@wallace~]# vim test.sh 
#!/bin/bash
echo `hostname

[root@wallace ~]# ansible 192.168.100.12 -m script -a '/root/test.sh'
192.168.100.12 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.100.12 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to 192.168.100.12 closed."
    ], 
    "stdout": "server02\r\n", 
    "stdout_lines": [
        "server02"
    ]
}

文件相关模块

 file
# 主要参数
[root@wallace~]# ansible-doc -s file
path     #要管理的文件路径
recurse  #递归
state:
     directory  #创建目录,如果目标不存在则创建目录及其子目录
     touch      #创建文件,如果文件存在,则修改文件 属性
     
     absent     #删除文件或目录
     mode       #设置文件或目录权限
     owner      #设置文件或目录属主信息
     group      #设置文件或目录属组信息
     link       #创建软连接,需要和src配合使用
     hard       #创建硬连接,需要和src配合使用
# 创建目录
[root@wallace~]# ansible 192.168.100.12 -m file -a 'path=/tmp/test1 state=directory'

# 创建文件
[root@wallace ~]# ansible 192.168.100.12 -m file -a 'path=/tmp/test2 state=touch'

# 建立软链接(src表示源文件,path表示目标文件)
[root@wallace ~]# ansible 192.168.100.12-m file -a 'src=/tmp/test1 path=/tmp/test3 state=link'

# 删除文件
[root@wallace ~]# ansible 192.168.100.12 -m file -a 'path=/tmp/test2 state=absent'

# 创建文件时同时设置权限等信息
[root@wallace~]# ansible 192.168.100.12 -m file -a 'path=/tmp/test4 state=directory mode=775 owner=root group=root'
 copy

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

# 常用模块参数
[root@wallace~]# ansible-doc -s copy
src      #需要copy的文件的源路径
dest     #需要copy的文件的目标路径
backup   #对copy的文件进行备份
content  #直接在远程主机被管理文件中添加内容,会覆盖原文件内容
mode     #对copy到远端的文件设置权限
owner    #对copy到远端的文件设置属主
group    #对copy到远端文件设置属组
# 复制文件到远程主机并改名
[root@wallace~]# ansible 192.168.100.12 -m copy -a 'dest=/tmp/a.sh src=/root/ansible_test.sh'

# 复制文件到远程主机,并备份远程文件,安装时间信息备份文件(当更新文件内容后,重新copy时用到)
[root@wallace~]# ansible 192.168.100.12 -m copy -a 'dest=/tmp/a.sh src=/root/ansible_test.sh backup=yes'

# 直接在远程主机a.sh中添加内容
[root@wallace~]# ansible 192.168.100.12 -m copy -a 'dest=/tmp/a.sh content="#!/bin/bash\n echo `uptime`"'

# 复制文件到远程主机,并设置权限及属主与属组
[root@wallace~]# ansible 192.168.100.12 -m copy -a 'dest=/tmp/passwd src=/etc/passwd mode=700 owner=root group=root'
 fetch

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

常用参数
[root@wallace~]# ansible-doc -s fetch
src      #指定需要从远端机器拉取的文件路径
dest     #指定从远端机器拉取下来的文件存放路径
[root@wallace ~]# ansible 192.168.100.12 -m fetch -a 'dest=/tmp src=/var/log/cron'

[root@wallace ~]# tree /tmp/192.168.100.12/
/tmp/192.168.100.12/
└── var
    └── log
        └── cron

2 directories, 1 file

用户相关模块

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

 user
# 查看模块参数
[root@wallace~]# ansible-doc -s user
name        #指定用户的名字
home        #指定用户的家目录
uid         #指定用户的uid
group       #指定用户的用户组
groups      #指定用户的附加组
password    #指定用户的密码
shell       #指定用户的登录shell
create_home #是否创建用户家目录,默认是yes
remove      #删除用户时,指定是否删除家目录
state:
      absent    #删除用户
# 创建用户名指定家目录,指定uid及组
[root@wallace~]# ansible 192.168.100.12 -m user -a 'name=mysql home=/opt/mysql uid=1002 group=root'
[root@wallace~]# ansible 192.168.1.31 -m shell  -a 'id mysql && ls -l /opt'
192.168.100.12 | CHANGED | rc=0 >>
uid=1002(mysql) gid=0(root) 组=0(root)
总用量 0
drwx------  3 mysql root 78 5月  27 18:13 mysql

# 创建用户,不创建家目录,并且不能登录
[root@wallace~]# ansible 192.168.100.12 -m user -a 'name=apache shell=/bin/nologin uid=1003 create_home=no'
[root@wallace~]# ansible 192.168.1.31 -m shell  -a 'id apache && tail -1 /etc/passwd'
192.168.100.12 | CHANGED | rc=0 >>
uid=1003(apache) gid=1003(apache) 组=1003(apache)
apache:x:1003:1003::/home/apache:/bin/nologin

# 删除用户
[root@wallace~]# ansible 192.168.100.12 -m user -a 'name=apache state=absent'

# 删除用户并删除家目录
[root@wallace~]# ansible 192.168.100.12 -m user -a 'name=mysql state=absent remove=yes'
 group

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

# 查看模块参数
[root@wallace~]# ansible-doc -s group
name     #指定组的名字
gid      #指定组的gid
state:
     absent   #删除组
     present  #创建组(默认的状态)

# 创建组
[root@wallace~]# ansible 192.168.100.12 -m group -a 'name=www'

# 创建组并指定gid
[root@wallace ~]# ansible 192.168.100.12 -m group -a 'name=www1 gid=1005'

# 删除组
[root@ansible ~]# ansible 192.168.100.12 -m group -a 'name=www1 state=absent'

软件包相关模块

 yum

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

# 查看模块参数
[root@wallace~]# ansible-doc -s yum
name            #指定要操作的软件包名字
download_dir    #指定下载软件包的存放路径,需要配合download_only一起使用
download_only   #只下载软件包,而不进行安装,和yum --downloadonly一样
list:
    installed   #列出所有已安装的软件包
    updates     #列出所有可以更新的软件包
    repos       #列出所有的yum仓库
state:   
    installed, present   #安装软件包(两者任选其一都可以)
    removed, absent      #卸载软件包
    latest      #安装最新软件包
    
# 列出所有已安装的软件包
[root@wallace~]# ansible 192.168.100.12 -m yum -a 'list=installed'

# 列出所有可更新的软件包
[root@wallace~]# ansible 192.168.100.12 -m yum -a 'list=updates'

#列出所有的yum仓库
[root@wallace~]# ansible 192.168.100.12 -m yum -a 'list=repos'

#只下载软件包并到指定目录下
[root@wallace~]# ansible 192.168.100.12 -m yum -a 'name=httpd download_only=yes download_dir=/tmp'

#安装软件包
[root@wallace~]# ansible 192.168.100.12 -m yum -a 'name=httpd state=installed'

#卸载软件包
[root@wallace~]# ansible 192.168.100.12 -m yum -a 'name=httpd state=removed'

#安装包组,类似yum groupinstall 'Development Tools'
[root@wallace~]# ansible 192.168.100.12 -m yum -a 'name="@Development Tools" state=installed'

服务管理模块

 service

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

# 查看模块参数
[root@wallace~]# ansible-doc -s service
name       #指定需要管理的服务名
enabled    #指定是否开机自启动
state:     #指定服务状态
    started    #启动服务
    stopped    #停止服务
    restarted  #重启服务
    reloaded   #重载服务

# 启动服务,并设置开机自启动 
[root@wallace~]# ansible 192.168.100.12 -m service -a 'name=httpd state=started enabled=yes'
 cron

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

# 查看模块参数
[root@wallace~]# ansible-doc -s cron
job     #指定需要执行的任务
minute   #分钟
hour     #小时
day      #天
month    #月
weekday  #周
name     #对计划任务进行描述
state:
    absetn   #删除计划任务


# 创建一个计划任务,并描述是干嘛用的
[root@wallace~]# ansible 192.168.100.12 -m cron -a "name='测试的计划任务' minute=* hour=* day=* month=* weekday=* job='/bin/bash /root/test.sh'"
[root@wallace~]# ansible 192.168.100.12 -m shell -a 'crontab -l'
192.168.100.12 | CHANGED | rc=0 >>
#Ansible: 这是一个测试的计划任务
* * * * * /bin/bash /root/test.sh

# 创建一个没有带描述的计划任务
[root@wallace~]# ansible 192.168.100.12 -m cron -a "job='/bin/sh /root/test.sh'"

# 删除计划任务
[root@wallace~]# ansible 192.168.100.12 -m cron -a "name='None' job='/bin/sh /root/test.sh' state=absent"

系统信息模块

 setup

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

# 查看模块参数
[root@wallace~]# ansible-doc -s setup

# 查看系统所有信息
[root@wallace~]# ansible 192.168.100.12 -m setup

# filter 对系统信息进行过滤
[root@wallace~]# ansible 192.168.100.12 -m setup -a 'filter=ansible_all_ipv4_addresses'

# 常用的过滤选项
ansible_all_ipv4_addresses         所有的ipv4地址
ansible_all_ipv6_addresses         所有的ipv6地址
ansible_architecture               系统的架构
ansible_date_time                  系统时间
ansible_default_ipv4               系统的默认ipv4地址
ansible_distribution               系统名称
ansible_distribution_file_variety  系统的家族
ansible_distribution_major_version 系统的版本
ansible_domain                     系统所在的域
ansible_fqdn                       系统的主机名
ansible_hostname                   系统的主机名,简写
ansible_os_family                  系统的家族
ansible_processor_cores            cpu的核数
ansible_processor_count            cpu的颗数
ansible_processor_vcpus            cpu的个数

 

猜你喜欢

转载自www.cnblogs.com/tashanzhishi/p/13393542.html