Ansible安装及简单使用

一、配置免密互通

  参考地址:https://www.cnblogs.com/qq1035807396/p/16998602.html

二、开始安装Ansible

1、添加repo

yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

2、yum安装ansible

yum install ansible -y

3、查看安装信息

rpm -qa ansible

4、添加主机清单

vim /etc/ansible/hosts
[pc_cdh]
master.cdh
slave1.cdh
slave2.cdh
slave3.cdh

5、查看当前ansible都支持哪些模块

ansible-doc -l

6、查看某个模块有哪些参数可以使用

ansible-doc -s 模块名

7、下面比较常用的几个模块:

ansible-doc -s copy
ansible-doc -s file
ansible-doc -s cron
ansible-doc -s group
ansible-doc -s user
ansible-doc -s yum
ansible-doc -s service
ansible-doc -s script
ansible-doc -s ping
ansible-doc -s command
ansible-doc -s raw
ansible-doc -s get_url
ansible-doc -s synchronize

三、模块介绍

1、copy模块

  目的:把主控端/root目录下的s.py文件拷贝到到指定节点上

ansible pc_cdh -m copy -a 'src=/root/s.py dest=/root/'

2、file模块

  目的:更改指定节点上的文件权限为755,属主和属组为root

ansible pc_cdh -m file -a "dest=/root/s.py mode=755 owner=root group=root"

3、cron模块

  目的:在指定节点上定义一个计划任务,每隔3分钟到主控端更新一次时间(先安装yum install ntpdate)

ansible pc_cdh -m cron -a 'name="custom job" minute=*/3 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate -d 12.12.104.220"'

4、group模块

  目的:在所有节点上创建一个组名为nolinux,gid为2014的组

ansible pc_cdh -m group -a 'gid=2014 name=nolinux'

5、user模块

  目的:在指定节点上创建一个用户名为nolinux,组为nolinux的用户

ansible pc_cdh -m user -a 'name=nolinux groups=nolinux state=present'

6、yum模块

  目的:在指定节点上安装 lrzsz 服务

ansible pc_cdh -m yum -a "state=present name=httpd"

7、service模块

  目的:启动指定节点上的 httpd服务,并让其开机自启动

ansible pc_cdh -m service -a 'name=httpd state=restarted enabled=yes'

8、script模块

  目的:在指定节点上执行/root/a.sh脚本(该脚本是在ansible控制节点上的)

ansible pc_cdh -m script -a '/root/a.sh'

9、ping模块

  目的:检查指定节点机器是否还能连通

ansible pc_cdh -m ping

10、command模块

  目的:在指定节点上运行hostname/pwd命令

ansible pc_cdh -m command -a 'hostname'
ansible pc_cdh -m command -a 'pwd'

11、synchronize模块

  目的:将主控方/root/testasible目录推送到指定节点目录下

ansible pc_cdh -m synchronize -a 'src=/root/testasible dest=/root/ compress=yes'

12、shell模块

  目的:在指定节点中执行shell命令

ansible pc_cdh -m shell -a 'uptime' -o
ansible pc_cdh -m shell -a 'date' -o
ansible pc_cdh -m shell -a 'echo "测试asible"' -o
ansible pc_cdh -m shell -a 'echo "测试asible" >> /root/a.sh' -o
ansible pc_cdh -m shell -a 'cat /root/a.sh' -o

 

posted @ 2023-01-05 09:34  宜家数据小哥  阅读(363)  评论(0编辑  收藏  举报