ansible-基础命令

本章主要是记录一下常用的ansible命令:

列出主机组的主机

主机组名为my_app

1
# ansible my_app --list

获取模块帮助信息

这里使用yum模块

1
# ansible-doc yum

软件安装,状态检查

my_app组下所有主机都安装ntpd,然后检查ntpd的版本

1
2
# ansible my_app -m yum -a 'name=ntpd state=present'
# ansible my_app -m command -a 'ntpd --version'

限定主机操作

my_app组下有:172.28.7.239、172.28.7.240两台机器

  1. –limit

    1
    # ansible my_app -m command -a 'systemctl status ntpd' --limit 172.28.7.239
  2. 指定IP

    1
    # ansible 172.28.7.239 -m command -a 'systemctl status ntpd'
  3. 分隔符:
    通过分隔符 : 会操作172.28.7.239、172.28.7.240、172.28.7.241三台机器

    1
    # ansible "172.28.7.239:172.28.7.241" -m command -a 'systemctl status ntpd'
  4. 通配符
    查看所有172.28.7.
    机器的ntpd状态

    1
    # ansible 172.28.7.* -m command -a 'systemctl status ntpd'

新增用户

主机组组为my_app
新增用户dba,使用bash shell模块,附加组为admins,dbagroup,家目录为:/home/dba/
groups 设定: groups=用户组1,用户组2……
增量 添加 属 组: append=yes
表明 属 组 状态 为 新建: state=present

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# ansible my_app -m user -a 'name=dba shell=/bin/bash groups=admins,dbagroup append=yes home=/home/dba/ state=present'
172.28.7.239 | success >> {
"changed": true,
"comment": "",
"createhome": true,
"group": 503,
"groups": "admins, dbagroup",
"home": "/home/dba/",
"name": "dba",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 501
}

修改用户属组

修改 DBA 附加 组 为 dbagroups( 即 删除 admins 组 权限)。
append= no

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# ansible my_app -m user -a 'name=dba groups=dbagroup append=no'
172.28.7.239 | success >> {
"append": false,
"changed": true,
"comment": "",
"group": 503,
"groups": "dbagroup",
"home": "/home/dba/",
"move_ home": false,
"name": "dba",
"shell": "/bin/bash",
"state": "present",
"uid": 501
}

修改用户属性

设置 dba 用户的过期时间为 2018/10/1 12:00:00 (UNIXTIME: 1538366400)。

1
# ansible my_app -m user -a 'name=dba expires=1538366400'

删除用户

删除 用户 DBA, 并 删除 其家 目录 和 邮件 列表。
表明 属 组 状态 为 删除: state= absent
设定 remove= yes: remove= yes

1
# ansible my_app -m user -a 'name=dba state=absent remove=yes'

结果检查: 登陆到对应主机使用ROOT用户查看/etc/passwd 是否存在 dba 用户, 或执行命令 id dba 确认是否有结果返回。

变更用户密码

设置 系统 用户 tom 的 密码 为 redhat123。

1
# ansible my_app -m user -a 'name=tom shell=/bin/bash password=to46pW3GOukvA update_password=always'

请注意,password 后的字符串 to46pW3GOukvA 并非真正的密码, 而是经过加密后的密码。 Ansible 变更用户密码方式与直接通过系统 命令 passwd 修改密码有较大差别。 Ansible 变更密码时所使用的密码是将明文密码加密后的密码(有些拗口)。官网上介绍了两种密码 加密方式,笔者建议使用方式 2passlib,因为 mkpasswd 方式因系统而异,功能差异较大。
密码生成方式:

1
2
# pip install passlib
# python -c 'import crypt;print crypt.crypt("redhat123", "dba")'

新增mysql用户

新增 MySQL 用户 stanley, 设置登录密码为 magedu@bj, 对 zabbix.* 表有 ALL 权限。

1
2
3
4
5
# ansible my_app -m mysql_user -a 'login_host=localhost login_password=magedu login_user=root name=stanley password=magedu@bj priv=zabbix.*:ALL state=present'
172.28.7.239 | success >> {
"changed": true,
"user": "stanley"
}

验证步骤:
登陆到db服务器,执行:show grants for ‘stanley’@’localhost’;

其实如上命令存在很大的安全隐患,因为MySQL的登录信息完全暴露在命令台。 Ansible 建议的使用方式如下:

  1. 在远程主机的~/.my.cnf文件中配置root的登录信息,配置信息如下:

    1
    2
    3
    [client]
    user=root
    password=magedu
  2. 命令行执行命令如下:

    1
    # ansible my_app -m mysql_user -a 'name=stanley password=magedu@bj priv=zabbix.*:ALL state=present'

拷贝文件

1
# ansible osds -m copy -a "src=/root/ceph.conf dest=/etc/ceph/ceph.conf owner=ceph group=ceph mode=644 backup=yes"

重启服务

1
# ansible osds -m service -a "name=ceph-osd@* state=restarted"