三、Ansible自动化运维实战-Ad-Hoc-点对点模式

1.简介

  临时的,在ansible中是指需要快速执行的单挑命令,并且不需要保存的命令。对于复杂的命令则playbook。

 

2.复制模块

  帮助:ansible-doc copy

  案例:把hostlist这个文件copy到别的机器上

  ansible webserver -m copy -a 'src=/root/hostlist dest=/tmp/ owner=root group=bin mode=700'

  ansible 主机清单 -m调用模块 模块名copy -a属性 'source资源 destination目的地 O主人 G组 权限'

  如果别的机器上已经有了那个文件,就会覆盖。

  如果文件内容发生了改变但名字没变的情况下,需要敲backup=yes进行备份,这样就可以让两个文件同时存在

  ansible webserver -m copy -a 'src=/root/hostlist dest=/tmp/ owner=root group=bin mode=700 backup=yes'

  

 

3.用户模块

   帮助  ansible-doc user

  创建用户

    ansible webserver -m user -a 'name=qianfeng state=present'

    ansible主机清单 调用user模块 -a属性 '用户名字name=qf 状态state=创建'

    

 

 

  修改密码

    生成加密密码

    echo '123456' | openssl passwd -1 -stdin  生成加密密码值

    输出123456传给管道openssl 以passwd密码的方式加密 -1是密码的类型 -标准输入输出(就是不跟用户会话了)

    

 

    $1$xhIahwTd$uYy3VSUsRNDwQgSa6DmJo0  获得加密后的密码

    ansible webserver -m user -a 'name=qf password="$1$xhIahwTd$uYy3VSUsRNDwQgSa6DmJo0"'  把加密后的密码传给清单主机中的每个qf用户,其密码是123456

    

 

 

  修改shell

     修改shell可以让该用户无发登录

    ansible webserver -m user -a 'name=qf shell=/sbin/nologin append=yes'

    

  删除用户

    ansible webserver -m user -a 'name=qf state=absent'  状态state=删除

 

4.软件包管理

  帮助  ansible-doc yum 

  ansible webserver -m yum -a 'name="*" state=latest'  升级所有包(基本不可能用)

  ansible webserver -m yum -a 'name="httpd" state=latest'  安装apache

  ansible webserver -m yum -a 'name="vsftpd" state=absent'  删除apache

 

5.服务模块

  ansible-doc service  帮助

  ansible webserver -m service -a 'name=httpd state=started'  启动服务httpd

  ansible webserver -m service -a 'name=httpd stste=stopped'  停止服务httpd

  ansible webserver -m service -a 'name=httpd state=started enabled=yes'  启动服务httpd并开机启动

 

6.文件模块

  ansible webserver -m file -a 'path=/tmp/88.txt mode=777 state=touch'  创建文件8.txt并且赋予指定权限

  ansible webserver -m file -a 'path=/tmp/99 mode=771 state=directory'  创建目录99并赋予指定权限

 

7.收集模块

  ansible-doc setup  帮助

  ansible host1 -m setup  收集host1主机的信息

  ansible host1 -m setup -a 'filter=ansible_all_ipv4_addresses'  手机host1主机过滤出所有ipv4地址的信息

 

8.shell模块

  ansible webserver -m shell -a 'hostname' -o  查看主机名字 -o简洁执行

  ansible webserver -m shell -a 'hostname' -o -f 2  -f 是指定线程数执行

  ansible webserver -m shell -a 'yum install -y vsftpd' -o  安装vsftpd服务

  ansible webserver -m shell -a 'yum remove -y vsftpd' -o  卸载vsftpd服务

  ansible webserver -m shell -a 'df -hT' -o  查看挂载点

  ansible webserver -m shell -a 'touch /tmp/777.txt' -o  创建文件777.txt

  

 

posted @   真渡  阅读(77)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示