ansible命令详解

1.comand模块 (默认模块)

用于在远程主机(被管理的主机)上执行某个命令
注:A. 不能变量$HOME和操作等"<",">","|",";“和”&"

在这里插入图片描述

#在默认家目录下创建一个 ansible.txt的文件
[root@cdhnode1 ~]# ansible   all -m command   -a "   touch   ansible.txt "
#指定切换到/home/zx目录下,再创建一个ansible.txt的文件
[root@cdhnode1 ~]# ansible   all  -a "chdir=/home/zx   touch  ansible.txt   "
[root@cdhnode1 ~]# ansible   all -m command   -a "  creates=/root/ansible.txt   touch  ansible2.txt   "
# 当/home/zx/ansible.txt文件存,就执行删除操作
[root@cdhnode1 ~]# ansible   all -m command   -a " chdir=/home/zx  removes=ansible.txt   rm   ansible.txt "

2.copy模块
把管理端的数据分发到被管理端服务器上
在这里插入图片描述

[root@cdhnode1 ~]# ansible   all -m copy -a "src=/root/test.txt     dest=/root/ "
[root@cdhnode1 ~]# ansible   all -m copy -a "src=/root/test.txt     dest=/root/   owner=ldg  group=ldg  backup=yes   mode=600 "
##创建一个密码文件
[root@cdhnode1 ~]# ansible   all -m copy -a " content=123456    dest=/root/mytest.password "

3.shell模块( 万能模块)

   功能: 在被管理机中执行操作

   注: 1.没有 command 模块的限制

           2.参数列表与command 相拟

4.script 模块
需求,要在我管理的主机上批量创建用户

要在管理机上执行批量创建用户的脚本

以前要执行的方式:

a.创批量创建用户的脚本 ----addUsers.sh

b. 分发到各个机子上

c.设置执行权限 chmod a+x

d.执行要脚本

使用 ansible在管理机上编写好脚本,会自动分发并执行

注: 参数列表功能与command模块相拟

[root@cdhnode1 ~]# ansible all -m script  -a "/root/test.sh  "
#!/bin/bash
m=1
for i in $( cat ./class.txt )
do
 user_name=$( echo "$i" | awk -F:  '{print $1} ')
 user_id=$( echo "$i" | awk -F:  '{print $2} ')
 user_passwd=$( echo "$i" | awk -F:  '{print $3} ')
 useradd  ${user_id} -c  ${user_name}
 echo ${user_passwd} | passwd  ${user_id} --stdin &> /dev/null
 echo "创建...${m}个学生用户 ${user_name} 帐号为: ${user_id}  密码为:${user_passwd}  成功..."
 (( m=m+1 ))
done
echo "批量创建用户完成"

#!/bin/bash
echo "删除用户操作"
for i in $( cat ./class.txt )
do
  user_id=$( echo ${i} | awk -F : '{print $2}' )
  userdel ${user_id} -r
  echo "删除用户:${user_id} 成功..."
done
echo "批量删除用户成功!"

[root@cdhnode1 ~]# cat class.txt
陈玲:test1:268808::
高攀:test2:213184::
敖容:test3:10446X::

a.分发 class.txt

b.管理机上ansible 来执行addUser.sh 脚本

5.user模块
管理用户帐号的模块
在这里插入图片描述
在这里插入图片描述


#在每个被管理的主机上创建名为user_ldg的用户
[root@cdhnode1 ~]# ansible  all -m user   -a " name=user_ldg  comment='my create user'  "


#删除指定用户 
[root@cdhnode1 ~]# ansible  all -m user   -a " name=user_ldg    state=absent  "


#创建一个用户 并指定密码( 加密过后密文 )
[root@cdhnode1 ~]# ansible  all -m user   -a 'name=user_ldg2   password=$6$ukkMjpa8$qLqXwaKzyhCdBU9WapXwWSviUFnP92338GhIb5/LO2AXLUW.PVnHCIMMXq4FDyhAPKoBF/Degii/.S8H26Qbp.'

6. file模块

主要用于对文件/目录操作,如:属性,创建文件/目录,创建链接文件,删除等操作
在这里插入图片描述
在这里插入图片描述

#递归创建/root.ansible_test/A 目录
[root@cdhnode1 ~]# ansible all -m file  -a " dest=/root/ansible_test/A  state=directory  "


#递归创建/root.ansible_test/A 目录
[root@cdhnode1 ~]# ansible all -m file  -a " dest=/root/ansible_test/A/test1.txt state=touch  "

#创建文件,并指定所有者或所属组
[root@cdhnode1 ~]# ansible all -m file  -a " dest=/root/ansible_test/A/test2.txt state=touch owner=ldg  group=ldg   "


#删除文件
[root@cdhnode1 ~]# ansible all -m file  -a " dest=/root/ansible_test/A/test2.txt state=absent  "

#递归删除/root/ansible_test/A
[root@cdhnode1 ~]# ansible all -m file  -a " dest=/root/ansible_test/A   state=absent  "



#创建软连接文件
[root@cdhnode1 ~]# ansible all -m file  -a " src=/root/ansible_test/A/test1.txt  dest=/root/ansible_test/A/test1.txt.ln   state=link  "

对目录能不能创建硬连接 ???? 不能

对文件能不能创建硬连接 ?? 能

7 cron 模块
用于管理定时计划任务模块 类拟于crontab -e 命令操作
注: 1.建议给任务指定个name计划名,方便执行删除操作

2.如果不指定计划的时间,那计划时间五个参数默认都为星号

#创建任务计划任务  计划任务名为:默认为None 
[root@cdhnode1 ~]# ansible all -m cron -a "  job='/usr/bin/date  >> /root/a.txt'    "

#创建一个任务计划,并指定名称为:create_date
[root@cdhnode1 ~]# ansible all -m cron -a "  job='/usr/bin/date  >> /root/a.txt'   name=create_date   "

#删除指定的计划名为create_date
[root@cdhnode1 ~]# ansible all -m cron -a " name=create_date   state=absent  "

#对create_date这个计划进行修改前进行备份,备份至/tem/cronXXXXXX文件中
[root@cdhnode1 ~]# ansible all -m cron -a "  minute=0  hour=23   weekday=0     job='/usr/bin/date  >> /root/a.txt'   name=create_date   backup=yes   "

8.yum模块
yum源软件管理(安装,卸载,更新)

#卸载
[root@cdhnode1 ~]# ansible all -m yum   -a " name=tree state=absent     "
[root@cdhnode1 ~]# ansible all -m yum   -a " name=tree state=removed"



#安装
[root@cdhnode1 ~]# ansible all -m yum   -a " name=tree state=installed "
[root@cdhnode1 ~]# ansible all -m yum   -a " name=tree state=present "

9.fetch模块
拉取文件 从远程的被管理机复制文件到管理机

 #从远程的被管理机上拉取test.txt文件,存入到本机(管理机中)拉取的文件分别存到各自ip地址所对应的目录中
[root@cdhnode1 192.168.100.52]#  ansible all  -m fetch  -a " src=/root/test.txt   dest=/root/test    "

10.service服务模块

#启动服务
[root@cdhnode1 ~]#  ansible all  -m service   -a " name=crond  state=started   "

#重启
[root@cdhnode1 ~]#  ansible all  -m service   -a " name=crond  state=restarted   "


#关闭服务
[root@cdhnode1 ~]#  ansible all  -m service   -a " name=crond  state=stopped   "


#把服务加入开机启动
[root@cdhnode1 ~]#  ansible all  -m service   -a " name=crond  enabled=yes  "
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值