自动化运维-Ansible (第二部:模块)

Ansible 命令应用基础

  • 之前的一篇文章讲到了Ansible 的安装和作用,有兴趣的可以看看Ansible 介绍与安装。学习 ansible 就是重新学习一次命令和语法。

  • Ansible 可以使用命令行进行自动化管理,基本语法如下:
ansible <host-patterm> [-m module_name] [-a args]
<host-patterm>:对哪些主机生效
 [-m module_name]:要使用的模块
 [-a args] :模块特有参数
  • Ansible 的命令行 管理工具都是由一系列模块、参数所支持的,可以在命令后面加上 -h 或 --help 获取帮助。如果使用ansible-doc 工具可以通过 ansible-doc -h 或者 ansible-doc --help查看其他帮助信息。
  • ansible-doc 是用来查看模块帮助信息的工具,最主要的选项-l用来列出可使用的模块,-s 用来列出某个模块的描述信息和使用列子。如列出 yum 模块的描述信息和操作动作:
[root@localhost ~]# ansible-doc -s yum      //列出 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 can make this module
                               behave in a non-idempotent way. The task
                               could end up with a set of packages that
                               does not match the complete list of
                               specified packages to install (because
                               dependencies between the downgraded package
                               and others can cause changes to the
                               packages which were in the earlier
                               transaction).

Ansible 常用模块讲解

Ansible 自带了很多模块,能够下发执行 Ansible 的各种管理任务,默认使用 command 模块,即 -m 选项省略时也可以运行此模块,主要用于在被管理的主机上运行命令:

command 模块

[root@localhost ~]# ansible all -a 'tail -n 3 /etc/passwd'   //查看所有主机passwd最后3行 
192.168.154.132 | CHANGED | rc=0 >>     //这是第一台被控主机
test01:x:1001:1001::/home/test01:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
jerry:x:1002:1002::/home/jerry:/bin/bash

192.168.154.133 | CHANGED | rc=0 >>         //这是第二台被控主机
yihong:x:1000:1000:yihong:/home/yihong:/bin/bash
test01:x:1001:1001::/home/test01:/bin/bash
test02:x:306:306::/home/test02:/bin/bash

自动化运维-Ansible (第二部:模块)

cron 模块

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

1·添加计划任务

[root@localhost ~]# ansible mysql -m cron -a 'minute="*/1" job="/usr/bin/echo hello word" name="test job"'        //添加计划任务,每分钟执行一次 hello word 
192.168.154.133 | CHANGED => {
    "changed": true, 
    "envs": [], 
    "jobs": [
        "test job"
    ]
}
[root@localhost ~]# ansible mysql -a 'crontab -l'     //查看计划任务
192.168.154.133 | CHANGED | rc=0 >>
#Ansible: test job
*/1 * * * * /usr/bin/echo hello word

2·移除计划任务

[root@localhost ~]# ansible mysql -m cron -a 'name="test job" state=absent'   
192.168.154.133 | CHANGED => {
    "changed": true, 
    "envs": [], 
    "jobs": []
}
[root@localhost ~]# ansible mysql -a 'crontab -l'
192.168.154.133 | CHANGED | rc=0 >>

自动化运维-Ansible (第二部:模块)

User 模块

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

1·创建普通用户

