Ansible批量管理

Ansible安装

[root@ansible ~]# yum -y install ansible
[root@ansible ~]# ansible --version
ansible 2.8.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
Ansible配置
[root@ansible_controller ~]# vim /etc/ansible/hosts
[webserver]                                         #主机组webserver
192.168.161.[154:155]                       #集合模式书写IP
[webserver:vars]                                #声明webserver组的变量
ansible_ssh_port = 22
ansible_ssh_pass = '1'
​
[dbserver]
192.168.161.156
[dbserver:vars]
ansible_ssh_port = 22
ansible_ssh_pass = '1'
​
[server:chilren]                                #设置总组(下面包含着子组)
webserver
dbserver
[root@ansible_controller ~]# vim /etc/ansible/ansible.cfg
inventory      = /etc/ansible/hosts             #主机组设置
library        = /usr/share/my_modules/     #模块存放目录
remote_tmp     = ~/.ansible/tmp                     #远程临时目录
local_tmp      = ~/.ansible/tmp                     #本地临时目录
forks          = 5                                              #并发线程数
sudo_user      = root                                           #远程执行命令的用户
remote_port    = 22                                             #远程ssh连接端口

Ansible Ad-Hoc
##执行命令
ansible webserver -a 'hostname'
​
##并行执行Ad-hoc<-f>
ansible webserver -a 'ss -anptu' -f 5
​
##指定模块去工作<-m>
ansible webserver -m shell -a 'echo $TERM'
​
##copy模块
ansible webserver -m copy -a "src=/etc/hosts dest=/tmp/hosts"
​
##file模块
ansible webserver -m file -a "dest=/tmp/hosts mode=600"
ansible webserver -m file -a "dest=/tmp/hosts mode=600 owner=lsy group=lsy"
ansible webserver -m file -a "dest=/tmp/lsy/pp/1 mode=755 owner=lsy group=lsy state=directory"
ansible webserver -m file -a "dest=/tmp/lsy/pp/1 state=absent"
​
##yum模块
ansible webserver -m yum -a "name=vim state=present"
ansible webserver -m yum -a "name=vim state=latest"
ansible webserver -m yum -a "name=vim state=absent"
​
##git模块
ansible webserver -m git -a "repo=git://github.com/lsy/pybasis.git dest=/usr/myapp version=HEAD"
​
##service模块
ansible webserver -m service -a "name=httpd state=started"
ansible webserver -m service -a "name=httpd state=restarted"
​
##获取fact信息
ansible all -m setup
​
##script模块
ansible webserver -m script -a "/tmp/myapp/cpu_load.sh"

Ansible PlayBook(部署LNMP环境:单机)
[root@ansible_controller ~]# mkdir -p /etc/ansible/playbook/template
[root@ansible_controller ~]# vim /etc/ansible/playbook/template/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
​
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
[root@ansible_controller ~]# vim /etc/ansible/playbook/deployLNMP.yaml
---
- hosts: webserver
  vars:
    domainName: www.lsycc.com
    webPort: 80
  remote_user: root
  tasks:
  - name: Add nginx deploy yumrepo.
    template: src=/etc/ansible/playbook/template/nginx_configfile.j2 dest=/etc/yum.repos.d/nginx.repo
    
  - name: Install epel yumrepo.
    yum: name=epel-release state=latest
​
  - name: Install LNMP service software.
    yum: name=nginx state=latest
​
  - name: Modify nginx configfile.
    template: src=/etc/ansible/playbook/template/nginx_vhost.j2 dest=/etc/nginx/conf.d/default.conf
    notify:
      - restart nginx service.
  - name: Install php env.
    yum: name={{ item }} state=latest
    with_items:
      - php
      - php-fpm
      - php-mysql
      - php-devel
      - php-mbstring
      - php-mcrypt
      - php-gd
      - php-pear
    notify:
      - restart php-fpm service.
  handlers:
    - name: restart nginx service.
      service: name=nginx state=restarted enabled=yes
    - name: restart php-fpm service.
      service: name=php-fpm state=restarted enabled=yes
​
- hosts: dbserver
  remote_user: root
  tasks:
  - name: Install mariadb database service.
    yum: name={{ item }} state=latest
    with_items:
      - mariadb-server
      - mariadb
    notify:
      - restart mariadb service.
​
  handlers:
    - name: restart mariadb service.
      service: name=mariadb state=restarted enabled=yes
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

好好学技术oH

你的鼓励是一起学习的动力何阶梯

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值