ansible-基本使用-2

ansible  基本模块及常见命令参数使用

命令格式

ansible   webserver|www.a.com|1.1.1.1  (主机组/主机名/主机ip)   -m (模块参数)  shell(模块名字)  -a (命令参数)  “echo xxx”    

远程登录方式

密码方式之免交互
  vi /etc/ansible/hosts

  [node]
  10.11.118.164 ansible_ssh_user=root ansible_ssh_pass=asdf      #主机后面配置用户名密码

 密码方式之交互式输入密码

   ansible webserver -a "df -h" -u tomcat -k

配置面交互方式
  ssh-keygen 生成密钥对,复制到远程主机即可

普通用户登录sudo 提权执行命令

前提是在远程主机上为普通用户tomcat 配置了sudo 的权限

ansible webserver -m shell -a "pwd" -u tomcat -k --become --become-user root -K
-k 交互式的输入tomcat 远程登录密码
-K 交互式输入tomcat sudo 到root 的密码

默认使用模块command

ansible    webserver   -a  "df -h"      #此处省略的是  -m  conmmand 

copy 模块

复制文件的模块
$ ansible atlanta -m copy -a "src=/etc/hosts dest=/tmp/hosts"

 

shell 模块

shell  与command  模块类似,只是shell  模块支持 && >> 等特殊符号,command 不支持
ansible  webserver  -m  shell -a  “echo sss >> /tmp.a.txt”

setup

获取主机信息
ansible webserver  -m setup                    #获取所有主机信息

ansible webserver  -m setup -a "filter=ansible_nodename"  #已经信息的key 名称,进行过滤获取
ansible webserver  -m setup  -a "filter=ansible_*name"    #支持通配符


yum 安装或卸载包

ansible node -m yum -a "name=net-tools state=present"    #state状态决定安装或者卸载

file  文件管理模块

$ ansible webservers -m file -a "dest=/srv/foo/a.txt mode=600"
$ ansible webservers -m file -a "dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan"

user 用户管理模块

$ ansible all -m user -a "name=foo password=<crypted password here>"

$ ansible all -m user -a "name=foo state=absent"

service 服务管理模块

$ ansible webservers -m service -a "name=httpd state=started"
$ ansible webservers -m service -a "name=httpd state=restarted"
$ ansible webservers -m service -a "name=httpd state=stopped"    

$ ansible webservers -m service -a "name=httpd enabled=true"        #设置开机自启
posted @ 2020-07-10 19:45  fanggege  阅读(173)  评论(0编辑  收藏  举报