module_name =command ##ansible的默认模块是command模块,可是在使用的时候很是的有局限性,建议改为shell模块 host_key_checking = False ##检查对应要控制主机的的host_key,建议取消注释,以减轻管理时须要输入的密码 log_path = /var/log/ansible.log ##ansible的登陆日志文件所在的位置 executable = /bin/sh ##默认登陆到对方用户下面使用的shell版本
green.example.com ##定义单个被管理的主机,能够是FQDN,也能够是IP地址 [webservers] ##把被管理的主机放在一个组中 alpha.example.org www[001:006].example.com ##支持相似通配符写法,此项表明从www001.ex ample.com到www006.ex ample.com 之间的全部主机
因为ansible默认是基于ssh服务来管理主机的,因此首先要在管理的主机上生成公钥文件,并传递给要管理的主机 之上,才能实现基于密钥的管理
[root@localhost ~] ssh-keygen -t rsa ##生成对称密钥,出现提示选择默认便可 Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:06qoPmoSy7UGkKie95RnHn6bPOFEnusk/B0m+/+g8C0 root@localhost.localdomain The key's randomart image is: +---[RSA 2048]----+ | | | | |.. | |+ o | |o S o | |o. . o B | |oo+ .o *++oo . | |o=.+..=.*=OE+ . | |+o=oo..ooB+=oo.. | +----[SHA256]-----+
[root@localhost ~] ssh-copy-id -i 192.168.1.20 ##传递到远程的主机上进行管理 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" The authenticity of host '192.168.1.20 (192.168.1.20)' can't be established. ECDSA key fingerprint is SHA256:htIQABZZdudyHVZbppjWeY2d/pQQ0km8k+i/39SZ04Q. ECDSA key fingerprint is MD5:78:6e:b3:3d:fc:29:b2:b0:fc:2f:6d:d6:ff:3c:63:1a. Are you sure you want to continue connecting (yes/no)? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys root@192.168.1.20's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh '192.168.1.20'" and check to make sure that only the key(s) you wanted were added.
[web] ##给被管理的主机进行分组 192.168.1.19 192.168.1.20 [db] 192.168.1.21
[root@localhost ~] ansible db -m ping ##查看db组中被管理的主机是否在线 192.168.1.21 | SUCCESS => { "changed": false, "ping": "pong" } [root@localhost ~] ansible all -m ping ##all表明全部被管理的主机 192.168.1.21 | SUCCESS => { "changed": false, "ping": "pong" ##若是处于在线状态,会放回一个pong的提示 } 192.168.1.19 | SUCCESS => { "changed": false, "ping": "pong" } 192.168.1.20 | SUCCESS => { "changed": false, "ping": "pong" }
[root@localhost ~] ansible db -m user -a 'name=mysql state=present' ##present表示创建,建立一个用户名为mysql 的用户 192.168.1.21 | CHANGED => { "changed": true, "comment": "", "create_home": true, "group": 1000, "home": "/home/mysql", "name": "mysql", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1000 }
[root@localhost ~] ansible db -m user -a 'name=mariadb state=present system=yes' ##建立一个用户名为mariadb的 系统用户 192.168.1.21 | CHANGED => { "changed": true, "comment": "", "create_home": true, "group": 994, "home": "/home/mariadb", "name": "mariadb", "shell": "/bin/bash", "state": "present", "system": true, "uid": 997 }
[root@localhost ~] ansible db -m user -a 'name=mysql state=absent' ##absent表明移除,删除用户名为mysql的用户 192.168.1.21 | CHANGED => { "changed": true, "force": false, "name": "mysql", "remove": false, "state": "absent" }
[root@localhost ~] ansible db -m group -a 'name=tomcat state=present' ##建立组和建立用户的方法差很少,只是用 的模块上有些差别,此命令为建立一个普通的用户组 192.168.1.21 | CHANGED => { "changed": true, "gid": 1000, "name": "tomcat", "state": "present", "system": false }
[root@localhost ~] ansible db -m group -a 'name=tomcat state=absent' ##移除用户组 192.168.1.21 | CHANGED => { "changed": true, "name": "tomcat", "state": "absent" }
[root@localhost ~] ansible db -m copy -a 'src=/root/test dest=/root/' ##拷贝一个test文件到对方主机的root目录下,src 指定源文件,dest指定目标文件的存放目录 192.168.1.21 | CHANGED => { "changed": true, "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", "dest": "/root/test", "gid": 0, "group": "root", "md5sum": "d41d8cd98f00b204e9800998ecf8427e", "mode": "0644", "owner": "root", "size": 0, "src": "/root/.ansible/tmp/ansible-tmp-1556108167.92-277769296604040/source", "state": "file", "uid": 0 }
[root@localhost ~] ansible db -m yum -a "name=vsftpd" ##安装vsftpd 192.168.1.21 | CHANGED => { "ansible_facts": { "pkg_mgr": "yum" }, "changed": true, "msg": "Repository 'cdrom' is missing name in configuration, using id\n", "rc": 0, ##rc返回值为0表明执行成功 ......
[root@localhost ~] ansible db -m yum -a 'name=vsftpd state=absent' ##删除已安装的软件包 192.168.1.21 | CHANGED => { "ansible_facts": { "pkg_mgr": "yum" }, "changed": true, "msg": "Repository 'cdrom' is missing name in configuration, using id\n", "rc": 0, "results": [ ......
[root@localhost ~] ansible db -m shell -a 'hostname' ##在远程主机上执行hostname命令 192.168.1.21 | CHANGED | rc=0 >> localhost.localdomain
编写一个test脚本mysql
[root@localhost ~] vim test.sh #!/bin/bash wall hello word
不用给建立的脚本执行权限,就可使远程主机执行脚本web
[root@localhost ~] ansible db -m script -a /root/test.sh ##让远程主机执行脚本 192.168.1.21 | CHANGED => { "changed": true, "rc": 0, "stderr": "Shared connection to 192.168.1.21 closed.\r\n", "stderr_lines": [ "Shared connection to 192.168.1.21 closed." ], "stdout": "", "stdout_lines": [] }
[root@localhost ~] ansible db -m file -a 'path=/root/test owner=mariadb mode=700' ##给远程主机的文件设置属主, 和权限 192.168.1.21 | CHANGED => { "changed": true, "gid": 0, "group": "root", "mode": "0700", "owner": "mariadb", "path": "/root/test", "size": 0, "state": "file", "uid": 997 }
[root@localhost ~] ansible db -m file -a 'src=/root/test dest=/root/test-link state=link' 192.168.1.21 | CHANGED => { ##给文件建立软连接,固然也能够建立名为test-link硬连接,须要把link改为hard "changed": true, "dest": "/root/test-link", "gid": 0, "group": "root", "mode": "0777", "owner": "root", "size": 10, "src": "/root/test", "state": "link", "uid": 0 }
[root@localhost ~] ansible db -m shell -a 'rpm -qa | grep crontabs' ##查看被管理的主机是否安装crontabs软件 [root@localhost ~] ansible db -m shell -a 'systemctl status crond' ##查看计划任务服务是否启动 [root@localhost ~] ansible db -m cron -a 'minute=*/5 job="/usr/bin/wall hello word"' ##设置计划任务,每五分钟执行一 次hello word,还能够指定小时,天,月,星期,若是没指定,默认是*
在对方主机上执行查看是否有计划任务sql
[root@localhost ~] crontab -l #Ansible: None */5 * * * * /usr/bin/wall hello word
[root@localhost ~] ansible db -m service -a 'name=httpd state=started' #安装http服务 192.168.1.21 | CHANGED => { "changed": true, "name": "httpd", "state": "started", "status": { "ActiveEnterTimestampMonotonic": "0", "ActiveExitTimestampMonotonic": "0", ......
[root@localhost ~] ansible db -a 'systemctl status httpd' #查看http服务是否启动 192.168.1.21 | CHANGED | rc=0 >> ● httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled) Active: active (running) since Wed 2019-04-24 21:54:56 EDT; 42s ago ......
[root@localhost ~] ansible db -m service -a 'name=httpd state=stopped' #中止http服务 192.168.1.21 | CHANGED => { "changed": true, "name": "httpd", "state": "stopped", "status": { ......