1.列出ansible所支持的模块。
ansible-doc -l
2.查看模块的详细帮助信息,比如查看 fetch 模块的帮助。
ansible-doc -s fetch
3.调用模块,比如调用 ping模块。
ansible 192.168.128.83 -m ping
4.调用模块的同时传入模块所需要的参数,以 fetch 模块为例。
ansible 192.168.128.83 -m fetch -a "src=/testdir/testfile1 dest=/testdir/ansible/"
常用模块
command
https://blog.csdn.net/dylloveyou/article/details/80412513
command 模块可以帮助我们在远程主机上执行命令
chdir # 切换目录 creates # 如果存在,则不执行后面的命令 removes # 如果存在,则执行后面的命令 ansible web -a 'pwd' ansible web -a 'ls /tmp' ansible web -a 'creates=/tmp mdkir /data' #被忽略,因为/tmp存在 ansible web -a 'creates=/tmp2 mkdir /data' #被执行,因为/tmp2目录不存在 ansible web -a 'removes=/tmp2 mkdir /data2' #被忽略,因为/tmp2目录不存在 ansible web -a 'removes=/tmp mkdir /data2' # 被执行,因为/tmp存在
shell
https://blog.csdn.net/dylloveyou/article/details/80443497
shell 模块可以帮助我们在远程主机上执行命令。与 command 模块不同的是,shell 模块在远程主机中执行命令时,会经过远程主机上的 /bin/sh
程序处理。
被管控机创建脚本文件 a.sh
#!/bin/bash
mkdir /wutenglan3
执行
ansible db -m shell -a 'echo "1234"|passwd --stdin alex' ansible 10.0.0.143 -m shell -a "bash a.sh" # 执行脚本 ansible 10.0.0.143 -m shell -a "/root/a.sh" ansible 10.0.0.143 -m shell -a "/root/a.py" # 执行python文件
如果出现这样问题,请修改被管理机文件权限 chmod +x a.py
[root@localhost ~]# ansible 192.168.14.164 -m shell -a'/root/a.py' 192.168.14.164 | FAILED | rc=126 >> /bin/sh: /root/a.py: Permission deniednon-zero return code
ansible db -m script -a "/root/b.sh" # 执行管控机器上的文件 ansible web -m script -a "creates=/root/a.py /root/b.sh" # 判断被管控机器上的文件是否存在 ,如果存在,就跳过 ansible web -m script -a "removes=/root/a.py /root/b.sh" # 判断被管控机器上的文件是否存在 ,如果存在,就执行
copy (管理主机=>远程)
copy 模块的作用就是拷贝文件,将 ansible 管理主机上的文件拷贝到远程主机中。
backup # 备份 content # 内容 dest # 目的地址 group # 文件的属组 mode #文件的权限 R 4 W 2 X 1 owner # 文件的属主 src #文件的源文件地址 ansible web -m copy -a "dest=/tmp/f src=/etc/fstab" #复制单独文件 ansible web -m copy -a "src=/etc/init.d dest=/tmp" #复制文件目录 ansible web -m copy -a "src=/etc/init.d/ dest=/tmp" # 复制文件夹内的所有的文件 ansible db -m copy -a "dest=/tmp/b.sh src=/root/b.sh mode=644 owner=alex" # 复制文件并修改权限和属主 ansible db -m copy -a "src=/etc/init.d dest=/tmp owner=alex" # 修改文件夹的权限或者属主属组时,文件夹内的所有文件的权限也会被修改 # 通过md5来做校验 ansible db -m copy -a "dest=/tmp/b.sh src=/root/b.sh mode=644 owner=alex backup=yes" # 复制文件并将原来的文件做备份 ansible db -m copy -a "dest=/tmp/c.txt content='大弦嘈嘈如急雨,小弦切切如私语'" # 直接往文件里面写入文件,是直接覆盖写入,慎用
fetch (远程=>管理主机)
dest #目标地址 src # 源地址 ansible db -m fetch -a "dest=/tmp src=/var/log/cron" # 复制远程被管控机器的文件道管控机器上,以被管控机的ip为目录,并保留原来的目录结构
access_time # 创建时间 group # 属组 mode # 权限 owner # 属主 path # 文件的路径 src # 源地址,只有在软连接和硬链接的时候才会使用 state # directory 目录 touch 文件 link 软连接 hard 硬链接 absent 删除 ansible db -m file -a "path=/alex state=directory" # 创建一个目录 ansible db -m file -a "path=/root/alex.txt state=touch" # 创建一个文件 ansible db -m file -a "src=/root/q.txt path=/tmp/q state=link" # 创建软连接,源地址是本机上的文件地址 ansible db -m file -a "path=/tmp/q state=absent" # 删除文件或者文件夹
补充: 链接
软连接 快捷方式 会跟源文件改变 ln -s
硬链接 硬盘的重复应用 会跟源文件改变 ln
复制 复制了一份 不会跟源文件改变 cp
yum
https://blog.csdn.net/dylloveyou/article/details/81674949
yum 模块可以帮助我们在远程主机上通过 yum 源管理软件包
补充:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
rpm 全称 radhat package manager yum 区别就是可以解决依赖关系, python写的
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[epel] # 名字 name=Extra Packages for Enterprise Linux 7 - $basearch 描述信息 baseurl=http://mirrors.aliyun.com/epel/7/$basearch yum源地址 failovermethod=priority enabled=1 # 当前yum源是否启用,1代表启用,0代表不启用 gpgcheck=0 # 用来检测gpgkey文件,1代表检测,0代表不检查 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
yum grouplist # 查询包组信息 yum groupinstall -y "Development Tools" # 安装包组
disable_gpg_check # 禁止检查gpgcheck disablerepo # 禁用repo源 enablerepo # 启用repo源 name #包的名称 state remove 卸载 install 安装 ansible web -m yum -a 'name=python2-pip' # 安装python2-pip ansible web -m yum -a 'name=python2-pip,redis' #用来安装多个包 ansible web -m yum -a 'name="@Development Tools"' # 用来安装包组
pip
pip模块可以帮助我们在远程主机上通过 pip管理软件包
# 补充
pip freeze > a.txt # 导出安装的软件包 pip list # 列出当前系统的软件包 pip install -r a.txt # 安装a。txt里面所有的包
应用
chdir # 切换目录 name # 包名 virtualenv # 虚拟环境 ansible web -m pip -a "name=flask"
service
https://blog.csdn.net/dylloveyou/article/details/80951933
service 模块可以帮助我们管理远程主机上的服务。比如,启动或停止远程主机中的 nginx 服务
补充:
# centos6: service redis start|stop|restart|reload chkconfig redis on # 开机自启动 # centos7: systemctl start|stop|restart|reload redis systemctl enable redis # 开机自启动 ss -tnlp # 查看端口 ps -ef # 查看进程
应用:
name # 包名 enabled # 开机自启动 state started|stopped|reloaded|restarted ansible web -m service -a 'name=redis state=started enabled=yes' #启动redis,并设置开机自启动 ansible web -m service -a 'name=redis state=stopped' #关闭redis
cron
https://blog.csdn.net/dylloveyou/article/details/80875132
cron 模块可以帮助我们管理远程主机中的计划任务,功能相当于 crontab 命令
补充:
# 示例1,每天的1点5分输出 test 字符 5 1 * * * echo test # 示例2,每3天执行一次计划任务,于当天的1点1分执行,具体任务为输出 test 字符 1 1 */3 * * echo test # 示例3,每次系统启动后需要执行一次计划任务,具体任务为输出 test 字符 @reboot echo test # 示例4,每小时执行一次计划任务,具体任务为输出 test 字符 @hourly echo test
crontab -l # 列出crontab
crontab -e # 编辑
crontab -r # 删除
应用
day # 天 hour # 小时 disabled # 禁用 job # 任务 minute #分钟 month #月 name #名字,描述信息 weekday #周 user #用户 state # 删除 ansible web -m cron -a "minute=19 job='touch /tmp/alex.txt' name=touchfile" ansible web -m cron -a "minute=19 job='touch /tmp/alex.txt' name=touchfile disabled=yes" # 表示禁用crontab,禁用的时候不可以直接指定名称 ansible web -m cron -a "name=touchfile state=absent" # 删除crontab,可以直接指定名称进行删除
user
https://blog.csdn.net/dylloveyou/article/details/81051324
user 模块可以帮助我们管理远程主机上的用户,比如创建用户、修改用户、删除用户、为用户创建密钥对等操作。
补充:
用户分为普通用户和管理员 管理员的uid为0 普通用户分为可登陆用户和不可登录用户(系统用户)
不可登录用户(系统用户) 1-999 centos7 1-499 centos6 可以登录的用户 1000-65535 centos7 500-65535 centos6
-d, --home-dir HOME_DIR 指定家目录 -g, --gid GROUP 指定gid -G, --groups GROUPS 指定用户的附加组 -r, --system 创建系统用户,创建时是倒序排列 -s, --shell SHELL 登录shell -u, --uid UID 指定uid useradd -d /opt/alexsb2 alexsb2 创建用户时指定家目录 userdel -r alexsb # 删除用户并删除用那个的家目录
应用:
group #组名 groups # 附加组 home # 家目录位置 remove #删除用户的家目录 shell # 用户登录的shell system # 系统用户 uid # 用户id ansible db -m user -a "uid=500 system=yes groups=root name=mysql" # 创建mysql用户,指定用户为系统用户,并指定uid为500,指定附加组为root ansible db -m user -a "uid=3000 groups=mysql name=canglaoshi home=/opt/canglaoshi shell=/sbin/nologin" # 创建普通用户,并制定用户的家目录和登录shell以及uid,附加组 ansible db -m user -a "name=canglaoshi remove=yes state=absent" # 删除用户
group
https://blog.csdn.net/dylloveyou/article/details/81147528
group 模块可以帮助我们管理远程主机上的组
补充:
用户组分为普通用户组和管理员组 管理员组的gid为0 普通用户组 -g gid -r 系统用户组 tail -1 /etc/group # 查看用户组
应用
gid : 组的id name :组的名字 state: 状态 system: 系统组 ansible web -m group -a "name=alex10" # 用来创建用户组 ansible web -m group -a "name=alex20 system=yes" # 用来创建系统组 ansible web -m group -a "name=alex10 state=absent" # 删除用户组
https://blog.csdn.net/dylloveyou/article/details/81951679
setup 模块用于收集远程主机的一些基本信息
# ansible 主机名-m setup
# ansible 主机名 -m setup -a "filter=ansible_all_ipv4_addresses"
ansible_all_ipv4_addresses:仅显示ipv4的信息。
ansible_devices:仅显示磁盘设备信息。
ansible_distribution:显示是什么系统,例:centos,suse等。
ansible_distribution_major_version:显示是系统主版本。
ansible_distribution_version:仅显示系统版本。
ansible_machine:显示系统类型,例:32位,还是64位。
ansible_eth0:仅显示eth0的信息。
ansible_hostname:仅显示主机名。
ansible_kernel:仅显示内核版本。
ansible_lvm:显示lvm相关信息。
ansible_memtotal_mb:显示系统总内存。
ansible_memfree_mb:显示可用系统内存。
ansible_memory_mb:详细显示内存情况。
ansible_swaptotal_mb:显示总的swap内存。
ansible_swapfree_mb:显示swap内存的可用内存。
ansible_mounts:显示系统磁盘挂载情况。
ansible_processor:显示cpu个数(具体显示每个cpu的型号)。
ansible_processor_vcpus:显示cpu个数(只显示总的个数)。
需求 web - 创建wusir20用户组,并指定为系统组 ansible web -m group -a "name=wusir20 system=yes" - 创建alex30用户,并制定家目录为/opt/alex30,登录shell为/sbin/nologin,并指定附加组为wusir20 ansible web -m user -a "name=alex30 home=/opt/alex30 shell=/sbin/nologin groups=wusir20" - 复制/etc/fstab文件到/tmp/f ansible web -m copy -a "dest=/tmp/f src=/etc/fstab" - 创建/data目录 ansible web -m file -a "path=/data state=direcrtory" - 安装nginx并启动它,并设置成开机自启动 ansible web -m yum -a "name=nginx" ansible web -m service -a "name=nginx state=started enabled=yes"