目录:
1.Ansible介绍
2.Ansible常用模块介绍
1.Ansible介绍
1.1 Ansible特性
模块化:调用特定的模块,完成特定任务
有Paramiko, PyYAML, Jinja2(模板语言)三个关键模块
支持自定义模块
基于Python语言实现
部署简单,基于python和SSH(默认已安装), agentless
安全,基于OpenSSH
支持playbook编排任务
幂等性:一个任务执行1遍和执行n遍效果一样,不因重复执行带来意外情况
无需代理不依赖PKI(无需ssl)
可使用任何编程语言写模块
YAML格式,编排任务,支持丰富的数据结构
较强大的多层解决方案
1.2 Ansible主要组成部分
ANSIBLE PLAYBOOKS:任务剧本(任务集),编排定义Ansible任务集的配置文件,由Ansible顺序依次执行,通常是JSON格式的YML文件
INVENTORY: Ansible管理主机的清单/etc/anaible/hosts
MODULES: Ansible执行命令的功能模块,多数为内置核心模块,也可自定义
PLUGINS:模块功能的补充,如连接类型插件、循环插件、变量插件、过滤插件等,该功能不常用
API:供第三方程序调用的应用程序编程接口
ANSIBLE:组合INVENTORY、 API、 MODULES、 PLUGINS的绿框, 可以理解为是ansible命令工具,其为核心执行工具
Ansible命令执行来源:
USER,普通用户,即SYSTEM ADMINISTRATOR
CMDB(配置管理数据库) API 调用
PUBLIC/PRIVATE CLOUD API调用
USER-> Ansible Playbook -> Ansibile
利用ansible实现管理的方式:
Ad-Hoc 即ansible命令,主要用于临时命令使用场景
Ansible-playbook 主要用于长期规划好的,大型项目的场景,需要有前期的规划过程
Ansible-playbook(剧本)执行过程
将已有编排好的任务集写入Ansible-Playbook
通过ansible-playbook命令分拆任务集至逐条ansible命令,按预定规则逐条执行
Ansible主要操作对象
HOSTS主机
NETWORKING网络设备
注意事项
执行ansible的主机一般称为主控端,中控, master或堡垒机
主控端Python版本需要2.6或以上
被控端Python版本小于2.4需要安装python-simplejson
被控端如开启SELinux需要安装libselinux-python
windows不能做为主控端
1.3 Ansible配置文件
配置文件:
/etc/ansible/ansible.cfg 主配置文件,配置ansible工作特性
/etc/ansible/hosts 主机清单
/etc/ansible/roles/ 存放角色的目录
程序:
/usr/bin/ansible 主程序,临时命令执行工具
/usr/bin/ansible-doc 查看配置文档,模块功能查看工具
/usr/bin/ansible-galaxy 下载/上传优秀代码或Roles模块的官网平台
/usr/bin/ansible-playbook 定制自动化任务,编排剧本工具
/usr/bin/ansible-pull 远程执行命令的工具
/usr/bin/ansible-vault 文件加密工具
/usr/bin/ansible-console 基于Console界面与用户交互的执行工具
/etc/ansible/ansible.cfg
Ansible 配置文件/etc/ansible/ansible.cfg (一般保持默认)
[defaults]
#inventory = /etc/ansible/hosts # 主机列表配置文件
#library = /usr/share/my_modules/ # 库文件存放目录
#remote_tmp = $HOME/.ansible/tmp #临时py命令文件存放在远程主机目录
#local_tmp = $HOME/.ansible/tmp # 本机的临时命令执行目录
#forks = 5 # 默认并发数
#sudo_user = root # 默认sudo 用户
#ask_sudo_pass = True #每次执行ansible命令是否询问ssh密码
#ask_pass = True
#remote_port = 22
#host_key_checking = False # 检查对应服务器的host_key,建议取消注释
#log_path=/var/log/ansible.log #日志文件
#module_name = command #默认模块
2.Ansible常用模块介绍
2.1 COMMAND模块
[root@ansible ~]# ansible-doc -s command
1.测验
[root@ansible ~]# ansible all -m command -a "ls /data"
192.168.43.111 | CHANGED | rc=0 >>
192.168.43.112 | CHANGED | rc=0 >>
192.168.43.159 | CHANGED | rc=0 >>
lost+found
[root@ansible ~]# ansible all -m command -a "chdir=/data ls"
192.168.43.112 | CHANGED | rc=0 >>
192.168.43.159 | CHANGED | rc=0 >>
lost+found
192.168.43.111 | CHANGED | rc=0 >>
[root@ansible ~]# ansible all -m command -a "creates=/etc/fstab ls /data"
192.168.43.112 | SUCCESS | rc=0 >>
skipped, since /etc/fstab exists
192.168.43.111 | SUCCESS | rc=0 >>
skipped, since /etc/fstab exists
192.168.43.159 | SUCCESS | rc=0 >>
skipped, since /etc/fstab exists
[root@ansible ~]# ansible all -a "creates=/etc/fstab ls /data"
192.168.43.111 | SUCCESS | rc=0 >>
skipped, since /etc/fstab exists
192.168.43.112 | SUCCESS | rc=0 >>
skipped, since /etc/fstab exists
192.168.43.159 | SUCCESS | rc=0 >>
skipped, since /etc/fstab exists
[root@ansible ~]# ansible all -m command -a "creates=/etc/xxxx ls /data"
192.168.43.111 | CHANGED | rc=0 >>
192.168.43.112 | CHANGED | rc=0 >>
192.168.43.159 | CHANGED | rc=0 >>
lost+found
command模块的缺陷
该对于管道符|,重定向符号>,$符号都不支持
[root@ansible ~]# ansible all -a 'echo $HOSANAME'
192.168.43.111 | CHANGED | rc=0 >>
$HOSANAME
192.168.43.112 | CHANGED | rc=0 >>
$HOSANAME
192.168.43.159 | CHANGED | rc=0 >>
$HOSANAME
[root@ansible ~]# ansible all -a 'getent passwd root'
192.168.43.112 | CHANGED | rc=0 >>
root:x:0:0:root:/root:/bin/bash
192.168.43.111 | CHANGED | rc=0 >>
root:x:0:0:root:/root:/bin/bash
192.168.43.159 | CHANGED | rc=0 >>
root:x:0:0:root:/root:/bin/bash
[root@ansible ~]# ansible all -a 'echo stone |passwd --stdin wang'
192.168.43.159 | CHANGED | rc=0 >>
stone |passwd --stdin wang
192.168.43.111 | CHANGED | rc=0 >>
stone |passwd --stdin wang
192.168.43.112 | CHANGED | rc=0 >>
stone |passwd --stdin wang
2.2 SHELL模块
[root@ansible ~]# ansible-doc -s shell
[root@ansible ~]# ansible all -a 'echo abcdefg |passwd --stdin stone'
192.168.43.111 | CHANGED | rc=0 >>
abcdefg |passwd --stdin stone
192.168.43.112 | CHANGED | rc=0 >>
abcdefg |passwd --stdin stone
192.168.43.159 | CHANGED | rc=0 >>
abcdefg |passwd --stdin stone
[root@ansible ~]# ansible -m shell all -a 'echo abcdefg |passwd --stdin stone'
192.168.43.111 | CHANGED | rc=0 >>
Changing password for user stone.
passwd: all authentication tokens updated successfully.
192.168.43.112 | CHANGED | rc=0 >>
Changing password for user stone.
passwd: all authentication tokens updated successfully.
192.168.43.159 | CHANGED | rc=0 >>
Changing password for user stone.
passwd: all authentication tokens updated successfully.
[root@ansible ~]# ansible -m shell all -a 'echo $HOSTNAME'
192.168.43.159 | CHANGED | rc=0 >>
localhost.localdomain
192.168.43.111 | CHANGED | rc=0 >>
centos7min.localdomain
192.168.43.112 | CHANGED | rc=0 >>
centos7min.localdomain
[root@ansible ~]# vim /etc/ansible/ansible.cfg
module_name = shell 修改默认模块,则不用添加-m shell
[root@ansible ~]# ansible all -a 'echo $HOSTNAME'
192.168.43.111 | CHANGED | rc=0 >>
centos7min.localdomain
192.168.43.112 | CHANGED | rc=0 >>
centos7min.localdomain
192.168.43.159 | CHANGED | rc=0 >>
localhost.localdomain
2.3 SCRIPT模块
[root@ansible ~]# ansible-doc -s script
[root@ansible ~]# ansible all -m script -a '/root/test.sh'
192.168.43.112 | CHANGED => {
"changed": true,
"stdout_lines": [
"hello world"
]
}
192.168.43.111 | CHANGED => {
"changed": true,
"stdout_lines": [
"hello world"
]
}
192.168.43.159 | CHANGED => {
"changed": true,
"stdout_lines": [
"hello world"
]
}
2.4 COPY模块
[root@ansible ~]# ansible-doc -s copy
[root@ansible ~]# ansible websrvs -m copy -a 'src=/etc/fstab dest=/data/'
192.168.43.111 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "296fa550cb5e18f5c36e173cc7fe2f18a18182db",
"dest": "/data/fstab",
"gid": 0,
"group": "root",
"md5sum": "78b8fee58b3d2f103de165d36ab34011",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:default_t:s0",
"size": 595,
"src": "/root/.ansible/tmp/ansible-tmp-1590616203.14-11200-219119172690356/source",
"state": "file",
"uid": 0
}
192.168.43.112 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "296fa550cb5e18f5c36e173cc7fe2f18a18182db",
"dest": "/data/fstab",
"gid": 0,
"group": "root",
"md5sum": "78b8fee58b3d2f103de165d36ab34011",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:default_t:s0",
"size": 595,
"src": "/root/.ansible/tmp/ansible-tmp-1590616203.5-11202-216123074508448/source",
"state": "file",
"uid": 0
}
[root@ansible ~]# ansible websrvs -a 'ls /data/'
192.168.43.112 | CHANGED | rc=0 >>
fstab
192.168.43.111 | CHANGED | rc=0 >>
fstab
[root@ansible ~]# ansible websrvs -a 'ls -l /data/'
192.168.43.112 | CHANGED | rc=0 >>
total 4
-rw-r--r--. 1 root root 595 Jun 2 01:09 fstab
192.168.43.111 | CHANGED | rc=0 >>
total 4
-rw-r--r--. 1 root root 595 May 28 05:50 fstab
[root@ansible ~]# ansible websrvs -m copy -a 'src=/etc/fstab dest=/data/' 由于幂等性,不会重复执行
192.168.43.112 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"checksum": "296fa550cb5e18f5c36e173cc7fe2f18a18182db",
"dest": "/data/fstab",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"path": "/data/fstab",
"secontext": "system_u:object_r:default_t:s0",
"size": 595,
"state": "file",
"uid": 0
}
192.168.43.111 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"checksum": "296fa550cb5e18f5c36e173cc7fe2f18a18182db",
"dest": "/data/fstab",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"path": "/data/fstab",
"secontext": "system_u:object_r:default_t:s0",
"size": 595,
"state": "file",
"uid": 0
}
[root@ansible ~]# ansible websrvs -m copy -a 'src=/etc/passwd dest=/data/passwd2 mode=600 owner=stone group=bin'
192.168.43.111 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "4fcce7277eb887a69f2cad9e0073c70364dd5cdd",
"dest": "/data/passwd2",
"gid": 1,
"group": "bin",
"md5sum": "6fbf394e130f5d7e8dc151dfc9d82a5e",
"mode": "0600",
"owner": "stone",
"secontext": "system_u:object_r:default_t:s0",
"size": 901,
"src": "/root/.ansible/tmp/ansible-tmp-1590616522.33-11363-48907142025305/source",
"state": "file",
"uid": 1000
}
192.168.43.112 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "4fcce7277eb887a69f2cad9e0073c70364dd5cdd",
"dest": "/data/passwd2",
"gid": 1,
"group": "bin",
"md5sum": "6fbf394e130f5d7e8dc151dfc9d82a5e",
"mode": "0600",
"owner": "stone",
"secontext": "system_u:object_r:default_t:s0",
"size": 901,
"src": "/root/.ansible/tmp/ansible-tmp-1590616522.66-11365-145652869041762/source",
"state": "file",
"uid": 1000
}
[root@ansible ~]# ansible websrvs -a 'ls -l /data'
192.168.43.111 | CHANGED | rc=0 >>
total 8
-rw-r--r--. 1 root root 595 May 28 05:50 fstab
-rw-------. 1 stone bin 901 May 28 05:55 passwd2
192.168.43.112 | CHANGED | rc=0 >>
total 8
-rw-r--r--. 1 root root 595 Jun 2 01:09 fstab
-rw-------. 1 stone bin 901 Jun 2 01:15 passwd2
[root@ansible ~]# ansible websrvs -m copy -a 'src=/etc/issue dest=/data/passwd2 mode=600 owner=stone group=bin backup=yes'
192.168.43.112 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"backup_file": "/data/passwd2.9708.2020-06-02@01:17:26~",
"changed": true,
"checksum": "5c76e3b565c91e21bee303f15c728c71e6b39540",
"dest": "/data/passwd2",
"gid": 1,
"group": "bin",
"md5sum": "f078fe086dfc22f64b5dca2e1b95de2c",
"mode": "0600",
"owner": "stone",
"secontext": "system_u:object_r:default_t:s0",
"size": 23,
"src": "/root/.ansible/tmp/ansible-tmp-1590616652.64-11451-261638613220883/source",
"state": "file",
"uid": 1000
}
192.168.43.111 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"backup_file": "/data/passwd2.11265.2020-05-28@05:57:31~",
"changed": true,
"checksum": "5c76e3b565c91e21bee303f15c728c71e6b39540",
"dest": "/data/passwd2",
"gid": 1,
"group": "bin",
"md5sum": "f078fe086dfc22f64b5dca2e1b95de2c",
"mode": "0600",
"owner": "stone",
"secontext": "system_u:object_r:default_t:s0",
"size": 23,
"src": "/root/.ansible/tmp/ansible-tmp-1590616652.64-11449-126085241623529/source",
"state": "file",
"uid": 1000
}
[root@ansible ~]# ansible websrvs -a 'ls -l /data'
192.168.43.112 | CHANGED | rc=0 >>
total 12
-rw-r--r--. 1 root root 595 Jun 2 01:09 fstab
-rw-------. 1 stone bin 23 Jun 2 01:17 passwd2
-rw-------. 1 stone bin 901 Jun 2 01:15 passwd2.9708.2020-06-02@01:17:26~
192.168.43.111 | CHANGED | rc=0 >>
total 12
-rw-r--r--. 1 root root 595 May 28 05:50 fstab
-rw-------. 1 stone bin 23 May 28 05:57 passwd2
-rw-------. 1 stone bin 901 May 28 05:55 passwd2.11265.2020-05-28@05:57:31~
2.5 FETCH模块
[root@ansible ~]# ansible-doc -s fetch
[root@ansible ~]# ansible websrvs -m fetch -a 'src=/var/log/messages dest=/data' -v
Using /etc/ansible/ansible.cfg as config file
192.168.43.111 | SUCCESS => {
"changed": false,
"checksum": "f5bee5935984dd2427fe798f1d1a2696357e0db8",
"dest": "/data/192.168.43.111/var/log/messages",
"file": "/var/log/messages",
"md5sum": "17599230f93cc5e7af26abde6bba637f"
}
192.168.43.112 | SUCCESS => {
"changed": false,
"checksum": "9cc6c413713c660e5a0865763c3ce85541956409",
"dest": "/data/192.168.43.112/var/log/messages",
"file": "/var/log/messages",
"md5sum": "892795e04e9a2c5c6af03a03431cc6b9"
}
[root@ansible ~]# tree /data
/data
├── 192.168.43.111
│ └── var
│ └── log
│ └── messages
├── 192.168.43.112
│ └── var
│ └── log
│ └── messages
2.6 FILE模块
[root@ansible ~]# ansible websrvs -a 'ls -l /data'
192.168.43.112 | CHANGED | rc=0 >>
total 12
-rw-r--r--. 1 root root 595 Jun 2 01:09 fstab
-rw-------. 1 stone bin 23 Jun 2 01:17 passwd2
-rw-------. 1 stone bin 901 Jun 2 01:15 passwd2.9708.2020-06-02@01:17:26~
192.168.43.111 | CHANGED | rc=0 >>
total 12
-rw-r--r--. 1 root root 595 May 28 05:50 fstab
-rw-------. 1 stone bin 23 May 28 05:57 passwd2
-rw-------. 1 stone bin 901 May 28 05:55 passwd2.11265.2020-05-28@05:57:31~
[root@ansible ~]# ansible websrvs -m file -a 'path=/data/fstab owner=stone mode=700 '
192.168.43.112 | CHANGED => {
}
192.168.43.111 | CHANGED => {
}
[root@ansible ~]# ansible websrvs -a 'ls -l /data'
192.168.43.112 | CHANGED | rc=0 >>
total 12
-rwx------. 1 stone root 595 Jun 2 01:09 fstab
-rw-------. 1 stone bin 23 Jun 2 01:17 passwd2
-rw-------. 1 stone bin 901 Jun 2 01:15 passwd2.9708.2020-06-02@01:17:26~
192.168.43.111 | CHANGED | rc=0 >>
total 12
-rwx------. 1 stone root 595 May 28 05:50 fstab
-rw-------. 1 stone bin 23 May 28 05:57 passwd2
-rw-------. 1 stone bin 901 May 28 05:55 passwd2.11265.2020-05-28@05:57:31~
[root@ansible ~]# ansible websrvs -m file -a 'src=/data/fstab path=/data/fstab.link state=link'
192.168.43.111 | CHANGED => {
}
192.168.43.112 | CHANGED => {
}
[root@ansible ~]# ansible websrvs -a 'ls -l /data'
192.168.43.111 | CHANGED | rc=0 >>
total 60
-rwx------. 1 stone root 595 May 28 05:50 fstab
lrwxrwxrwx. 1 root root 11 May 28 06:38 fstab.link -> /data/fstab
192.168.43.112 | CHANGED | rc=0 >>
total 60
-rwx------. 1 stone root 595 Jun 2 01:09 fstab
lrwxrwxrwx. 1 root root 11 Jun 2 01:58 fstab.link -> /data/fstab
[root@ansible ~]# ansible websrvs -m file -a 'src=/data/fstab path=/data/fstab.link state=hard'
192.168.43.111 | SUCCESS => {
}
192.168.43.112 | SUCCESS => {
}
[root@ansible ~]# ansible websrvs -m file -a 'src=/data/fstab path=/data/fstab.link2 state=hard'
192.168.43.112 | CHANGED => {
"uid": 1000
}
192.168.43.111 | CHANGED => {
}
[root@ansible ~]# ansible websrvs -a 'ls -l /data'
192.168.43.112 | CHANGED | rc=0 >>
total 64
-rwx------. 2 stone root 595 Jun 2 01:09 fstab
lrwxrwxrwx. 1 root root 11 Jun 2 01:58 fstab.link -> /data/fstab
-rwx------. 2 stone root 595 Jun 2 01:09 fstab.link2
192.168.43.111 | CHANGED | rc=0 >>
total 64
-rwx------. 2 stone root 595 May 28 05:50 fstab
lrwxrwxrwx. 1 root root 11 May 28 06:38 fstab.link -> /data/fstab
-rwx------. 2 stone root 595 May 28 05:50 fstab.link2
[root@ansible ~]# ansible websrvs -m file -a 'path=/data/f1.txt state=touch'
192.168.43.112 | CHANGED => {
}
192.168.43.111 | CHANGED => {
}
[root@ansible ~]# ansible websrvs -a 'ls -l /data'
192.168.43.112 | CHANGED | rc=0 >>
total 64
-rw-r--r--. 1 root root 0 Jun 2 02:02 f1.txt
192.168.43.111 | CHANGED | rc=0 >>
total 64
-rw-r--r--. 1 root root 0 May 28 06:42 f1.txt
[root@ansible ~]# ansible websrvs -m file -a 'path=/data/f1.txt state=absent' 删除f1.txt文件
192.168.43.111 | CHANGED => {
}
192.168.43.112 | CHANGED => {
}
[root@ansible ~]# ansible websrvs -a 'ls -l /data'
192.168.43.111 | CHANGED | rc=0 >>
total 0
192.168.43.112 | CHANGED | rc=0 >>
total 0
[root@ansible ~]# ansible websrvs -m file -a 'path=/data/ state=absent' 虽然显示失败,但是都删除了,只是最后删除挂载点的时候,不能删除
192.168.43.112 | FAILED! => {
}
192.168.43.111 | FAILED! => {
}
[root@ansible ~]# ansible websrvs -a 'ls -l /data'
192.168.43.111 | CHANGED | rc=0 >>
total 0
192.168.43.112 | CHANGED | rc=0 >>
total 0
2.7 UNARCHIVE模块
[root@ansible ~]# tar zcvf /data/sysconfig.tar.gz /etc/sysconfig
[root@ansible ~]# ll /data
-rw-r--r--. 1 root root 45935 May 28 06:24 sysconfig.tar.gz
[root@ansible ~]# ansible websrvs -a 'ls -l /data'
192.168.43.112 | CHANGED | rc=0 >>
total 0
192.168.43.111 | CHANGED | rc=0 >>
total 0
[root@ansible ~]# ansible websrvs -m unarchive -a 'src=/data/sysconfig.tar.gz dest=/data/ owner=stone mode=700'
192.168.43.111 | CHANGED => {
"src": "/root/.ansible/tmp/ansible-tmp-1590618396.66-12001-29126385423240/source",
}
192.168.43.112 | CHANGED => {
"src": "/root/.ansible/tmp/ansible-tmp-1590618396.65-12002-219899715583476/source",
}
[root@ansible ~]# ansible websrvs -a 'ls -l /data'
192.168.43.111 | CHANGED | rc=0 >>
total 1
drwxr-xr-x. 3 root root 23 May 28 06:26 etc
192.168.43.112 | CHANGED | rc=0 >>
total 1
drwxr-xr-x. 3 root root 23 Jun 2 01:46 etc
[root@ansible ~]# ansible websrvs -m copy -a 'src=/data/sysconfig.tar.gz dest=/data'
192.168.43.111 | CHANGED => {
"src": "/root/.ansible/tmp/ansible-tmp-1590618553.05-12089-18575197678224/source",
}
192.168.43.112 | CHANGED => {
"src": "/root/.ansible/tmp/ansible-tmp-1590618553.02-12090-80512559273179/source",
}
[root@ansible ~]# ansible websrvs -m file -a 'dest=/data/etc state=absent'
192.168.43.112 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"path": "/data/etc",
"state": "absent"
}
192.168.43.111 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"path": "/data/etc",
"state": "absent"
}
[root@ansible ~]# ansible websrvs -a 'ls -l /data'
192.168.43.112 | CHANGED | rc=0 >>
total 1
-rw-r--r--. 1 root root 45935 Jun 2 01:49 sysconfig.tar.gz
192.168.43.111 | CHANGED | rc=0 >>
total 1
-rw-r--r--. 1 root root 45935 May 28 06:29 sysconfig.tar.gz
[root@ansible ~]# ansible websrvs -m unarchive -a 'src=/data/sysconfig.tar.gz dest=/data copy=no'
192.168.43.112 | CHANGED => {
}
192.168.43.111 | CHANGED => {
}
[root@ansible ~]# ansible websrvs -a 'ls -l /data'
192.168.43.112 | CHANGED | rc=0 >>
total 2
drwxr-xr-x. 3 root root 23 Jun 2 01:52 etc
-rw-r--r--. 1 root root 45935 Jun 2 01:49 sysconfig.tar.gz
192.168.43.111 | CHANGED | rc=0 >>
total 2
drwxr-xr-x. 3 root root 23 May 28 06:32 etc
-rw-r--r--. 1 root root 45935 May 28 06:29 sysconfig.tar.gz
2.8 YUM模块
[root@ansible ~]# ansible websrvs -m yum -a 'name=httpd state=present' 安装,默认就是安装
[root@ansible ~]# ansible websrvs -m yum -a 'name=httpd state=absent' 卸载
[root@ansible ~]# ansible websrvs -m yum -a 'name=httpd'
[root@ansible ~]# ansible websrvs -m yum -a 'name=httpd state=started enabled=yes' 启动并设为开机启动
[root@ansible ~]# ansible websrvs -m yum -a 'name=httpd state=stopped' 停止服务
[root@ansible ~]# sed -n 's#^Linten.*#Listen 8080#p' /etc/httpd/conf/httpd.conf
Linten 8080
[root@ansible ~]# sed -i 's#^Linten.*#Listen 8080#p' /etc/httpd/conf/httpd.conf 修改httpd服务默认端口为8080
[root@ansible ~]# ansible websrvs -a 'sed -i "s#^Linten.*#Listen 8080#" /etc/httpd/conf/httpd.conf'
[root@ansible ~]# ansible websrvs -m yum -a 'name=httpd state=started'
[root@ansible ~]# ansible websrvs -a 'ss -ntl' 查看httpd服务已经修改为8080
2.9 USER模块
[root@ansible ~]# useradd -r -s /sbin/nologin -d /data/mysql mysql
[root@ansible ~]# ansible websrvs -m user -a 'name=mysql system=yes home=/data/mysql shell=/bin/false'
192.168.43.111 | CHANGED => {
}
192.168.43.112 | CHANGED => {
}
[root@ansible ~]# ansible websrvs -a 'getent passwd mysql'
192.168.43.112 | CHANGED | rc=0 >>
mysql:x:998:995::/data/mysql:/bin/false
192.168.43.111 | CHANGED | rc=0 >>
mysql:x:998:995::/data/mysql:/bin/false
[root@ansible ~]# ansible websrvs -a 'ls /data -l'
192.168.43.111 | CHANGED | rc=0 >>
total 0
drwx------ 2 mysql mysql 62 Jun 2 22:45 mysql
192.168.43.112 | CHANGED | rc=0 >>
total 0
drwx------ 2 mysql mysql 62 Jun 2 22:45 mysql
[root@ansible ~]# ansible websrvs -a 'ls /data/mysql -la'
192.168.43.111 | CHANGED | rc=0 >>
total 12
drwx------ 2 mysql mysql 62 Jun 2 22:45 .
drwxr-xr-x. 3 root root 19 Jun 2 22:45 ..
-rw-r--r-- 1 mysql mysql 18 Oct 31 2018 .bash_logout
-rw-r--r-- 1 mysql mysql 193 Oct 31 2018 .bash_profile
-rw-r--r-- 1 mysql mysql 231 Oct 31 2018 .bashrc
192.168.43.112 | CHANGED | rc=0 >>
total 12
drwx------ 2 mysql mysql 62 Jun 2 22:45 .
drwxr-xr-x. 3 root root 19 Jun 2 22:45 ..
-rw-r--r-- 1 mysql mysql 18 Oct 31 2018 .bash_logout
-rw-r--r-- 1 mysql mysql 193 Oct 31 2018 .bash_profile
-rw-r--r-- 1 mysql mysql 231 Oct 31 2018 .bashrc
[root@ansible ~]# ansible websrvs -m user -a 'name=mysql2 system=yes home=/data/mysql2 shell=/bin/false create_home=no' 不生成家目录
192.168.43.111 | CHANGED => {
}
192.168.43.112 | CHANGED => {
}
[root@ansible ~]# ansible websrvs -a 'ls /data/mysql2 -la'
192.168.43.111 | FAILED | rc=2 >>
ls: cannot access /data/mysql2: No such file or directorynon-zero return code
192.168.43.112 | FAILED | rc=2 >>
ls: cannot access /data/mysql2: No such file or directorynon-zero return code
[root@ansible ~]# ansible websrvs -m user -a 'name=mysql2 state=absent' 删除用户
192.168.43.112 | CHANGED => {
}
192.168.43.111 | CHANGED => {
}
[root@ansible ~]# ansible websrvs -a 'getent passwd mysql2'
192.168.43.112 | FAILED | rc=2 >>
non-zero return code
192.168.43.111 | FAILED | rc=2 >>
non-zero return code
[root@ansible ~]# ansible websrvs -m user -a 'name=mysql state=absent remove=yes' 删除用户同时移除家目录
192.168.43.112 | CHANGED => {
}
192.168.43.111 | CHANGED => {
}
[root@ansible ~]# ansible websrvs -a 'ls /data/ -la'
192.168.43.111 | CHANGED | rc=0 >>
total 0
drwxr-xr-x. 2 root root 6 Jun 2 22:52 .
dr-xr-xr-x. 21 root root 272 May 16 00:47 ..
192.168.43.112 | CHANGED | rc=0 >>
total 0
drwxr-xr-x. 2 root root 6 Jun 2 22:52 .
dr-xr-xr-x. 18 root root 236 Mar 15 16:02 ..
网友评论