ansible常用模块简介

rpm和yum的区别

yum 自动解决包的依赖关系

查看软件包是否安装

rpm -qa | grep python2-pip 
rpm -q nginx

yum

[epel] # 组名
name=Extra Packages for Enterprise Linux 7 - $basearch # 名字
baseurl=http://mirrors.aliyun.com/epel/7/$basearch  # url
failovermethod=priority 
enabled=1  # 是否启动
gpgcheck=0  # 是否校验gpgkey,0表示不校验,1表示校验
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

# 查询包组
yum grouplist

# 安装包组
yum groupinstall -y '包组名'

ansible

查看ansible生成的文件

# rpm -ql ansible | more
/etc/ansible
/etc/ansible/ansible.cfg  # ansible配置文件
/etc/ansible/hosts
/etc/ansible/roles

host-pattern

Usage: ansible <host-pattern> [options]

查看主机联通性

ansible 主机ip -m ping
ansible 主机1 ip, 主机2 ip -m ping
ansible 分组名 -m ping
ansible 分组一,分组二 -m ping	
ansible "分组一:分组二" -m ping	# 并集
ansible "分组一:&分组二" -m ping	# 交集
ansible "分组一:!分组二" -m ping	# 差集

显示可用模块

ansible-doc -l

Usage: ansible-doc [-l|-s] [options] [-t <plugin type] [plugin]

plugin documentation tool

Options:
  -a, --all             **For internal testing only** Show documentation for all plugins.
  -h, --help            show this help message and exit
  -l, --list            List available plugins
  -M MODULE_PATH, --module-path=MODULE_PATH
                        prepend colon-separated path(s) to module library
                        (default=[u'/root/.ansible/plugins/modules',
                        u'/usr/share/ansible/plugins/modules'])
  -s, --snippet         Show playbook snippet for specified plugin(s)
  -t TYPE, --type=TYPE  Choose which plugin type (defaults to "module")
  -v, --verbose         verbose mode (-vvv for more, -vvvv to enable connection debugging)
  --version             show program's version number and exit


# 统计总个的模块个数
ansible-doc params | wc -l

# 查看某个模块参数的用法(如:查看command模块中的-s参数)
ansible-doc -s command

command模块

ansible-doc -s command

- name: Executes a command on a remote node
  command:
      chdir:                 # Change into this directory before running the command.
      creates:               # A filename or (since 2.0) glob pattern, when it already exists, this step
                               will *not* be run.
      free_form:             # (required) The command module takes a free form command to run.  There is
                               no parameter actually named 'free form'.
                               See the examples!
      removes:               # A filename or (since 2.0) glob pattern, when it does not exist, this step
                               will *not* be run.
      stdin:                 # Set the stdin of the command directly to the specified value.
      warn:                  # If command_warnings are on in ansible.cfg, do not warn about this
                               particular line if set to `no'.

# 注意:
variables like `$HOME' and operations like `"<"', `">"', `"|"', `";"' and `"&"' will not work.

shell模块

# 修改用户密码
ansible 主机id/分组 -m shell -a "echo 'passwd' | passwd --stdin 用户名"

# 远程执行脚本
ansible 主机id/分组 -m shell -a "./1.py"

script模块

# 在其他机器上执行管控机上的文件
ansible 主机id/分组 -m script -a "./2.py"

copy模块

# 复制文件到远程主机
ansible 主机id/分组 -m copy -a "src=源文件路径 dest=目标文件路径"

# 复制文件到远程主机,并对远程主机上的原文件进行备份
ansible 主机id/分组 -m copy -a "src=源文件路径 dest=目标文件路径 backup=yes"

# 复制文件到远程主机,并修改属主和权限
ansible 主机id/分组 -m copy -a "src=源文件路径 dest=目标文件路径 owner=用户名 mode=700"

# 将content中的内容覆盖写入到指定文件中
ansible 主机id/分组 -m copy -a "dest=目标文件路径 owner=用户名 mode=700 content=nihao"

file模块

# 在远程主机上创建一个文件
ansible 主机id/分组 -m file -a "path=路径 state=touch"

# 在远程主机上创建一个文件夹
ansible 主机id/分组 -m file -a "path=路径 state=directory"

# 在远程主机上创建软连接
ansible 主机id/分组 -m file -a 'path=目标 state=link src=源文件'

# 在远程主机上创建硬连接
ansible 主机id/分组 -m file -a 'path=目标 state=hard src=源文件'

# 在远程主机上删除文件
ansible 主机id/分组 -m file -a 'path=文件路径 state=absent'

fetch模块

# 拉取被管控机的文件或目录到本地,以被管控机的ip作为目录名,并保留原有的目录结构
ansible 主机id/分组 -m fetch -a "src=/var/log/cron dest=/root"

yum模块

# 在被控机上安装软件包
ansible 主机id/分组 -m yum -a "name=软件名"

# 在被控机上安装软件包组
ansible 主机id/分组 -m yum -a "name=@包组名"

# 在被控机上卸载软件包
ansible 主机id/分组 -m yum -a "name=软件名 state=absent"

pip模块

# 安装指定模块
ansible 主机ip/分组 -m pip -a "name=模块名"

service模块

# 开启nginx
ansible 主机ip/分组 -m service -a "name=nginx state=started"

# 关闭nginx
ansible 主机ip/分组 -m service -a "name=nginx state=stoped"

cron模块

分 时 日 月 周 job
1  0  *  *  *
*  *  *  *  * 

# 创建定时任务
ansible 主机ip/分组 -m cron -a "minute=58 job='touch 1.txt' name=nihao"

# 删除定时任务
ansible 主机ip/分组 -m cron -a "minute=58 job='touch 1.txt' name=nihao disable=yes"

user模块

# 用户
- 超级用户 root 0
- 系统用户 不能登录 201-999 centos7 1-499 centos6
- 普通用户 可以登录 1000-60000 centos7 500-65535 centos6

# 组
- 超级组 root 0
- 系统组 201-999 centos7 1-499 centos6
- 普通组 1000-60000 centos7 500-65535 centos6



# 新建用户并指定组
ansible db -m user -a "name=test groups=root"

# 删除用户但是不删除家目录
ansible db -m user -a "name=test state=absent"

# 删除用户同时删除家目录
ansible db -m user -a "name=test state=absent remove=yes"

group模块

# 创建普通组
ansible 主机ip/分组 -m user -a "name=组名"

# 删除普通组
ansible 主机ip/分组 -m user -a "name=组名 state=absent"

setup模块

# 查看ansible收集的信息
ansible 主机ip/分组 -m setup

posted on 2019-07-04 11:56  透明的洪湖里  阅读(134)  评论(0编辑  收藏  举报