注意:-a 参数后的命令用单引号;双引号有可能会出问题,特别是在user模块html
模块一:测试目标主机是否在线,ping模块web
主机若是在线,则回复Pongshell
[root@ansible ~]# ansible web -m ping 192.168.30.36 | SUCCESS => { "changed": false, "ping": "pong" } 192.168.30.32 | SUCCESS => { "changed": false, "ping": "pong" }
模块二:command模块和shell模块缓存
用于在各被管理主机节点运行指定的命令服务器
shell与command的区别:command模块是执行ansible自带模块执行,若是要用> < | & ‘ ‘等管道,须要使用shell模块工具
[root@ansible tmp]# ansible web -m command -a 'uptime' 192.168.30.32 | SUCCESS | rc=0 >> 21:10:43 up 138 days, 1:34, 1 user, load average: 0.00, 0.01, 0.05 192.168.30.36 | SUCCESS | rc=0 >> 21:08:30 up 95 days, 3:02, 4 users, load average: 0.00, 0.00, 0.00
shell模块:测试
[root@ansible tmp]# ansible web -m shell -a 'cat /etc/fstab | grep ^UUID' 192.168.30.36 | SUCCESS | rc=0 >> UUID=28b3513d-9e6f-42a8-ade0-7700573fed2a /boot ext4 defaults 1 2 192.168.30.32 | SUCCESS | rc=0 >> UUID=de2e369a-c0e8-4188-89e0-db1f4aded764 /boot xfs defaults 0 0
相关选项以下:fetch
creates:一个文件名,当文件名存在,则该命令不执行
chdir:在执行指令以前,先切换到该目录
free_from:要执行的Linux指令
removes:一个文件名,当文件名不存在,则该命令不执行
executable:切换shell来执行指令,该执行路径必须是一个绝对路径
参考:<https://docs.ansible.com/ansible/latest/modules/shell_module.html>ui
模块三:远程复制备份--copy模块加密
[root@ansible tmp]# ansible web -m copy -a 'src=/etc/hosts dest=/tmp/hosts'
相关选项:
backup:在覆盖前,将源文件备份,备份文件包含时间信息,有俩选项:yes|no content:用于替代”src”,能够直接设定指定文件的值 dest:必选项,要将源文件复制到远程主机的绝对路径,若是源文件是一个目录,那么该路径必须是个目录 directory_mode:递归设定目录的权限,默认为系统默认权限 force:若是目标主机包含该文件,但内容不一样,若是设置为yes,则强制覆盖,若是为no,则只有当目标主机的目标位置不存在该文件时,才复制。默认为yes others:全部的file模块里的选项均可以在这里使用 src:被复制到远程主机的本地文件,能够是绝对路径,也能够是相对路径。若是路径是一个目录,他将递归复制。这种状况路径使用”/”来结尾,则只复制目录里的内容,若是没有使用“/”,则包含目录在内的所有内容复制,相似与rsync。 owner:指定属主 group:指定属组 mode:指定权限,能够以数字指定好比0644
参考:<https://docs.ansible.com/ansible/latest/modules/copy_module.html>
模块四:对远程文件管理--file模块
修改文件权限: [root@ansible ~]# ansible web -m file -a 'path=/srv/a.txt mode=600 owner=snow group=snow' 建立一个文件: [root@ansible ~]# ansible web -m file -a 'dest=/srv/a.txt state=touch' 建立一个目录文件: [root@ansible ~]# ansible web -m file -a 'path=/srv/haha mode=600 state=directory' 建立一个软连接: [root@ansible ~]# ansible web -m file -a 'dest=/srv/a.lnk src=/srv/a.txt state=link' 删除文件: [root@ansible ~]# ansible web -m file -a 'path=/srv/a.lnk state=absent'
相关选项:
force:须要在两种状况下强制建立软连接,一种源文件不存在,但以后会创建的状况;另外一种是目标软连接存在,须要先取消以前的连接,再建立新连接,有两个选项:yes|no path:必选项,定义文件/目录的路径(alias:dest,name) recurse:递归设置文件的属性,只对目录有效 src:被连接的源文件路径,只能用于state=link状况 dest:目标文件路径,只能用于state-link状况 state: directory:若是目录文件不存在,就建立目录文件 *file:即便文件不存在也会被建立 link:建立软连接 hard:建立硬连接 touch:若是文件不存在,则会建立一个新的文件,若是文件或目录已存在,则更新其最后修改时间 absent:删除目录、文件或者取消连接文件
参考:<https://docs.ansible.com/ansible/latest/modules/file_module.html>
fetch模块
: 从远程服务器拉取文件至本机,只能fetch文件,不能fetch目录,若是拉目录,先tar/zip 再拉到本机便可。
使用参考:<https://docs.ansible.com/ansible/latest/modules/fetch_module.html>
模块五:程序包管理工具--yum模块
安装程序包(可接版本号): [root@ansible ~]# ansible web -m yum -a 'name=httpd state=present' 安装最新程序包: [root@ansible ~]# ansible web -m yum -a 'name=httpd state=latest' 卸载程序包: [root@ansible ~]# ansible web -m yum -a 'name=httpd state=absent'
相关选项:
conf_file #设定远程yum安装时所依赖的配置文件。如配置文件没有在默认的位置。 disable_gpg_check #是否禁止GPG checking,只用于`present' or `latest'。 disablerepo #临时禁止使用yum库。 只用于安装或更新时。 enablerepo #临时使用的yum库。只用于安装或更新时。 name= #所安装的包的名称 state= #present安装, latest安装最新的, absent 卸载软件。 update_cache #强制更新yum的缓存
参考:<https://docs.ansible.com/ansible/latest/modules/yum_module.html>
注意:此模块只能在Python2上使用,在Python3上须要使用dnf模块用于包管理
使用参考:<https://docs.ansible.com/ansible/latest/modules/dnf_module.html>
模块六:服务程序管理--Service模块
启动服务: [root@ansible ~]# ansible web -m service -a 'name=httpd state=started' 开机启动服务: [root@ansible ~]# ansible web -m service -a 'name=httpd enabled=yes'
相关选项:
arguments #命令行提供额外的参数 enabled #设置开机启动。 name= #服务名称 runlevel #开机启动的级别,通常不用指定。 sleep #在重启服务的过程当中,是否等待。如在服务关闭之后等待2秒再启动。 state #started启动服务, stopped中止服务, restarted重启服务, reloaded重载配置
参考:<https://docs.ansible.com/ansible/latest/modules/service_module.html>
模块七:管理计划任务--cron模块
[root@ansible ~]# ansible web -m cron -a 'name="sync time" minute=*/10 job="/usr/sbin/ntpdate ntp.aliyun.com"'
相关选项:
backup= # 若是设置,建立一个crontab备份 cron_file= #若是指定, 将使用这个文件替换远程主机上cron.d目录下用户的任务计划,而不是单个用户计划任务
day= # 日应该运行的工做( 1-31, *, */2, etc ) hour= # 小时 ( 0-23, *, */2, etc )
minute= #分钟( 0-59, *, */2, etc )
month= # 月( 1-12, *, */2, etc )
weekday # 周 ( 0-6 for Sunday-Saturday, *, etc )
job= #指明运行的命令是什么 name= #定时任务描述 reboot # 任务在重启时运行,不建议使用,建议使用special_time special_time # 指定何时执行,参数:reboot,annually,monthly,weekly,daily,hourly state #指定状态,prsent表示添加定时任务,也是默认设置,absent表示删除定时任务 user # 以哪一个用户的身份执行
参考:<https://docs.ansible.com/ansible/latest/modules/cron_module.html>
模块八:远程主机执行本地脚本--script模块
[root@ansible ~]# ansible web -m script -a '/tmp/a.sh'
相关选项:
creates:一个文件名,当文件名存在,则该命令不执行
chdir:在执行指令以前,先切换到该目录
free_from:要执行的Linux指令
removes:一个文件名,当文件名不存在,则该命令不执行
executable:切换shell来执行指令,该执行路径必须是一个绝对路径
decrypt:使用Vault控制源文件的自动解码,选项yes|no
参考:<https://docs.ansible.com/ansible/latest/modules/script_module.html>
模块九:用户管理--user模块
建立加密密码:
[root@ansible ~]# echo 123456 | openssl passwd -1 -stdin $1$wMroVeEW$2ImLQiNupnwDJ5nk86F2X0
建立新用户:
[root@ansible ~]# ansible web -m user -a 'name=wolf system=yes password=$1$wMroVeEW$2ImLQiNupnwDJ5nk86F2X0 state=present'
更新用户密码:
[root@ansible ~]# ansible web -m user -a 'name=wolf update_password=always password=$1$wMroVeEW$2ImLQiNupnwDJ5nk86F2X0'
删除用户:
[root@ansible ~]# ansible web -m user -a 'name=wolf remove=yes state=absent'
相关选项:
comment # 用户的描述信息 createhom # 是否建立家目录,yes,默认项,即建立家目录;no,建立用户时不建立家目录. force # 在使用`state=absent'是, 行为与`userdel --force'一致. group # 指定基本组 groups # 指定附加组,若是指定为('groups=')表示删除全部组 home # 指定用户家目录 login_class #能够设置用户的登陆类 FreeBSD, OpenBSD and NetBSD系统. move_home # 若是设置为`home='时, 试图将用户主目录移动到指定的目录 name= # 指定用户名 non_unique # 该选项容许改变非惟一的用户ID值 password # 指定用户密码,password参数须要接受md5加密后的值 remove # yes:删除家目录,须要指定此参数;no:默认项,删除用户时默认不删除用户家目录. shell # 指定默认shell state #设置账号状态,默认值为present表示建立,可不写,指定值为absent表示删除 system # 当建立一个用户,设置这个用户是系统用户。这个设置不能更改现有用户。 uid #指定用户的uid update_password # 更新用户密码,always:新密码和旧密码不一样时进行修改,on_create:为新建用户指定密码 expires #指明密码的过时时间
参考:<https://docs.ansible.com/ansible/latest/modules/user_module.html>
模块十:收集远程主机的信息--setup模块
收集可用的facts用于内建变量。每一个主机的各类信息,cpu颗数、内存大小等。会存在facts中的某个变量中。调用后返回不少对应主机的信息,在后面的操做中能够根据不一样的信息来作不一样的操做。使用filter参数过滤,如redhat系列用yum安装,而debian系列用apt来安装软件
[root@ansible ~]# ansible web -m setup –a ‘filter=ansible_*_mb’
参考:<https://docs.ansible.com/ansible/latest/modules/setup_module.html>