一文详解Ansible自动化运维
- - DockOne.ioAnsible是近年来越来越火的一款开源运维自动化工具,通过Ansible可以实现运维自动化,提高运维工程师的工作效率,减少人为失误. Ansible通过本身集成的非常丰富的模块可以实现各种管理任务,其自带模块超过上千个. 更为重要的是,它操作非常简单,即使小白也可以轻松上手,但它提供的功能又非常丰富,在运维领域,几乎可以做任何事.
[root@centos01 ~]# cd /mnt/ansiblerepo/ansiblerepo/repodata/
[root@centos01 ansiblerepo]# vim /etc/yum.repos.d/local.repo
[local]
name=centos
baseurl=file:///mnt/ansiblerepo/ansiblerepo
enabled=1
gpgcheck=0
[root@centos01 ~]# yum -y install ansible
[root@centos01 ~]# ansible --version
ansible 2.3.1.0
config file = /etc/ansible/ansible.cfg
configured module search path = Default w/o overrides
python version = 2.7.5 (default, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
[root@centos01 ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
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:cJz6NRTrvMDxX+Jpce6LRnWI3vVEl/zvARL7D10q9WY root@centos01
The key's randomart image is:
+---[RSA 2048]----+
| . . .|
| . . + oo|
| . = o o. oo|
| = * o..+ *|
| . S *.=+=*+|
| . o =+XooE|
| . ..=.++.|
| ..o ..|
| .. o. |
+----[SHA256]-----+
[root@centos01 ~]# ssh-copy-id -i .ssh/id_rsa.pub [email protected]
[root@centos01 ~]# ssh-copy-id -i .ssh/id_rsa.pub [email protected]
[root@centos01 ~]# ansible -i /etc/ansible/hosts web -m ping
[root@centos01 ~]# ansible web -m ping
[root@centos01 ~]# vim /etc/ansible/hosts
............
[web]
192.168.100.20
192.168.100.30
[test]
www.benet.com:222
[mail]
yj1.kgc.cn
yj[2:5].kgc.cn
[root@centos01 ~]# ansible web -m command -a "systemctl status httpd" --limit "192.168.100.20"
192.168.100.20 | SUCCESS | rc=0 >>
[root@centos01 ~]# ansible 192.168.100.20 -m command -a "systemctl status httpd"
192.168.100.20 | SUCCESS | rc=0 >>
[root@centos01 ~]# ansible 192.168.1.* -m command -a "systemctl status httpd"
192.168.100.20 | SUCCESS | rc=0 >>
.......
192.168.100.30 | SUCCESS | rc=0 >>
.......
[root@centos01 ~]# ansible
ansible ansible-console-2 ansible-galaxy ansible-playbook-2.7 ansible-vault-2
ansible-2 ansible-console-2.7 ansible-galaxy-2 ansible-pull ansible-vault-2.7
ansible-2.7 ansible-doc ansible-galaxy-2.7 ansible-pull-2
ansible-connection ansible-doc-2 ansible-playbook ansible-pull-2.7
ansible-console ansible-doc-2.7 ansible-playbook-2 ansible-vault
Ansible [options]
[root@centos01 ~]# ansible all -f 5 -m ping
192.168.100.20 | SUCCESS => {
"changed": false,
"ping": "pong"
}
192.168.100.30 | SUCCESS => {
"changed": false,
"ping": "pong"
}
[root@centos01 ~]# ansible web --list
hosts (2):
192.168.100.20
192.168.100.30
[root@centos01 ~]# ansible web -m command -a "df -hT"
192.168.100.30 | SUCCESS | rc=0 >>
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/cl-root xfs 17G 4.4G 13G 26% /
devtmpfs devtmpfs 897M 0 897M 0% /dev
tmpfs tmpfs 912M 84K 912M 1% /dev/shm
tmpfs tmpfs 912M 0 912M 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 173M 842M 18% /boot
tmpfs tmpfs 183M 16K 183M 1% /run/user/42
tmpfs tmpfs 183M 0 183M 0% /run/user/0
192.168.100.20 | SUCCESS | rc=0 >>
文件系统 类型 容量 已用 可用 已用% 挂载点
/dev/mapper/cl-root xfs 17G 4.3G 13G 26% /
devtmpfs devtmpfs 897M 0 897M 0% /dev
tmpfs tmpfs 912M 84K 912M 1% /dev/shm
tmpfs tmpfs 912M 0 912M 0% /sys/fs/cgroup
/dev/sda1 xfs 1014M 173M 842M 18% /boot
tmpfs tmpfs 183M 16K 183M 1% /run/user/42
tmpfs tmpfs 183M 0 183M 0% /run/user/0
/dev/sr0 iso9660 4.1G 4.1G 0 100% /mnt
ansible-doc [options] [module……]
[root@centos01 ~]#ansible-doc -l
[root@centos01 ~]# ansible-doc ping
> PING (/usr/lib/python2.7/site-packages/ansible/modules/system/ping.py)
A trivial test module, this module always returns `pong' on successful contact. It
does not make sense in playbooks, but it is useful from `/usr/bin/ansible' to verify
the ability to login and that a usable python is configured. This is NOT ICMP ping,
this is just a trivial test module.
EXAMPLES:
# Test we can logon to 'webservers' and execute python with json lib.
ansible webservers -m ping
MAINTAINERS: Ansible Core Team, Michael DeHaan
METADATA:
Status: ['stableinterface']
Supported_by: core
Ansible-playbook playbook.yml
[root@centos01 ~]# ansible-console
Welcome to the ansible console.
Type help or ? to list commands.
root@all (2)[f:5]$ cd web
root@web (2)[f:5]$ list
192.168.100.20
192.168.100.30
[root@centos01 ~]# ansible web -m command -a "chdir=/ ls ./"
[root@centos01 ~]# ansible web -m shell -a "echo hello world "
192.168.100.20 | SUCCESS | rc=0 >>
hello world
192.168.100.30 | SUCCESS | rc=0 >>
hello world
[root@centos01 ~]# ansible web -m shell -a "echo hello world > /1.txt"
192.168.100.20 | SUCCESS | rc=0 >>
192.168.100.30 | SUCCESS | rc=0 >>
[root@centos01 ~]# ansible web -m copy -a "src=/etc/hosts
dest=/root/a1.hosts mode=777 owner=root group=root"
[root@centos01 ~]# ansible 192.168.100.20 -m hostname -a "name=test"
[root@centos01 ~]# ansible web -m shell -a "/usr/bin/rm -rf
/etc/yum.repos.d/CentOS-*"
[root@centos01 ~]# ansible web -m shell -a "/usr/bin/mount
/dev/cdrom /mnt"
[WARNING]: Consider using mount module rather than running mount
192.168.100.20 | SUCCESS | rc=0 >>
mount: /dev/sr0 写保护,将以只读方式挂载
192.168.100.30 | SUCCESS | rc=0 >>
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos01 ~]# ansible web -m yum -a "name=httpd
state=present"
[root@centos01 ~]# ansible web -m shell -a "rpm -qa | grep httpd"
[WARNING]: Consider using yum, dnf or zypper module rather than running rpm
192.168.100.20 | SUCCESS | rc=0 >>
httpd-2.4.6-67.el7.centos.x86_64
httpd-tools-2.4.6-67.el7.centos.x86_64
192.168.100.30 | SUCCESS | rc=0 >>
httpd-2.4.6-67.el7.centos.x86_64
httpd-tools-2.4.6-67.el7.centos.x86_64
[root@centos01 ~]# ansible web -m shell -a "systemctl start httpd"
[root@centos01 ~]# ansible web -m shell -a "netstat -anptu | grep httpd"
192.168.100.20 | SUCCESS | rc=0 >>
tcp6 0 0 :::80 :::* LISTEN 2072/httpd
192.168.100.30 | SUCCESS | rc=0 >>
tcp6 0 0 :::80 :::* LISTEN 3098/httpd
[root@centos01 ~]# ansible web -m service -a "name=httpd
enabled=yes state=restarted"
[root@centos01 ~]# ansible web -m user -a "name=user01
system=yes uid=502 group=root groups=root shell=/etc/nologin
home=/home/user01 password=pwd@123"
[root@centos01 ~]# grep -v ^# /etc/ansible/hosts | grep -v ^$
[web1]
192.168.100.20
[web2]
192.168.100.30
[root@centos01 ~]# vim /etc/ansible/a.yml
---
- hosts: web1
remote_user: root
tasks:
- name: adduser
user: name=user1 state=present
tags:
- aaa
- name: addgroup
group: name=root system=yes
tags:
- bbb
- hosts: web2
remote_user: root
tasks:
- name: copy file to web
copy: src=/etc/passwd dest=/home
tags:
- ccc
...
ansible-playbook [option] /PATH/TO/PLAYBOOK.yaml
[root@centos01 ~]# ansible-playbook --syntax-check /etc/ansible/a.yml
playbook: /etc/ansible/a.yml
[root@centos01 ~]# ansible-playbook -C /etc/ansible/a.yml
.................
192.168.100.20 : ok=3 changed=1 unreachable=0 failed=0
192.168.100.30 : ok=2 changed=1 unreachable=0 failed=0
[root@centos01 ~]# ansible-playbook --list-hosts /etc/ansible/a.yml
[root@centos01 ~]# ansible-playbook --list-tasks /etc/ansible/a.yml
[root@centos01 ~]# ansible-playbook --list-tags /etc/ansible/a.yml
[root@centos01 ~]# ansible-playbook /etc/ansible/a.yml
[root@centos01 ~]# ssh 192.168.100.20 tail -1 /etc/passwd
user1:x:1001:1001::/home/user1:/bin/bash
[root@centos01 ~]# ssh 192.168.100.30 ls -ld /home/passwd
-rw-r--r--. 1 root root 2342 7月 23 16:06 /home/passwd
[root@centos01 ~]# ssh 192.168.100.20 netstat -anpt | grep 80
tcp6 0 0 :::80 :::* LISTEN 94858/httpd
[root@centos01 ~]# vim /etc/ansible/httpd.yml
---
- hosts: web1
remote_user: root
tasks:
- name: change port
command: sed -i 's/Listen\ 80/Listen\ 8080/g' /etc/httpd/conf/httpd.conf
notify:
- restart httpd server
handlers:
- name: restart httpd server
service: name=httpd state=restarted
...
[root@centos01 ~]# ansible-playbook -C /etc/ansible/httpd.yml
[root@centos01 ~]# ansible-playbook /etc/ansible/httpd.yml
[root@centos01 ~]# ssh 192.168.100.20 netstat -anpt | grep 8080
tcp6 0 0 :::8080 :::* LISTEN 103594/httpd
- hosts: web
remote_user: root
roles:
- mysql
- httpd
[root@centos01 /]# mkdir -pv /etc/ansible/roles/mariadb/{files,tasks,handlers}
mkdir: 已创建目录 "/etc/ansible/roles/mariadb"
mkdir: 已创建目录 "/etc/ansible/roles/mariadb/files"
mkdir: 已创建目录 "/etc/ansible/roles/mariadb/tasks"
mkdir: 已创建目录 "/etc/ansible/roles/mariadb/handlers"
[root@ansible /]# cd /etc/ansible/roles/mariadb/tasks/
[root@centos01 tasks]# ls
[root@centos01 tasks]# vim main.yml
---
- name: install mariadb
yum: name=mariadb-server state=present
- name: move config file
shell: "[ -e /etc/my.cnf ] && mv /etc/my.cnf /etc/my.cnf.bak"
- name: provide a new config file
copy: src=my.cnf dest=/etc/my.cnf
- name: reload mariadb
shell: systemctl restart mariadb
- name: create database testdb
shell: mysql -u root -e "create database testdb;grant all on testdb.* to 'test'@'192.168.100.%' identified by 'test123';flush privileges;"
notify:
- restart mariadb
...
[root@centos01 tasks]# cd ../handlers/
[root@centos01 handlers]# vim main.yml
---
- name: restart mariadb
service: name=mariadb state=restarted
...
[root@centos01 handlers]# cd ../files
[root@centos01 files]# pwd
/etc/ansible/roles/mariadb/files
[root@centos01 files]# ls
my.cnf
[root@centos01 files]# cd /etc/ansible/
[root@centos01 ansible]# vim mariadb.yml
---
- hosts: web
remote_user: root
roles:
- mariadb
...
[root@centos01 ansible]# ansible-playbook -C mariadb.yml
........................
PLAY RECAP ***************************************************************************
192.168.100.20 : ok=3 changed=1 unreachable=0 failed=0
[root@centos01 ansible]# ansible-playbook mariadb.yml