Ansible常用模块

#常用模块
##ping

[root@node1 ~]# ansible-doc -s ping
- name: Try to connect to host, verify a usable python and return `pong' on success

  • ansible的执行状态
    绿色:执行成功并且不需要做改变的操作
    黄色:执行成功并且对目标主机做变更
    红色:执行失败

##user

[root@node1 ~]# ansible-doc -s user
- name: Manage user accounts
  user:
    comment    # 用户的描述信息
    createhom  # 是否创建家目录
    force      # 在使用`state=absent'是, 行为与`userdel --force'一致.
    group      # 指定基本组
    groups     # 指定附加组,如果指定为('groups=')表示删除所有组
    home       # 指定用户家目录
    name=           # 指定用户名
    password        # 指定用户密码
    remove          # 在使用 `state=absent'时, 行为是与 `userdel --remove'一致.
    shell           # 指定默认shell
    state           #设置帐号状态,不指定为创建,指定值为absent表示删除
    system          # 当创建一个用户,设置这个用户是系统用户。这个设置不能更改现有用户。
    uid             #指定用户的uid
    update_password  # 更新用户密码
    expires         #指明密码的过期时间

1.添加系统用户,指定uid、家目录、主组及注释
2.删除用户及家目录


##group

[root@node1 ~]# ansible-doc -s group
- name: 添加或删除组
  action: group
    gid       # 设置组的GID号
    name=     # 管理组的名称
    state     # 指定组状态,默认为创建,设置值为absent为删除
    system    # 设置值为yes,表示为创建系统组

##command

1.默认使用的模块
2.不支持管道,变量及重定向等
[root@node1 ~]# ansible-doc -s command
- name: Executes a command on a remote node

##shell

1.调用bash执行命令
2.但是某些复杂的操作即使使用shell也可能会失败
    解决方法:将操作写到脚本中,通过script模块
[root@node1 tmp]# ansible-doc -s shell
- name: Execute commands in nodes.
  shell:

##script

[root@node1 ~]# ansible-doc -s script
- name: Runs a local script on a remote node after transferring it

##copy

[root@node1 ~]# ansible-doc -s copy
- name: Copies files to remote locations
  copy:
    backup:在覆盖之前,将源文件备份,备份文件包含时间信息。
    content:用于替代“src”,可以直接设定指定文件的值
    dest:必选项。要将源文件复制到的远程主机的绝对路径
    directory_mode:递归设定目录的权限,默认为系统默认权限
    force:强制覆盖目的文件内容,默认为yes
    others:所有的file模块里的选项都可以在这里使用
    src:被复制到远程主机的本地文件,可以是绝对路径,也可以是相对路径。如果路径是一个目录,它将递归复制。

##file

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

##corn

[root@node1 ~]# ansible-doc -s cron
- name: Manage cron.d and crontab entries.
  cron:

##yum

[root@node1 ~]# ansible-doc -s yum
- name: Manages packages with the yum package manager
  yum:
    conf_file         #设定远程yum安装时所依赖的配置文件。如配置文件没有在默认的位置。
    disable_gpg_check    #是否禁止GPG checking,只用于`present' or `latest'。
    disablerepo   #临时禁止使用yum库。 只用于安装或更新时。
    enablerepo    #临时使用的yum库。只用于安装或更新时。
    name=            #所安装的包的名称
    state               #present安装, latest安装最新的, absent 卸载软件。
    update_cache  #强制更新yum的缓存。

##service

[root@node1 ~]# ansible-doc service
> SERVICE    (/usr/lib/python2.7/site-packages/ansible/modules/system/service.py)

        Controls services on remote hosts. Supported init systems include BSD init, OpenRC, SysV, Solaris
        SMF, systemd, upstart. For Windows targets, use the [win_service] module instead.

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

##setup

[root@node1 ~]# ansible-doc -s setup
- name: Gathers facts about remote hosts

##selinux

[root@node1 ~]# ansible-doc -s selinux
- name: Change policy and state of SELinux
  selinux:
      conf:                  # path to the SELinux configuration file, if non-standard
      policy:                # name of the SELinux policy to use
      state:                 # (required) The SELinux mode

#持续更新ing

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值