Ansible常用模块 shell sudo copy file yum user git service setup

感情迁移 提交于 2019-12-13 06:12:44

#https://docs.ansible.com/ansible/latest/user_guide/intro_adhoc.html

 

#shell 执行shell命令

ansible webservers -m shell  -a "ls /tmp"

 

#sodo

#user用户登录之后 sodo root 提权

#注意配置user 具有sudo权限  vim /etc/sudoers

#user    ALL=(ALL)     ALL

ansible webservers -m shell  -a "ls /tmp" -u user  -k --become --become-user root -K

 

#copy 文件传输

src 本地路径 dest 目标主机路径

ansible  webservers -m copy -a "src=/tmp/mysql-shell-8.0.18-linux-glibc2.12-x86-64bit.tar.gz dest=/tmp"

ansible webservers -m shell -a  "ls /tmp"

 

#file

#state类型  file,directory,link,hard,touch,absent

#创建目录

ansible webservers -m file -a "dest=/tmp/abc mode=600 state=directory"

#创建文件

ansible webservers -m file -a "dest=/tmp/abc mode=600 state=touch"

#删除

ansible webservers -m file -a "dest=/tmp/abc mode=600 state=absent"

 

#yum 包管理

#absent,present,installed,removed,latest

ansible webservers -m yum -a "state=installed name=memcached"

 

#user 用户和组

ansible webservers -m user -a "name=foo password=123"

ansible webservers -m user -a "name=foo password=123 shell=/sbin/nologin"

ansible webservers -m user -a "name=foo state=absent"

 

#git 从源代码管理系统部署

 ansible webservers -m git -a "repo=https://github.com/ansible/ansible.git dest=/tmp/ansible"

 

#service 服务管理

ansible webservers -m yum -a "state=installed name=memcached"

ansible webservers -m service -a "name=memcached state=started"

ansible webservers -m service -a "name=memcached state=restarted"

ansible webservers -m service -a "name=memcached state=stopped"

#开机启动

ansible webservers -m service -a "name=memcached  enabled=true"

systemctl list-unit-files|grep enabled 

 

#setup 手机目标主机信息

ansible webservers -m setup

ansible webservers -m setup -a "filter=ansible_nodename"

ansible webservers -m setup -a "filter=ansible_*_mb"

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!