本文是对ansible学习的入门,主要解决面对多台linux服务器,同一个任务的安装部署,只需在一台服务器中控制完成。环境是debain系统,并已安装ansible软件。
1、资源准备:
Ansible中文权威指南- 国内最专业的Ansible中文官方学习手册
2、修改管理哪些主机的清单文件
[root@ansible ansible]# vi /etc/ansible/hosts
[webserver]
192.168.1.21 ansible_ssh_port=1500 ansible_ssh_pass='XXXX'
192.168.1.22 #若默认22端口,则可不用指定端口;一开始为降低学习难度,先用明文的密码,后期熟悉
,再用密钥登录
3、给ansible和被管理的主机之间做一个密钥登录
[~/root/.ssh# ssh-keygen
id_rsa id_rsa.pub known_hosts
[root@ansible ansible]# ssh-copy-id -p 1500 192.168.1.21
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.1.21's password:
#输入密码后,将上传好了。
3、ansible管理主机两种方式:临时命令和剧本playbook,就像shell命令和.sh脚本一样。命令:
命令格式
ansible <hosts> [options]
-m:module,是指调用哪个模块;
-a:action,是指这个模块,调用哪个命令。
例子1: ping 测通是否通畅
[root@ansible ansible]# ansible webserver -m ping -u root
…省略
例子2: 调用目录下的echo命令
[root@ansible ansible]# ansible all -a “/bin/echo hello world”
…省略
例子3: copy文件到另一个目录
[root@ansible ansible]# ansible webserver -m copy -a “src=/etc/passwd dest=/opt/passwd”
…省略
例子4: 安装软件
[root@ansible ansible]# ansible webserver -m yum -a “name=lrzsz”
…省略
例子5: 添加用户
[root@ansible ansible]# ansible webserver -m user -a “name=zhangsan password=123”
例子6: 启动系统的某个服务
[root@ansible ansible]# ansible webserver -m service -a “name=sshd state=started”
…省略
[root@ansible ansible]# ansible webserver -m service -a “name=httpd state=started”
…省略
[root@ansible ansible]# ansible webserver -m service -a ‘name=httpd state=restarted’
…省略
例子7: 重启某个服务
[root@ansible ansible]# ansible webserver -m service -a “name=httpd state=restarted”
…省略
例子8: 指定3台机器执行同一个命令(属于并行执行)
[root@ansible ansible]# ansible webserver -a “echo hello” -f 3
…省略
例子9: 获取系统信息
[root@ansible ansible]# ansible webserver -m setup
4、第 二种就是 剧本:
Playbook组成,语法格式是yaml,若不熟悉这种格式,可参考 YAML - 简书
hosts: 目标主机
remote_user: 执行操作的用户身份
vars: 执行中的一些变量
tasks: 定义顺序执行的action,每个action调用一个模块
handers: event处理操作,仅有在action触发时才会执行,多次触发只执行一次并按照声明的顺序执行。
例子10: 安装httpd服务
[root@ansible /]# vi test.yml
hosts: webserver
remote_user: root
tasks:
name: install httpd
yum: pkg=httpd state=latest
hosts: webserver
remote_user: root
tasks:
name: start httpd
service: name=httpd state=started
例子11: 获取debug信息
[root@ansible /]# vi debug.yml
hosts: webserver
remote_user: root
tasks:
name: debug
debug:
msg: “{{ansible_default_ipv4.gateway}}”
例子12: shell模块
[root@ansible /]# vi shell.yml
hosts: webserver
remote_user: root
tasks:
name: guanbifanghuoqiang
shell: systemctl stop firewalld
例子13: 拷贝模块
[root@ansible /]# vi copy.yml
hosts: all
remote_user: root
tasks:
name: copy
copy: src=/etc/passwd dest=/home
例子14:创建用户 然后再 删除用户 执行双任务
[root@ansible /]# vi user.yml
hosts: all
remote_user: root
tasks:
name: create user
user:
name: apeng
uid: 5000
group: ftp
shell: /bin/bash
groups: apeng
append: yes
name: delete user
user:
name: apeng
state: absent
remove: yes
例子15: 安装httpd 然后再 卸载
[root@ansible /]# vi yum.yml
hosts: all
remote_user: root
tasks:
name: install httpd
yum:
name: httpd
state: latest
name: remove httpd
yum:
name: httpd
state: absent
实例16: command模块
[root@ansible /]# vi command.yml
hosts: root
remote_user: root
tasks:
name: cmd
command: ls
至此ansible就入门了。