Shenzhen letter lion ansible automated operation and maintenance

First, the basics:
1. Introduction
ansible python-based development, operation and maintenance advantages of a collection of many tools to achieve a batch system configuration, batch deployment, run the batch command functions. ansible is based on the work of the module, it does not have the ability to batch deployment.
Truly batch deployment is ansible operation module, ansible only a framework
(1) connected to plug connection plugins: responsible and a monitoring terminal for communication;
(2) Host Inventory: host specified operation, a profile which defines monitored the host
(3) various modules core module, command modules, custom modules;
(4) by means of a plug-in to complete the mail logging functions;
(. 5) PlayBook: script when executing a plurality of tasks, not necessarily a one-time operation that allows node multiple tasks.

2、特性:
    (1) no agents: 不需要在被管理主机上安装任务agent
    (2) no server: 无服务器端,使用时,直接运行命令即可
    (3) modules in any languages: 基于模块工作,可使用任意语言开发模块
    (4) yaml not code:使用yaml语言定制剧本playbook
    (5) ssh by default:基于SSH工作
    (6) strong multi-tier solution: 可实现多级指挥

3、优点:
    (1) 轻量级,无需在客户端安装agent,更新时,只需要在操作机上进行一次更新即可;
    (2) 批量任务可以写成脚本,而且不用分发到远程就可以执行
    (3) 使用python编写,维护简单
    (4) 支持sudo

Second, the experimental environment
Here Insert Picture Description
required packages Baidu cloud disk --linux in - ansible automated operation and maintenance

Third, the installation ansible
1, define a warehouse source domain
will ansiblerepo folder to the root directory of the Linux
cd /etc/yum.repo.d
RM -rf *
vim ansible.repo
[ansible]
name = ansible
baseurl = File: /// ansiblerepo
Enabled. 1 =
gpgcheck = 0

Clean All yum
2, yum mounted ansible
yum the install -Y ansible
. 3, verify the installation
ansible --version
. 4, ssh Free create interactive logon
ssh-keygen -t rsa
in generating the private key is password protected

ssh-copy-id [email protected]
ssh-copy-id [email protected]

5. Verify avoid dense Login
SSH 192.168.180.101
SSH 192.168.180.102

Fourth, the configuration definition ansible host management inventory ansible--
vim / etc / ansible / hosts
[Web]
192.168.180.101
192.168.180.102

Five, ansible command
1, ansible [Options]
ansible -i / etc / ansible / -m Web hosts the ping
2, ansible-DOC query ansible module documentation
ansible the ping-DOC
3, ansible-PlayBook is performed by the script changes to the remote host
ansible-playbook script name .yml
. 4, ansible-Console interaction tools
ansible Console-
cd // Web host or packet switching by the cd command
list // list the current device

Six modules
command module: Run the remote host does not support the pipe breaks, and redirection shell command
ansible all -m command -a "chdir = / home ls ./"
on all hosts to switch to the next / home, then run ls ./ command
shell module: performs remote host shell command
ansible web -m shell -a 'echo " hello" >> /hello.txt'
packets on all execution shell web host command echo "hello" >> / hello .txt
copy module: copying the file to a specified host specified location remote host
ansible Web -m -a copy "the src = / etc / dest = the hosts / tmp = the nobody owner MODE = 777 = the root Group"
hostname modules: the management of remote host host name
ansible 192.168.180.101 -m hostname -a "name = demo"
host name was changed to 192.168.180.101 Demo
yum module: call yum package management mechanisms to achieve on a remote host
ansible web -m yum -a "name = httpd state = present "
by the host web yum mounted httpd packet
service modules: state management service on a remote host
ansible web -m service -a" name = httpd state = restart enabled = yes "
Httpd service restart packet web host
user modules: a user account on remote hosts
ansible web -m user -a 'name = user1 system = yes password = user1 comment = "test user"'
, respectively, of the host web package create user1 user
ansible web -m user -a "name = user1 remove = yes state = absent"
delete user1 and user home directory

Seven, playbook configuration file
vim /etc/ansible/a.yml

  • hosts: web
    remote_user: root
    tasks:
  • name: adduser
    user: name=user2 state=present
    tags:
    • aaa
  • name: addgroup
    group: name=root system=yes
    tags:
    • bbb
      ...

Script syntax check: ansible-playbook --sybtax-check /etc/ansible/a.yml
pretest: ansible-playbook -C /etc/ansible/a.yml
listed host: ansible-playbook --list-hosts / etc /ansible/a.yml
listed tasks: ansible-playbook --list-tasks /etc/ansible/a.yml
lists tags: ansible-playbook --list-tags /etc/ansible/a.yml
mission: ansible -playbook /etc/ansible/a.yml

Eight, Case
1, create a local yum source

  • hosts: webserver
    remote_user: root
    tasks:
    - name: yuminstall
    shell: rm -rf /etc/yum.repos.d/Cent* && echo -e “[local]\nname=local\nenabled=1\nbaseurl=file:///mnt\ngpgcheck=0” > /etc/yum.repos.d/local.repo && mount /dev/cdrom /mnt && yum clean all && yum repolist
    tags:
    - yum

2, yum install httpd

  • hosts: webserver
    REMOTE_USER: root
    Tasks:
    - name: azhttpd
    yum: name = httpd State = Present
    Tags:
    - azhttpd
    ...
    3, change the default port and restart the httpd service

  • hosts: webserver
    remote_user: root
    tasks:
    - name: change port
    command: sed -i ‘s/Listen\ 80/Listen\ 8080/g’ /etc/httpd/conf/httpd.conf
    notify:
    - cqhttpd
    handlers:
    • name: cqhttpd
      service: name=httpd state=restarted
Published 29 original articles · won praise 0 · Views 585

Guess you like

Origin blog.csdn.net/drrui520/article/details/105214537