Ansible Playbooks:任务脚本,编排定义Ansible任务及的配置文件,由Ansible按序依次执行,通常是JSON格式的YML文件;
Inventory:Ansible 管理主机清单;
Modules:Ansible 执行命令功能模块,多数为内置的核心模块,也可自定义;
Plugins:模块功能的补充,如连接类型插件、循环插件、变量插件、过滤插件等,该功能不太常用;
API:供第三方程序调用的应用程序编程接口;
Ansible:该部分图中表现得不太明显,组合 Inventory、API、Modules、Plugins可以理解为是 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 root@192.168.100.20
[root@centos01 ~]# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.100.30
至此,已经完成 Ansible 的部署,接下来就可以通过 Ansible 对设备进行管理了。
[root@centos01 ~]# ansible -i /etc/ansible/hosts web -m ping如果使用默认的 Inventory文件(/etc/ansible/hosts),也可以不指定 Inventory 文件,例如:
[root@centos01 ~]# ansible web -m pingAnsible 通过设备列表以分组的方式添加到 /etc/ansible/hosts 文件来实现对设备的管理,所以在正式管理之前,首先要编写好 hosts 文件。hosts 文件中,以[ ]包含的部分代表组名,设备列表支持主机名和IP地址。默认情况下,通过访问22端口(SSH)来管理设备。若目标主机使用了非默认的SSH端口,还可以在主机名称之后使用冒号加端口标明,以行为单位分隔配置。另外,hosts文件还支持通配符。
[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可以将一个主机同时归置在不同的组中。配置完成之后,可以针对hosts定义的组进行远程操作,也可以针对组中的某一个或多个主机操作。例如:1)只对web组中192.168.1.2主机操作,通过—limit参数限定主机的变更。
[root@centos01 ~]# ansible web -m command -a "systemctl status httpd" --limit "192.168.100.20" 192.168.100.20 | SUCCESS | rc=0 >>2)只对192.168.100.20主机操作。通过IP限定主机的变更。
[root@centos01 ~]# ansible 192.168.100.20 -m command -a "systemctl status httpd" 192.168.100.20 | SUCCESS | rc=0 >>3)只对192.168.100.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
非固化需求;临时一次性操作;二次开发接口调用;非固化需求是指临时性的维护,如查看web服务器组磁盘使用情况、复制一个文件到其他机器等。类似这些没有规律的、临时需要做的任务,我们成为非固化需求,临时一次性操作,语法如下:
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" }②列出web组所有的主机列表,执行命令如下:
[root@centos01 ~]# ansible web --list hosts (2): 192.168.100.20 192.168.100.30③批量显示web组中的磁盘使用空间,执行命令如下:
[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% /mntweb关键字需要提前在/etc/ansible/hosts文件中定义组。Ansible的返回结果非常友好,一般会用三种颜色来表示执行结果:
ansible-doc [options] [module……]列出支持的模块:
[root@centos01 ~]#ansible-doc -l查询ping模块的说明信息:
[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"
name:指明主机名;示例如下:
[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管理端只是发送yum指令到被管理端,被管理端要存在可用的yum仓库才可以成功安装。
[root@centos01 ~]# ansible web -m service -a "name=httpd enabled=yes state=restarted"
name:必选参数,账号名称;state=present|absent:创建账号或者删除账号,present表示创建,absent表示删除;system=yes|no:是否为系统账户;uid:用户UID;group:用户的基本组groups:用户的附加组;shell:默认使用的shell;创建用户示例如下:
home:用户的家目录;
mve_home=yes|no:
如果设置的家目录已经存在,是否将已存在的家目录进行移动;
pssword:用户的密码,建议使用加密后的字符串;
comment:
用户的注释信息;
remore=yes|no:
当state=absent时,是否要删除用户的家目录;
[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 ...所有的“-”和“:”后面均有空格,而且注意缩进和对齐,如下图所示:
hosts:任务的目标主机,多个主机用冒号分隔,一般调用/etc/ansible/hosts中的分组信息;remote_user:远程主机上,运行此任务的默认身份为root;tasks:任务,即定义的具体任务,由模块定义的操作列表;handlers:触发器,类似tasks,只是在特定的条件下才会触发的任务。某任务的状态在运行后为changed时,可通过“notify”通知给相应的handlers进行触发执行;playbook文件定义的任务需要通过ansible-playbook命令进行调用并执行。ansible-playbook命令用法如下:
roles:角色,将hosts剥离出去,由tasks、handlers等所组成的一种特定的结构集合;
ansible-playbook [option] /PATH/TO/PLAYBOOK.yaml其中,[option]部分的功能包括:
[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通常情况下先执行 ansible-playbook -C /PATH/TO/PLAYBOOK.yaml 命令进行测试,测试没问题后再执行 ansible-playbook /PATH/TO/PLAYBOOK.yml 命令。
handlers是Ansible提供的条件机制之一。
handlers和task很类似,但是它只在被task通知的时候才会触发执行。
handlers只会在所有任务执行完成后执行。
而且即使被通知了很多次,它也只会执行一次。
handlers按照定义的顺序依次执行。
[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可以只调用一个角色,也可以调用多个角色,当定义了角色后,用ansible-playbook PALYBOOK文件执行即可。此时ansible会到角色集合的目录(/etc/ansible/roles)去找mysql和httpd目录,然后依次运行mysql和httpd目录下的所有代码。下面来个安装及配置mariadb数据库的实例需求分析:
要求被管理主机上自动安装mariadb,安装完成后上传提前准备好的配置文件至远端主机,重启服务,然后新建testdb数据库,并允许test用户对其拥有所有权限。
被管理主机配置yum仓库,自行配置,若被管理端可以连接互联网,那么直接将yum仓库指向互联网即可。
来源:
https://blog.51cto.com/14156658/2461907
- END -
微信号:sannet-edu
官方网站:www.sannet.net
咨询热线:0512-82289966