Ansible命令应用之常用模块(持续更新中)

上一篇博客中主要介绍了Ansible概述及Ansible部署,详见以下链接自动化运维之Ansible概述及Ansible部署请添加链接描述
本篇将介绍Ansible命令应用之常用模块,以实例的形式进行演示。

Ansible可用参数

  • -v:输出详细信息(可以使用多个v)
  • -i PATH:指定hosts文件位置
  • -f NUM :指定开启的进程数(默认为5)
  • -m MOULE :指定module的名称(默认为command)
  • -m DIRECTORY:指定module的目录来加载module,默认是/usr/share/ansible
  • -a,MODULE_ARGS:指定module模块的参数
  • -k:提示输入ssh的密码,而不是使用基于ssh的密钥认证
  • -u USERNAME:指定移动端的执行用户
  • -C:测试此命令执行会改变什么内容,不会真正的去执行

基本语法

ansible<host-pattern>[-m module_name][-a args]
<host-pattern>          //对哪些主机生效
[-m module_name]        //要使用的模块
[-a args]               //模块特有参数

Ansible的命令行管理工具都是由一系列模块、参数所支持的,可以在命令后面加上-h或--help获取帮助。如使用ansible-doc工具可以通过ansible-doc -h或者ansible-doc --help查看其帮助信息。
ansible-doc是用来查看模块帮助信息的工具,最主要的选项-l用来列出可使用的模块,-s用来列出某个模块的描述信息和使用示列。如列出yum模块的描述信息和操作动作:

[root@localhost ansible]# ansible-doc -s yum
- name: Manages packages with the `yum' package manager
  yum:
      allow_downgrade:       # Specify if the named package and version is allowed to
                               downgrade a maybe
                               already installed
                               higher version of that
                               package. Note that
                               setting
                               allow_downgrade=True
                               .....

Ansible常用模块

1.Command模块

默认模块,用于运行系统命令,比如echo hello。不支持shell变量和管道。

[root@localhost ansible]# ansible 192.168.88.10 -m command -a 'date'    //指定IP执行date命令
192.168.88.10 | SUCCESS | rc=0 >>
2018年 08月 01日 星期三 16:18:29 CST
[root@localhost ansible]# ansible webserver -m command -a 'date'    //指定组执行date命令
192.168.88.10 | SUCCESS | rc=0 >>
2018年 08月 01日 星期三 16:21:22 CST
[root@localhost ansible]# ansible all -a 'date'     //所有hosts执行date命令,不加-m默认使用command模块
192.168.88.10 | SUCCESS | rc=0 >>
2018年 08月 01日 星期三 16:22:44 CST

192.168.88.12 | SUCCESS | rc=0 >>
2018年 08月 01日 星期三 16:22:44 CST

2.cron模块

Ansible中的cron模块用于定义任务计划。其中有两种状态(state):present表示添加(省略状态时默认使用),absent表示移除。

[root@localhost ansible]# ansible-doc -s cron       //查看cron模块信息
- name: Manage cron.d and crontab entries
  cron:
      backup:                # If set, create a backup of the crontab before it is
                               modified. The location
                               of the backup is
                               returned in the
                               `backup_file' variable
                               by this module.
      cron_file:             # If specified, uses this file instead of an individual
                               user's crontab. If this
                               is a relative path, it
[root@localhost ansible]# ansible webserver -m cron -a 'minute="*/1" job="/bin/echo heihei" name="test cron job"'           //添加计划任务
192.168.88.10 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "test cron job"
    ]
}

[root@localhost ansible]# ansible webserver -a 'crontab -l'     //查看计划任务
192.168.88.10 | SUCCESS | rc=0 >>
#Ansible: test cron job
*/1 * * * * /bin/echo heihei

[root@localhost ansible]# ansible webserver -m cron -a 'name="test cron job" state=absent'       //移除计划任务
192.168.88.10 | SUCCESS => {
    "changed": true, 
    "envs": [], 
    "jobs": []
}

3.user模块

Ansible中user模块用于创建新用户和更改、删除已存在的用户。其中name选项用来指明创建的用户名称。

[root@localhost ansible]# ansible mysql -m user -a 'name="test01"'      //添加用户test01
192.168.88.12 | SUCCESS => {
    "changed": true, 
    "comment": "", 
    "create_home": true, 
    "group": 1001, 
    "home": "/home/test01", 
    "name": "test01", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 1001
}

[root@localhost ansible]# ansible mysql -a 'tail /etc/passwd'       //查看新添加用户test01
192.168.88.12 | SUCCESS | rc=0 >>
setroubleshoot:x:993:988::/var/lib/setroubleshoot:/sbin/nologin
sssd:x:992:987:User for sssd:/:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:991:986::/run/gnome-initial-setup/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
admin:x:1000:1000:admin:/home/admin:/bin/bash
test01:x:1001:1001::/home/test01:/bin/bash

