Ansible是?

Ansible是一个配置管理和配置工具,类似于Chef,Puppet或Salt。这是一款很简单也很容易入门的部署工具,它使用SSH连接到服务器并运行配置好的任务

也就是说,Ansible只运行在控制端,受控端只需要接受主控的ssh连接就可以

安装

yum -y install ansible-core

配置SSH连接(主控)

# 生成ssh key
ssh-keygen
# 拷贝ssh key到远程主机,ssh的时候就不需要输入密码了
ssh-copy-id remoteuser@remoteserver
# ssh的时候不会提示是否保存key(可选)
ssh-keyscan remote_servers >> ~/.ssh/known_hosts

如果报错、输入密码正确提示错误

vim /etc/ssh/sshd_config
#修改这两行
PasswordAuthentication yes
PermitRootLogin yes (这个一定要改)

验证SSH配置

ssh username@hostname
=====================
[root@master ~]# ssh [email protected]
Last failed login: Tue Nov 19 10:39:06 CST 2024 from 192.168.10.3 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Tue Nov 19 10:33:46 2024

Ansible管理哪些主机?

/etc/ansible/hosts中配置被控主机

最简单的hosts文件:

192.168.10.1
192.168.10.2
user1.alexblock.org
user2.alexblock.org

带分类的hosts文件:

[heypixel]
1.1.1.1
server1 ansible_host=1.1.1.1
user3.alexblock.org

命令管理主机

Ansible提供了一个命令行工具,Ad-Hoc Commands

检查ansible安装环境

检查所有的远程主机

ansible all -m ping

# 如果你的ssh炸了
[root@localhost ~]# ansible all -m ping --ask-pass
SSH password: (输入密码)
server1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "ping": "pong"
}

执行命令

在所有的远程主机上,以当前bash的同名用户,在远程主机执行“echo bash”

ansible all -a "/bin/echo hello"

拷贝文件

拷贝文件/etc/host到远程主机(组)web,位置为/tmp/hosts

ansible web -m copy -a "src=/etc/hosts dest=/tmp/hosts"

安装包

远程主机(组)web安装yum包acme

ansible web -m yum -a "name=acme state=present"

添加用户

ansible all -m user -a "name=foo password=<crypted password here>"

下载git包

ansible web -m git -a "repo=git://foo.example.org/repo.git dest=/srv/myapp version=HEAD"

启动服务

ansible web -m service -a "name=httpd state=started"

脚本管理

Ansible提供脚本功能。Ansible脚本的名字叫Playbook,使用的是YAML的格式,文件以yml、yaml结尾。

使用方法

ansible-playbook test.yml

格式

---
- name: 描述Playbook的名称或目的
  hosts: 目标主机或主机组
  become: yes/no  # 是否以特权用户(如root)运行
  vars:
    - 变量名1: 值1
    - 变量名2: 值2
  tasks:
    - name: 任务1的描述
      模块名1:
        参数1: 值1
        参数2: 值2
    - name: 任务2的描述
      模块名2:
        参数1: 值1
        参数2: 值2

示例

---
- name: 安装和配置Apache服务器
  hosts: server1
  become: yes
  vars:
    http_port: 80
    max_clients: 200
  tasks:
    - name: 安装Apache
      yum:
        name: httpd
        state: present
  handlers:
    - name: 重启Apache
      service:
        name: httpd
        state: restarted

运行

[root@localhost ~]# ansible-playbook test.yaml --ask-pass
SSH password: 

PLAY [安装和配置Apache服务器] **************************************************

TASK [Gathering Facts] *********************************************************
ok: [server1]

TASK [安装Apache] **************************************************************
ok: [server1]

PLAY RECAP *********************************************************************
server1                    : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@localhost ~]# 

简单的实操题目

要求

  • 编写/root/skills.yaml剧本
  • 在服务器1创建文件/root/ansible.txt
  • 将文件复制到所有受控节点的/root目录

编写剧本

vim /root/skills.yaml

内容

---
- name: copyaction
  hosts: all
  become: yes
  tasks:
    - name: create_ansible
      file:
        path: /root/ansible.txt
        state: touch
      when: inventory_hostname == "server1"

    - name: paste
      copy:
        src: /root/ansible.txt
        dest: /root/ansible.txt
        remote_src: yes
  • hosts: all :表示这个Playbook将应用于所有主机。
  • become: yes :表示任务需要提升权限(如sudo)。
  • 第一个任务 :在server1上创建文件/root/ansible.txt。使用file模块,并设置state: touch来创建一个空文件。when条件确保这个任务只在server1上执行。
  • 第二个任务 :将文件/root/ansible.txtserver1复制到所有受控节点的/root目录。使用copy模块,并设置remote_src: yes来从远程源复制文件。