ansible下载配置及常见模块使用,ansible配置模块


1.安装配置:
yum install ansible
主配置文件:/etc/ansible/ansible.cfg
主机清单:/etc/ansible/hosts
三个主程序:
ansible,absible-doc,ansible-playbook
2.定义主机清单:
vim /etc/ansible/hosts
1
可按组来分类
3.使用ssh秘钥分发至定义好的主机:
ssh-keygen -t rsa
ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.18.224.101
ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.18.224.102
4.ansible用法:
ansible [-m module_name] [-a args]
:可为all,表示主机清单定义的所有主机,也可为单个ip地址,也可以使用主机清单中定义的主机组名,也可以指定多台主机
-m:指定模块名称,常用模块有,copy,command(默认模块),cron,user,group,script,shell,setup(查看远程主机信息)
5.ansible模块查看:
ansible-doc -l 列出ansible所有模块
查看指定模块用法
ansible-doc -s copy 查看copy模块
2
6.ansible查看远程主机是否可ping:
ansible all -m ping
3
返回pong表示主机都能ping通
7.在定义好的远程主机上执行命令:
ansible web -a 'date'
command模块为默认模块,可省略
8.ansible shell模块:
当command模块中含有管道符或者是变量引用时,将不能正确执行。这个时候就得使用-m shell 模块来完成.
如,ansible all -m shell -a 'echo 123456|passwd --stdin user1'
9.user模块创建用户与用户组:
ansible all -m group -a'name=mysql gid=306 system=yes'
ansible all -m user -a 'name=mysql uid=306 system=yes group=mysql'
移除用户或用户组:ansible web -m user -a 'state=absent name=user1'
10.copy模块
4
ansible all -m copy -a 'src=/etc/fstab dest=/tmp/fstab.ansible'
src:源路径
dest:远程主机路径
11.ansible all -m set up:查看远程主机的信息
12.ansible all -m service -a 'name=httpd enabled=yes':设定服务是否开机启动
name=:服务名称,如httpd
enabled:yes 为开机自启动,no为开机不自启动
13.absible all -m script -a 'test.sh' :
将本地的tesh.sh在远程主机上运行(脚本所在路径只能使用相对路径)
test.sh内容
5
14.cron定时任务模块:
6
ansible all -m cron -a 'minute=*/10 job="ntpdate 172.18.224.100 &>/dev/null " name=test'
minute,hour,month,day,weekday:默认为*可自己指定
job:可为命令或者提前定义好的脚本
name:该定时任务的名称,不可省略
state:默认为present,添加定时任务,absent删除
删除定时任务:
ansible all -m cron -a 'name=test state=absent'
7
15.fetch:从远程拉取文件
ansible 172.18.224.101 -m fetch -a 'src=/apache-tomcat-8.5.35.tar.gz dest=/'
src只能为文件,不能为目录
dest只能为目录

相关内容