[root@localhost ansible]# ansible mysql -m user -a 'name="test01" state=absent'     //删除test01用户
192.168.88.12 | SUCCESS => {
    "changed": true, 
    "force": false, 
    "name": "test01", 
    "remove": false, 
    "state": "absent"
}

4.group模块

Ansible中的group模块用于对用户组进行管理

[root@localhost ansible]# ansible mysql -m group -a 'name=mysql gid=306 system=yes'     //创建mysql组
192.168.88.12 | SUCCESS => {
    "changed": true, 
    "gid": 306, 
    "name": "mysql", 
    "state": "present", 
    "system": true
}
[root@localhost ansible]# ansible mysql -m user -a 'name=test02 uid=307 system=yes group=mysql'       //将test02用户添加到mysql组中
192.168.88.12 | SUCCESS => {
    "changed": true, 
    "comment": "", 
    "create_home": true, 
    "group": 306, 
    "home": "/home/test02", 
    "name": "test02", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": true, 
    "uid": 307
}

5.copy模块

Ansible中的copy模块用于实现文件复制和批量下发文件。其中使用src来定义本地源文件路径,使用dest定义被管理主机文件路径,使用content则是通过指定信息内容来生成目标文件。

[root@localhost ansible]# ansible mysql -m copy -a 'src=/etc/fstab dest=/opt/fstab.back owner=root mode=640'       //将本地文件/etc/fstab复制到被管理主机上的/opt/fstab.back,将所有者设置为root,权限设置为640
192.168.88.12 | SUCCESS => {
    "changed": true, 
    "checksum": "d0aba9a4f6cecb55d88d3e71a999cd716f3b0ac5", 
    "dest": "/opt/fstab.back", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "3bc805fb84012b541f89cb823d6ca070", 
    "mode": "0640", 
    "owner": "root", 
    "secontext": "system_u:object_r:usr_t:s0", 
    "size": 689, 
    "src": "/root/.ansible/tmp/ansible-tmp-1533113834.4-74195180796257/source", 
    "state": "file", 
    "uid": 0
}
[root@localhost ansible]# ansible mysql -a 'ls -l /opt'         //查看复制出的文件
192.168.88.12 | SUCCESS | rc=0 >>
总用量 4
-rw-r-----. 1 root root 689 8月   1 16:57 fstab.back
drwxr-xr-x. 2 root root   6 3月  26 2015 rh

[root@localhost ansible]# ansible mysql -m copy -a 'content="hello heihei!" dest=/opt/fstab.back'       //将“hello heihei!”写入/opt/fstab.back
192.168.88.12 | SUCCESS => {
    "changed": true, 
    "checksum": "b783c5c2da963523d21deff007f6e6b97fc625dc", 
    "dest": "/opt/fstab.back", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "0e7a9bdc00d20b6e3e1b03d836095644", 
    "mode": "0640", 
    "owner": "root", 
    "secontext": "system_u:object_r:usr_t:s0", 
    "size": 13, 
    "src": "/root/.ansible/tmp/ansible-tmp-1533113953.5-109792337994377/source", 
    "state": "file", 
    "uid": 0
}
[root@localhost ansible]# ansible mysql -a 'cat /opt/fstab.back'    //查看写入结果
192.168.88.12 | SUCCESS | rc=0 >>
hello heihei!

6.file模块

在Ansible中使用file模块来设置文件属性。其中使用path指定文件路径,使用src定义源文件路径,使用name或dest来替换创建文件的符号链接。

[root@localhost ansible]# ansible mysql -m file -a 'owner=mysql group=mysql mode=644 path=/opt/fstab.back'           //设置/opt/fstab.back的属主属组为mysql,权限为644
192.168.88.12 | SUCCESS => {
    "changed": true, 
    "gid": 306, 
    "group": "mysql", 
    "mode": "0644", 
    "owner": "mysql", 
    "path": "/opt/fstab.back", 
    "secontext": "system_u:object_r:usr_t:s0", 
    "size": 13, 
    "state": "file", 
    "uid": 305
}
[root@localhost ansible]# ansible mysql -m file -a 'path=/opt/fstab.link src=/opt/fstab.back state=link'     //设置文件/optfstab.link为文件/opt/fstab.back的连接文件
192.168.88.12 | SUCCESS => {
    "changed": true, 
    "dest": "/opt/fstab.link", 
    "gid": 0, 
    "group": "root", 
    "mode": "0777", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:usr_t:s0", 
    "size": 15, 
    "src": "/opt/fstab.back", 
    "state": "link", 
    "uid": 0
}
[root@localhost ansible]# ansible mysql -m file -a "path=/opt/fstab.back state=absent"       //删除一个文件
192.168.88.12 | SUCCESS => {
    "changed": true, 
    "path": "/opt/fstab.back", 
    "state": "absent"
}
[root@localhost ansible]# ansible mysql -m file -a "path=/opt/test state=touch"     //创建一个文件
192.168.88.12 | SUCCESS => {
    "changed": true, 
    "dest": "/opt/test", 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:usr_t:s0", 
    "size": 0, 
    "state": "file", 
    "uid": 0
}

