安装 ansible
[[email protected] ~]# yum install -y ansible
修改管理哪些主机的清单文件
- [[email protected] ansible]# vi /etc/ansible/hosts
- [webserver]
- 192.168.1.21
- 192.168.1.22
- 192.168.1.23
给 ansible 和被管理的主机之间做一个密钥登录
- [[email protected] ansible]# SSH-keygen
- [[email protected] ansible]# SSH-copy-id 192.168.1.21
管理主机分两种方式 (命令 / 剧本)
命令:
命令格式
ansible <hosts> [options]
例子 1: ping 测通是否通畅
[[email protected] ansible]# ansible webserver -m ping -u root
... 省略
例子 2: 调用目录下的 echo 命令
[[email protected] ansible]# ansible all -a "/bin/echo hello world"
... 省略
例子 3: copy 文件到另一个目录
[[email protected] ansible]# ansible webserver -m copy -a "src=/etc/passwd dest=/opt/passwd"
... 省略
例子 4: 安装软件
[[email protected] ansible]# ansible webserver -m yum -a "name=lrzsz"
... 省略
例子 5: 添加用户
[[email protected] ansible]# ansible webserver -m user -a "name=zhangsan password=123"
例子 6: 启动系统的某个服务
[[email protected] ansible]# ansible webserver -m service -a "name=sshd state=started"
... 省略
[[email protected] ansible]# ansible webserver -m service -a "name=httpd state=started"
... 省略
[[email protected] ansible]# ansible webserver -m service -a 'name=httpd state=restarted'
... 省略
例子 7: 重启某个服务
[[email protected] ansible]# ansible webserver -m service -a "name=httpd state=restarted"
... 省略
例子 8: 指定 3 台机器执行同一个命令 (属于并行执行)
[[email protected] ansible]# ansible webserver -a "echo hello" -f 3
... 省略
例子 9: 获取系统信息
[[email protected] ansible]# ansible webserver -m setup
... 省略
剧本:
Playbook 组成
hosts: 目标主机
remote_user: 执行操作的用户身份
vars: 执行中的一些变量
tasks: 定义顺序执行的 action, 每个 action 调用一个模块
handers: event 处理操作, 仅有在 action 触发时才会执行, 多次触发只执行一次并按照声明的顺序执行.
例子 10: 安装 httpd 服务
- [[email protected] /]# vi test.YAML
- 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 信息
- [[email protected] /]# vi debug.YAML
- hosts: webserver
- remote_user: root
- tasks:
- name: debug
- debug:
- msg: "{{ansible_default_ipv4.gateway}}"
例子 12: shell 模块
- [[email protected] /]# vi shell.YAML
- hosts: webserver
- remote_user: root
- tasks:
- name: guanbifanghuoqiang
- shell: systemctl stop firewalld
例子 13: 拷贝模块
- [[email protected] /]# vi copy.YAML
- hosts: all
- remote_user: root
- tasks:
- name: copy
- copy: src=/etc/passwd dest=/home
例子 14: 创建用户 然后再 删除用户 执行双任务
- [[email protected] /]# vi user.YAML
- 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 然后再 卸载
- [[email protected] /]# vi yum.YAML
- hosts: all
- remote_user: root
- tasks:
- name: install httpd
- yum:
- name: httpd
- state: latest
- name: remove httpd
- yum:
- name: httpd
- state: absent
实例 16: command 模块
- [[email protected] /]# vi command.YAML
- hosts: root
- remote_user: root
- tasks:
- name: cmd
- command: ls
来源: http://www.bubuko.com/infodetail-3141912.html