主机名 | wan ip | lan ip | 角色 |
---|---|---|---|
m01 | 10.0.0.61 | 172.16.1.61 | ansible控制端 |
web01 | 10.0.0.7 | 172.16.1.7 | ansible被控端 |
web02 | 10.0.0.8 | 172.16.1.8 | ansible被控端 |
#卸载saltmaster [root@m01 ~]# yum remove -y salt-master salt-minion #安装ansible [root@m01 ~]# yum install -y ansible #查看ansible版本和相关信息 [root@m01 ~]# ansible --version config file = /etc/ansible/ansible.cfg #主配置文件 python version = 2.7.5 # 查看默认主机清单位置及相关 [root@m01 ~]# vim /etc/ansible/ansible.cfg [root@m01 ~]# rpm -q ansible ansible-2.9.9-1.el7.noarch [root@m01 ~]# rpm -ql ansible|grep -v lib /etc/ansible/ansible.cfg #配置文件 /etc/ansible/hosts #ansible默认主机清单文件 /etc/ansible/roles /usr/bin/ansible #在全局环境变量里面,能够TAB和使用相对路径执行 /usr/bin/ansible-2 /usr/share/ansible -- version #ansible版本信息,检查当前使用的是哪一个配置文件 -i #指定主机清单的路径(默认指定/etc/ansible/hosts) -m #使用的模块名称,默认使用command模块(不支持特殊符号,可省略),还有shell模块(支持特殊符号,不过也不太好用) -a #使用的模块参数,模块的具体动做 -k #提示输入ssh密码,而不是基于ssh的密匙认证 -C #模拟执行测试,可是不会真的执行(编译) -T #执行命令超时时间
1.$ANSIBLE_CONFIG #ansible相关的环境变量 2../ansible.cfg #当前工做目录下的 3.~/ansible.cfg #当前用户家目录下的 4./etc/ansible/ansible.cfg #主配置文件 #文件名必须是ansible.cfg,可使用-i切换配置文件
[root@m01 ~]# vim /etc/ansible/ansible.cfg [defaults] #inventory = /etc/ansible/hosts #主机清单 #library = /usr/share/my_modules/ #库文件存放目录 #module_utils = /usr/share/my_module_utils/ #remote_tmp = ~/.ansible/tmp #临时py文件存放在远程主机目录 #local_tmp = ~/.ansible/tmp #plugin_filters_cfg = /etc/ansible/plugin_filters.yml #forks = 5 #默认并发数 #poll_interval = 15 #sudo_user = root #默认sudo用户 #ask_sudo_pass = True #每次执行是否询问sudo的ssh密码 #ask_pass = True #每次执行是否询问ssh密码 #transport = smart #remote_port = 22 #远程主机端口 #module_lang = C #module_set_locale = False #是否跳过检查的主机指纹(与第一次链接时的yes/no有关) #log_path = /var/log/ansible.log #ansible日志 #普通用户提权操做 [privilege_escalation] #become=True #become_method=sudo #become_user=root #become_ask_pass=False #跳过检查的主机指纹, 不优化就不能直接使用ansible远程链接,因此服务端必定要作 (不作的话就会显示know_hosts报错,不打开的话也能够,不过要先使用ssh挨个链接一遍,才能使用ansible来管理客户端) 优化后能够直接使用ansible远程链接 [root@m01 ~]# vim /etc/ansible/ansible.cfg #改成 host_key_checking = False
/etc/ansible/hosts 是ansible默认主机资产清单文件,用于定义被管理主机的认证信息,例如ssh登陆用户名,密码以及key信息python
inventory文件中填写须要被管理的主机与主机组信息,还能够自定义inventory主机清单的位置,使用 -i 指定文件位置便可web
ansible的链接可使用本地的缓存redis
[root@m01 ~]# vim /etc/ansible/hosts #[]标签名任意,可是最好不要用特殊符号和大写字母,中文 #端口是22的时候能够省略 [web_group] 172.16.1.7 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1' 172.16.1.8 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1' 172.16.1.9 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1' [db_group] 172.16.1.51 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1' 172.16.1.52 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1' 172.16.1.53 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1' 172.16.1.54 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1' [nfs_group] 172.16.1.31 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1' [redis_group] 172.16.1.81 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1' [lb_group] 172.16.1.5 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1' 172.16.1.6 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1' [backup_group] 172.16.1.41 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1' [zabbix_group] 172.16.1.71 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1' [m01_group] 172.16.1.61 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1' [mtj_group] 172.16.1.202 ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'
支持正则shell
#主机名(至关于注释)不等于主机名,ansible命令指定的主机名是在hosts文件中的主机名 [root@m01 ~]# vim /etc/ansible/hosts [web_group] web0[1:2:3] ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1' #须要作本地域名解析 [root@m01 ~]# vim /etc/hosts 172.16.1.7 web01 172.16.1.8 web02 172.16.1.9 web03
#主机名(至关于注释)不等于主机名 [root@m01 ~]# vim /etc/ansible/hosts [web_group] web01 ansible_ssh_host='172.16.1.7' ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1' web02 ansible_ssh_host='172.16.1.8' ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1' web03 ansible_ssh_host='172.16.1.9' ansible_ssh_port=22 asible_ssh_user=root ansible_ssh_pass='1'
使用变量指定密码vim
[root@m01 ~]# vim /etc/ansible/hosts [web_group] web01 ansible_ssh_host=172.16.1.7 web02 ansible_ssh_host=172.16.1.8 web03 ansible_ssh_host=172.16.1.9 [db_group] db01 ansible_ssh_host=172.16.1.5 db02 ansible_ssh_host=172.16.1.6 #指定web_group组的主机密码,等于把密码加入到主机组 [web_group:vars] ansible_ssh_pass='1' [db_group:vars] ansible_ssh_pass='1'
#自定义标签组或者(合并组),新组名为install_rsync和install_nfs [root@m01 ~]# vim /etc/ansible/hosts [install_rsync:children] web_group nfs_group [install_nfs:children] web_group nfs_group
经过密匙认证客户端缓存
1.建立密钥对 [root@m01 ~]# ssh-keygen 2.推送公钥 [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.5 [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.6 [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.7 [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.8 [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.9 [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.31 [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.41 [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.51 [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.52 [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.53 [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.54 [root@m01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@172.16.1.61 3.编辑主机清单 [root@m01 ~]# vim /etc/ansible/hosts [web_group] web01 ansible_ssh_host=172.16.1.7 asible_ssh_user=root ansible_ssh_port=22 web02 ansible_ssh_host=172.16.1.8 asible_ssh_user=root ansible_ssh_port=22 web03 ansible_ssh_host=172.16.1.9 asible_ssh_user=root ansible_ssh_port=22
#检查服务端和客户端是否能够连通(是真正能够链接的,受控端状态异常的话报错) 只能指定标签名,不能指定主机名 [root@m01 ~]# ansible 'web_group' -m ping 172.16.1.8 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" #缓存和使用命令 }, "changed": false, "ping": "pong" #ping pong 至关于模块之间的暗号 } 172.16.1.7 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } 172.16.1.9 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } #查看能够远程链接的主机(不是真正能够链接的,只是显示本地记录的) [root@m01 ~]# ansible 'all' --list-host hosts (11): 172.16.1.5 172.16.1.6 172.16.1.31 172.16.1.7 172.16.1.8 172.16.1.9 172.16.1.51 172.16.1.52 172.16.1.53 172.16.1.54 172.16.1.81 [root@m01 ~]# ansible 'web_group' --list-host [root@m01 ~]# ansible 'web01' --list-host [root@m01 ~]# ansible '10.0.0.7' --list-host [root@m01 ~]# ansible 'all' --list-host [root@m01 ~]# ansible 'lnmp' --list-host [root@m01 ~]# ansible '*' --list-host #显示的是默认主机清单中的主机 hosts (11): 172.16.1.5 172.16.1.6 172.16.1.31 172.16.1.7 172.16.1.8 172.16.1.9 172.16.1.51 172.16.1.52 172.16.1.53 172.16.1.54 172.16.1.81 # -i指定自定义主机清单,自定义操做(通常用于临时批量操做) # --list-host只显示主机清单中的第一列 [root@m01 ~]# ansible '*' -i /root/hosts --list-host hosts (5): web01 web02 web03 db01 db02
#查看模块语法 [root@m01 ~]# ansible-doc command EXAMPLES: #查看支持的模块个数 [root@localhost ~]# ansible-doc -l |wc -l
绿色:表示被管理端主机没有被控制端修改(执行成功)bash
黄色:表示被管理端主机发生变动(执行成功)并发
红色:表示出现了故障,注意看提示ssh
粉色:警告(说明命令的执行有可能行,有可能不行)测试
#卸载httpd [root@m01 ~]# yum remove -y httpd #编辑yml语法(当心2468空格,不能直接TAB) [root@m01 ~]# vim httpd.yml - hosts: web_group tasks: - name: Install Httpd Server #随便写,只是个标记 yum: name: httpd state: present #检查yml语法 [root@m01 ~]# ansible-playbook --syntax-check httpd.yml playbook: httpd.yml #控制端执行 1.工做目录清单优先级 > 家目录下清单优先级 > 默认主机清单优先级(?) 2. -C 指定要执行的yml语法文件 [root@m01 ~]# ansible-playbook -C httpd.yml PLAY [web_group] ****************************************************************** TASK [Gathering Facts] ****************************************************************** ok: [172.16.1.8] ok: [172.16.1.7] ok: [172.16.1.9]