7.ping模块

在Ansible中使用ping模块来检测指定主机的连通性。

[root@localhost ansible]# ansible all -m ping
192.168.88.12 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
192.168.88.10 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

8.service模块

在Ansible中使用service模块来控制管理服务的运行状态。其中,使用enabled表示是否开机自动启动,取值为true或者false;使用name定义服务名称;使用state指定服务状态,取值分别为started、stopped、restarted。

[root@localhost ansible]# ansible webserver -a 'systemctl status httpd'     //查看webserver组内主机httpd服务状态
192.168.88.10 | SUCCESS | rc=0 >>
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since 三 2018-08-01 17:41:13 CST; 5s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 43797 (httpd)
 ...

 [root@localhost ansible]# ansible webserver -m service -a 'enabled=true name=httpd state=stopped'     //关闭webserver组内主机httpd服务

9.shell模块

Ansible中的shell模块可以在被管理主机上运行命令,并支持像管道符等功能的复杂命令。

[root@localhost ansible]# ansible mysql -m shell -a 'echo abc123|passwd --stdin mysql'      //创建用户使用无交互模式给用户设置密码
192.168.88.12 | SUCCESS | rc=0 >>
更改用户 mysql 的密码 。
passwd:所有的身份验证令牌已经成功更新。

10.script模块

Ansible中的script模块可以将本地脚本复制到被管理主机上进行运行。需要注意的是,使用相对路径来指定脚本。

[root@localhost opt]# vi test.sh            //编写测试脚本
[root@localhost opt]# chmod +x test.sh      //给予执行权限
[root@localhost opt]# ansible mysql -m script -a 'test.sh'      //在mysql组中执行test脚本
192.168.88.12 | SUCCESS => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.88.12 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to 192.168.88.12 closed."
    ], 
    "stdout": "", 
    "stdout_lines": []
}
[root@localhost ~]# cat /opt/script.txt         //在mysql组的主机上进行查看
hello ansible from script

11.yum模块

Ansible中的yum模块负责在被管理主机上安装和卸载软件包,但是需要提前在每个节点配置自己的YUM仓库。其中使用name指定要安装的软件包,还需要带上软件包的版本号,否则安装最新的软件包;使用state指定安装软件包的状态,present、latest用来表示安装,absent表示卸载。

[root@localhost opt]# ansible mysql -m yum -a 'name=zsh'        //在mysql组的主机上安装zsh软件包
192.168.88.12 | SUCCESS => {
    "changed": true, 
    "msg": "", 
    "rc": 0, 
    "results": [
        "Loaded plugins: fastestmirror, langpacks\nLoading mirror speeds from cached hostfile\n * base: mirrors.nwsuaf.edu.cn\n * epel: mirrors.ustc.edu.cn\n * extras: mirrors.shu.edu.cn\n * updates: mirrors.shu.edu.cn\nResolving Dependencies\n--> Running transaction check\n---> Package zsh.x86_64 0:5.0.2-28.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package        Arch              Version                 Repository       Size\n================================================================================\nInstalling:\n zsh            x86_64            5.0.2-28.el7            base            2.4 M\n\nTransaction Summary\n================================================================================\nInstall  1 Package\n\nTotal download size: 2.4 M\nInstalled size: 5.6 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Installing : zsh-5.0.2-28.el7.x86_64                                      1/1 \n  Verifying  : zsh-5.0.2-28.el7.x86_64                                      1/1 \n\nInstalled:\n  zsh.x86_64 0:5.0.2-28.el7                                                     \n\nComplete!\n"
    ]
}
[root@localhost ~]# rpm -q zsh          //在mysql主机上查看zsh软件包安装情况
zsh-5.0.2-28.el7.x86_64

12.setup模块

在Ansible中使用setup模块收集、查看被管理主机的facts(facts是Ansible采集被管理主机设备信息的一个功能)。每个被管理主机在接收并运行管理命令之前,都会将自己的相关信息(操作系统版本、IP地址等)发送给控制主机。

[root@localhost opt]# ansible mysql -m setup        //查看mysql组主机的facts信息
192.168.88.12 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.122.1", 
            "192.168.88.12"
        ], 
        "ansible_all_ipv6_addresses": [
            "fe80::c3db:eb90:1263:4a4c"
        ], 
        "ansible_apparmor": {
        ...

下一篇将介绍Ansible Playbook的使用,敬请期待!

转载于:https://blog.51cto.com/13625810/2153885

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值