1、user模块
1、ansible-doc 创建
[devops@master ansible]$ ansible node1 -m user -a "name=rhce comment=rhcsa"
node1 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"},"changed": true,"comment": "rhcsa","create_home": true,"group": 1001,"home": "/home/rhce","name": "rhce","shell": "/bin/bash","state": "present","system": false,"uid": 1001
}
[devops@master ansible]$ ansible node1 -m shell -a "grep rhce /etc/passwd"
node1 | CHANGED | rc=0 >>
rhce:x:1001:1001:rhcsa:/home/rhce:/bin/bash
2、写剧本编写
[devops@master ansible]$ ansible-doc user
二、文件相关模块
1、file模块
-
主要就是用于被控主机文件的操作
-
**文件,目录,硬链接,软连接
[root@master ~]# ansible-doc user
1、file模块参数
-
path 文件的路径
-
state 状态,touch,directory,absent,link,hard
-
mode 文件的权限 0644
-
owner 文件的拥有人
-
group 文件的拥有组
-
src 被控节点文件原文件
-
dest 被控节点对于这个原文件做一个软连接或者硬链接,通常配合 state=link或者hard使用
[devops@master ansible]$ ansible node1 -m file -a " src=/etc/hosts dest=/mnt/hosts state=link "
2、copy模块
-
主要就是将主控节点的文件拷贝到被控节点上面去
-
src 主控节点上面的文件
-
dest 将主控节点的文件拷贝到被控节点上
-
backup 如果拷贝的时候有相同的文件的话,可以对其进行一个备份的操作,yes,默认的情况下是不会进行备份的操作
-
content 写的内容直接拷贝到被控节点上面去,类似于重定向的操作
-
remote_src yes, 将被控节点的文件拷贝到被控节点上面去
[devops@master ansible]$ ansible node1 -m copy -a "src=/etc/hosts dest=/tmp/"# 将被控节点的文件拷贝到被控节点上面去
[devops@master ansible]$ ansible node1 -m copy -a "remote_src=yes src=/tmp/f1.txt dest=/mnt/f1.txt"# 写的内容直接拷贝到被控节点上面去
[devops@master ansible]$ ansible node1 -m copy -a "content='echo 123' dest=/tmp/f1.txt "
3、fetch模块
-
被控节点的文件传输到主控节点上面
-
拷贝到主控节点上面的表现形式是一个目录,是一个套娃的操作,以被控节点的主机名为目录命名
-
只能拷贝文件,不能拷贝目录
-
flat yes,单纯的拷贝一个文件,不会被套娃的目录出现,dest=后面必须有一个/,才有用,默认为no
# 没有flat的话,就会以目录的形式存在,被控主机名为目录
[devops@master ansible]$ ansible node1 -m fetch -a "src=/etc/hosts dest=/tmp"
# 添加 flat=yes的话,就只需要拷贝文件到主控节点即可
# 然后这个dest后的话,需要添加/这个才行[devops@master ansible]$ ansible node1 -m fetch -a "src=/etc/hosts dest=/tmp/ flat=yes"
三、软件相关的模块
1、yum_repository
-
配置yum仓库
-
name 仓库名称
-
description 才是描述信息
-
baseurl 仓库地址
-
enabled 开启仓库
-
file 文件名 不需要写.repo,会自动的加上这个后缀,如果要在一个文件中多次的添加yum源的话,就需要file了
-
state present 默认创建,absent删除
# 编写了一个appstream的仓库后# 再次添加到同一个文件的话,就只需要指定 file=文件名即可
2、yum模块
-
主要就是安装软件包的
-
state present 安装,absent删除,lastet安装最新的版本
-
name 软件名, @包名 就是安装软件包组
四、系统服务相关的模块
1、service模块
-
启动服务等操作
-
name 服务的名字
-
state started,stopped,reloaded
-
enabled yes就是开启自启,no就是开机不自动起来
[devops@master ansible]$ ansible node1 -m service -a "name=vsftpd state=started enabled=yes"
2、systemd模块
- 与上面的模块类似操作
五、用户模块
1、user模块
-
创建用户或者删除用户
-
name 用户名
-
comment 用户描述信息
-
shell 登录shell
-
create_home yes创建家目录,默认是创建在/home下面
-
password 需要进行加密,不能明文输入
-
groups 添加附加组,需要跟append 来配合使用
-
remove yes 然后加上state=absent 完全删除了用户
# 彻底删除一个用户
[devops@master ansible]$ ansible node1 -m user -a "name=rhce state=absent remove=yes"
node1 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"},"changed": true,"force": false,"name": "rhce","remove": true,"state": "absent"
}
# 将一个用户添加到其他的附属组中# 如果只是单纯的使用groups这个组的话,只能添加一个用户# 因此的话需要使用append=yes,这样的话,添加到多个附属组中去了[devops@master ansible]$ ansible node1 -m user -a "name=rhce groups=apache append=yes
2、group模块
- 创建一个组
六、计划任务模块
1、cron模块
-
name 计划任务的描述信息
-
job 执行的命令
-
minute 每分钟执行
-
user 以哪一个用户执行
-
weekday 每周
-
hour 每小时
-
cron_file 写在哪一个配置文件中 /etc/crontab ,需要加上user这个参数才行
[devops@master ansible]$ ansible node1 -m cron -a "name='echo 123' minute=1,5,10 state=present job='echo 123' cron_file=/etc/crontab user=root"
node1 | CHANGED => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"},"changed": true,"cron_file": "/etc/crontab","envs": ["SHELL","PATH","MAILTO"],"jobs": ["echo 123"]
}