[root@localhost ~]# ansible mysql -m user -a 'name="test01"'  //这里需要注意双影号
192.168.154.133 | SUCCESS => {

[root@localhost ~]# ansible mysql -a 'tail -n 3 /etc/passwd'  //查看passwd 文件
192.168.154.133 | CHANGED | rc=0 >>
yihong:x:1000:1000:yihong:/home/yihong:/bin/bash
test01:x:1001:1001::/home/test01:/bin/bash    //新创建的用户test01

2·创建系统用户

[root@localhost ~]# ansible mysql -m user -a 'name="tom" system=yes shell=/sbin/nologin'  
192.168.154.133 | CHANGED => {    //创建系统用户,执行shell登陆环境
    "changed": true,

3·删除用户

[root@localhost ~]# ansible mysql -m user -a 'name="test01" state=absent'
192.168.154.133 | CHANGED => {     //删除成功

group 模块

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


创建MySQL组,将tom用户添加到MySQL组中。

[root@localhost ~]# ansible webserver -m group -a 'name=mysql gid=306 system=yes'
192.168.154.132 | CHANGED => {
    "changed": true, 
    "gid": 306,
[root@localhost ~]# ansible webserver -m user -a 'name=tom uid=307 system=yes group=mysql'
192.168.154.132 | CHANGED => {

copy 模块

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

1·列如以下命令是:将本地文件 /etc/fstab 复制到被管理主机上的 /tmp/fstab.ansible,并且将所有者设置为 root ,权限设置为 640


[root@localhost ~]# ansible mysql -m copy -a 'src=/etc/fstab dest=/tmp/fstab.ansible owner=root mode=640'
192.168.154.133 | CHANGED => {
    "changed": true, 
    "checksum": "d855a342669b42e1b6632f814cbb9f327d472b28"

[root@localhost ~]# ansible mysql -a 'ls -l /tmp/fstab.ansible'
192.168.154.133 | CHANGED | rc=0 >>
-rw-r-----. 1 root root 617 10月 22 10:42 /tmp/fstab.ansible

2·将 “Hell Word” 写入 /tmp/fstab.ansible 文件中。

[root@localhost ~]# ansible mysql -m copy -a 'content="Hell Word" dest=/tmp/fstab.ansible'
192.168.154.133 | CHANGED => {
    "changed": true

[root@localhost ~]# ansible mysql -a 'cat /tmp/fstab.ansible'
192.168.154.133 | CHANGED | rc=0 >>
Hell Word

file 模块

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

1·列如,设置 /tmp/fstab.ansible 的所属主为 mysql ,属组为 mysql ,权限为 644. 这里需要注意,你的系统需要有这个mysql 这个用户才可以,没有就自己创建一个。

[root@localhost ~]# ansible mysql -m file -a 'owner=mysql group=mysql mode=644 path=/tmp/fstab.ansible'
192.168.154.133 | CHANGED => {
    "changed": true, 

[root@localhost ~]# ansible mysql -a 'ls -l /tmp/fstab.ansible'
192.168.154.133 | CHANGED | rc=0 >>
-rw-r--r--. 1 mysql mysql 9 10月 22 10:47 /tmp/fstab.ansible

ping 模块

ping 模块的使用很简单,它的作用就是拿来检测指定主机的连通性。

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

service 模块

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

1·查看 httpd 服务状态。

[root@localhost ~]# ansible all -a 'service httpd status'
 [WARNING]: Consider using the service module rather than running service.  If you need to use command
because service is insufficient you can add warn=False to this command task or set command_warnings=False
in ansible.cfg to get rid of this message.

说明:这里有个警告,大概意思是如果你只是查询状态,这个模块没问题,但是service主要用作他用。

2·启动 httpd 并设置为开机自启动

[root@localhost ~]# ansible webserver -m service -a 'enabled=true name=httpd state=started'
192.168.154.132 | SUCCESS => {
    "changed": false, 
    "enabled": true

shell 模块

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

列如:创建用户后使用无交互模式给用户设置密码

[root@localhost ~]# ansible mysql -m user -a 'name="nginx" system=yes shell=/sbin/nologin'                     //创建 nginx 用户
192.168.154.133 | CHANGED => {
    "changed": true

[root@localhost ~]# ansible mysql -m shell -a 'echo abc123 | passwd --stdin nginx'
192.168.154.133 | CHANGED | rc=0 >>
更改用户 nginx 的密码 。
passwd:所有的身份验证令牌已经成功更新。

script 模块

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

列如:编辑一个本地脚本 test.sh ,复制到被管理主机上进行运行。

[root@localhost ~]# vim test.sh
#!/bin/bash
echo "this is test job" > /tmp/script.ansible

[root@localhost ~]# chmod +x test.sh

[root@localhost ~]# ansible mysql -m script -a 'test.sh'
192.168.154.133 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 192.168.154.133 closed.\r\n"
[root@localhost ~]# ansible mysql -a 'cat /tmp/script.ansible'
192.168.154.133 | CHANGED | rc=0 >>
this is test job

yum 模块

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

1·安装软件 zsh

[root@localhost ~]# ansible mysql -m yum -a 'name=zsh'
192.168.154.133 | CHANGED => {
    "ansible_facts": {
        "pkg_mgr": "yum"
    }, 
    "changed": true,
[root@localhost ~]# ansible mysql -a 'rpm -q zsh'
 [WARNING]: Consider using the yum, dnf or zypper module rather than running rpm.  If you need to use
command because yum, dnf or zypper is insufficient you can add warn=False to this command task or set
command_warnings=False in ansible.cfg to get rid of this message.

192.168.154.133 | CHANGED | rc=0 >>
zsh-5.0.2-28.el7.x86_64

2·卸载 zsh 软件包

[root@localhost ~]# ansible mysql -m yum -a 'name=zsh state=absent'
192.168.154.133 | CHANGED => {
    "ansible_facts": {
        "pkg_mgr": "yum"
    }, 
    "changed": true

setup 模块

在Ansible 中使用 setup 模块,可以收集、查看被管理主机的设备信息。每个被管理的主机在接收并运行管理命令之前,都会将自己的相关信息发送给控制主机!

[root@localhost ~]# ansible mysql -m setup   //这里信息量较大,以下的省略
192.168.154.133 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.122.1", 
            "192.168.154.133"

总结

  • Ansible 的模块非常多。当不知道模块怎么用时可以用 【ansible-doc -s 模块名字】来查看模块的用法。
  • 【ansible-doc -l】 可以看看有哪些模块可以使用!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章