Ansible的安装部署很是简单,其仅依赖于 Python 和 SSH,而系统默认均已安装。html
Ansible 被 RedHat 红帽官方收购后,其安装源被收录在 epel 中,如已安装 epel 可直接 yum 安装。node
经过 pip 和 easy_install 的 Python 第三方包管理器也能够便捷安装 Ansible。python
推荐使用yum方式安装。nginx
[root@localhost]# yum install epel-release -y # 安装epel源 [root@localhost]# yum install ansible -y # yum安装ansible便可,很是简单方便 [root@localhostins]# ansible --version # 验证安装是否成功 [root@localhost]# ansible help # help能够查看ansible的命令参数
[root@localhost]# yum install gcc glibc-devel zlib-devel rpm-build openssl-devel -y # 安装前确保服务器的gcc、glibc开发环境均已安装 [root@localhost]# yum install python-pip python-devel -y # 安装 python-pip 及 python-devel 程序包 [root@localhost]# pip install --upgrade pip # 升级本地 pip 至最新版本 [root@localhost]# pip install --upgrade ansible # 安装ansible
配置文件 | 做用 |
---|---|
/etc/ansible/ansible.cfg | 主配置文件 |
/etc/ansible/hosts | 机器清单,进行分组管理 |
/etc/ansible/roles/ | 存放角色的目录 |
[root@localhost]# vim /etc/ansible/ansible.cfg # 修改主配置文件
[defaults] #inventory = /etc/ansible/hosts # 默认主机文件 #library = /usr/share/my_modules/ # 库文件存放目录 #forks = 5 # 默认开启的并发数 #remote_user = root # 默认palybooks用户 #sudo_user = root # 默认sudo用户 #ask_sudo_pass = True # 是否须要sudo密码 #ask_pass = True # 是否须要密码 #remote_port = 22 # 默认远程主机的端口号 #host_key_checking = False # 是否检查host_key,推荐关闭 #deprecation_warnings=False # 是否开启“不建议使用”警告,该类型警告大多属于版本更新方法过期提醒 #command_warnings=False # 当shell和命令行模块被默认模块简化的时,Ansible 将默认发出警告 #timeout = 10 # 链接主机的时间 #log_path=/var/log/ansible.log # 开启ansible日志 #private_key_file = /path/to/file(/root/.ssh/id_rsa) # 基于密钥认证的文件 [privilege_escalation] # 默认以root身份运行ansible become=True become_method=sudo become_user=root become_ask_pass=False
[root@localhost]# vim /etc/ansible/hosts #修改主机清单配置文件(Inventory)
# Ex 1: Ungrouped hosts, specify before any group headers. #未分组的主机清单 ## green.example.com #定义主机为主机名 ## blue.example.com ## 192.168.100.1 #定义主机为IP ## 192.168.100.10
## [webservers] #组名为webservers的主机 ## alpha.example.org #定义主机为主机名 ## beta.example.org ## 192.168.1.100 #定义主机为IP ## 192.168.1.110 ## www[001:006].example.com #若主机名有规律,也能够范围性的简写
## [dbservers] #同上,此组名为dbserver ## db01.intranet.mydomain.net ## db02.intranet.mydomain.net ## 10.25.1.56 ## 10.25.1.57 ## db-[99:101]-node.example.com
## [all:vars] # 为全部主机设置变量(可用于首次批量推送密钥) ## ansible_ssh_user=wushuaishuai # 用户名 ## ansible_ssh_pass=123456 # 密码
https://docs.ansible.com ansible官方首页可针对性看一些模板和实例web
[root@localhost]# vim /etc/ansible/ansible.cfg # 修改主配置文件 host_key_checking = False # 关闭host_key检查 [root@localhost]# vim /etc/ansible/hosts # 修改主机清单 [test] 10.0.1.7 ansible_ssh_user=root ansible_ssh_pass=123456 ansible_ssh_port=22 10.0.1.8 ansible_ssh_user=root ansible_ssh_pass=123456 ansible_ssh_port=22 #10.0.1.7 # 主机IP #ansible_ssh_user=root # 主机用户 #ansible_ssh_pass=123456 # 主机密码 #ansible_ssh_port=22 # 主机ssh端口
[root@localhost]# ansible test -a "df -h" # 批量查看主机的磁盘分区 10.0.1.8 | SUCCESS | rc=0 >> # test主机组名称 Filesystem Size Used Avail Use% Mounted on # -a指定模块参数 /dev/sda2 30G 2.4G 28G 9% / # "df -h" 输入的命令 devtmpfs 3.9G 0 3.9G 0% /dev tmpfs 3.9G 0 3.9G 0% /dev/shm tmpfs 3.9G 106M 3.8G 3% /run tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup /dev/sda1 497M 81M 417M 17% /boot /dev/sdb1 40G 49M 38G 1% /mnt/resource tmpfs 797M 0 797M 0% /run/user/0 10.0.1.7 | SUCCESS | rc=0 >> Filesystem Size Used Avail Use% Mounted on /dev/sda2 30G 3.4G 27G 12% / devtmpfs 3.4G 0 3.4G 0% /dev tmpfs 3.4G 0 3.4G 0% /dev/shm tmpfs 3.4G 330M 3.1G 10% /run tmpfs 3.4G 0 3.4G 0% /sys/fs/cgroup /dev/sda1 497M 81M 417M 17% /boot /dev/sdb1 281G 65M 267G 1% /mnt/resource tmpfs 696M 0 696M 0% /run/user/0
以wushuaishuai sudo至root用户执行ping存活检测shell
[root@localhost]# ansible all -m ping -u wushuaishuai -b
以wushuaishuai sudo至junpserver用户执行ping存活检测编程
[root@localhost]# ansible all -m ping -u wushuaishuai -b --become-user jumpserver
Playbooks (剧本)与 ad-hoc(临时任务) 相比,是一种彻底不一样的运用 ansible 的方式,是很是之强大的.vim
Playbooks:是一种简单的配置管理系统 ,适合管理配置应用及部署复杂的工做。基于易读的YAML编程语言。centos
[root@localhost]# vim /etc/ansible/ansible.cfg host_key_checking = False
[root@localhost]# ssh-keygen -t rsa -b 2048 -P '' -f /root/.ssh/id_rsa # root用户 [root@localhost]# ssh-keygen -t rsa -b 2048 -P '' -f /home/wushuaishuai/.ssh/id_rsa # 也但是其余用户
[root@localhost ansible]# vim /etc/ansible/hosts # hosts [web] 192.168.77.129 ansible_ssh_pass=1234567 192.168.77.130 ansible_ssh_pass=123456
[root@localhost]# vim ssh-addkey.yml
# ssh-addkey.yml --- - hosts: all gather_facts: no tasks: - name: install ssh key authorized_key: user=root key="{{ lookup('file', '/home/wushuaishuai/.ssh/id_rsa.pub') }}" state=present
[root@localhost]# ansible-playbook -i hosts ssh-addkey.yml
这样,管理节点的公钥就会添加到节点的authorized_keys文件中,再把主机清单里的ansible_ssh_pass去掉,执行ansible all -m ping 就不须要密码了。服务器
注意:须要切换到你生成密钥的用户下才能免密去执行playbook
[root@localhost]# vim nginx.yml
# nginx.yml --- - hosts: test vars: hello: Ansible tasks: - name: yum repo yum_repository: name: nginx description: nginx repo baseurl: http://nginx.org/packages/centos/7/$basearch/ gpgcheck: no enabled: 1 - name: Install nginx yum: name: nginx state: latest - name: Copy nginx configuration file copy: src: /etc/ansible/ansible-playbook/ansible.conf dest: /etc/nginx/conf.d/site.conf - name: Start nginx service: name: nginx state: started - name: Create wwwroot directory file: dest: /var/www/html state: directory - name: Create test page index.html shell: echo "hello {{hello}}" > /usr/share/nginx/html/index.html
附加:须要建立一个nginx的新配置文件,以下:
[root@localhost]# vim ansible.conf #在当前目录下建立nginx新配置文件 server { listen 81; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } }
[root@localhost]# vim /etc/ansible/hosts [test] 10.0.1.7 ansible_ssh_user=root ansible_ssh_pass=123456 ansible_ssh_port=22 10.0.1.8 ansible_ssh_user=root ansible_ssh_pass=123456 ansible_ssh_port=22
[root@localhost ansible-playbook]# ansible-playbook nginx.yml PLAY [test] *************************************************************************************************************************************************** TASK [Gathering Facts] **************************************************************************************************************************************** ok: [10.0.1.8] ok: [10.0.1.7] TASK [yum repo] *********************************************************************************************************************************************** ok: [10.0.1.8] ok: [10.0.1.7] TASK [Install nginx] ****************************************************************************************************************************************** changed: [10.0.1.8] changed: [10.0.1.7] TASK [Copy nginx configuration file] ************************************************************************************************************************** ok: [10.0.1.8] ok: [10.0.1.7] TASK [Start nginx] ******************************************************************************************************************************************** changed: [10.0.1.8] changed: [10.0.1.7] TASK [Create wwwroot directory] ******************************************************************************************************************************* ok: [10.0.1.8] ok: [10.0.1.7] TASK [Create test page index.html] **************************************************************************************************************************** changed: [10.0.1.8] changed: [10.0.1.7] PLAY RECAP **************************************************************************************************************************************************** 10.0.1.7 : ok=7 changed=3 unreachable=0 failed=0 10.0.1.8 : ok=7 changed=3 unreachable=0 failed=0
到agent端查看,nginx安装完成,访问nginx ip:81,页面显示hello Ansible !!!