跳转至

Ansible


更新于 2023-11-15

官网
官方文档
github仓库

简介

Ansible是自动化运维工具,支持系统配置,软件部署,任务编排
默认使用SSH协议传输指令,被控制设备无需安装额外软件

组成

  • 控制节点
  • 被控节点
  • 被控节点描述文件

安装

Bash
1
2
3
4
5
#ubuntu
sudo apt install ansible

# redhat/centos
sudo yum install ansible

远端主机配置文件

文件格式

  • json
  • ini
  • yaml

默认配置:

/etc/ansible/hosts,格式为ini

其他格式

可以用ansible xxx -i cfg-file指定配置文件

ini格式配置文件

INI
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
[local]
localhost ansible_connectton=local
# 为主机节点添加参数
192.168.1.1 ansible_connection=ssh  ansible_ssh_user=mpdehaan

[local:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com

[group1]
# 指定远端主机连接端口
192.168.1.2:1234
192.168.1.3

[group2]
192.168.1.4
192.168.1.5
192.168.1.6

[group3]
# 匹配一系列IP
192.168.1.[7:100]

#为组设置变量
[group3:vars]
some_server=foo.southeast.example.com
halon_system_timeout=30
self_destruct_countdown=60
escape_pods=2

# 将组设置于其他组
[group:children]
group1
group2
[]内是组名
后续跟的是该组内远程主机

yaml格式配置文件

YAML
1
2
3
4
5
6
7
8
virtualmachines:
  hosts:
    vm01:
      ansible_host: 192.0.2.50
    vm02:
      ansible_host: 192.0.2.51
    vm03:
      ansible_host: 192.0.2.52
参数名 说明
ansible_ssh_host 将要连接的远程主机名
ansible_ssh_port ssh端口号.当不是22时需要设置
ansible_ssh_user 当ssh用户名不是当前用户名时需要设置
ansible_ssh_pass ssh 密码(这种方式并不安全,我们强烈建议使用 --ask-pass 或 SSH 密钥)
ansible_sudo_pass sudo 密码(这种方式并不安全,我们强烈建议使用 --ask-sudo-pass)
ansible_sudo_exe sudo 命令路径
ansible_connection 与主机的连接类型.比如:local, ssh等
ansible_ssh_private_key_file ssh 使用的私钥文件
ansible_shell_type 目标系统的shell类型.默认情况下,命令的执行使用 'sh' 语法,可设置为 'csh' 或 'fish'
ansible_python_interpreter 目标主机的 python 路径

被控节点描述文件

文件可以是iniyaml格式
文件名inventory.yaml或 注释以#;开头

如果有个主机没有在“known_hosts”中被初始化将会导致在交互使用Ansible或定时执行Ansible时对key信息的确

/etc/ansible/ansible.cfg or ~/.ansible.cfg来实现:

[defaults] host_key_checking = False

Bash
1
2
3
4
5
6
7
8
ansible all --list-hosts
ansible all -m ping
 # as bruce
$ ansible all -m ping -u bruce
# as bruce, sudoing to root
$ ansible all -m ping -u bruce --sudo
# as bruce, sudoing to batman
$ ansible all -m ping -u bruce --sudo --sudo-user batman
–ask-pass –ask-sudo-pass /etc/ansible/hosts authorized_keys

--private-key

ansible命令

ansible <pattern_goes_here> -m <module_name> -a <arguments>

Bash
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
ansible webservers -m service -a "name=httpd state=restarted"
ansible webservers:dbservers  -m service -a "name=httpd state=restarted" 
# 传输文件
ansible atlanta -m copy -a "src=/etc/hosts dest=/tmp/hosts"

# 传输文件
ansible webservers -m file -a "dest=/srv/foo/a.txt mode=600"
ansible webservers -m file -a "dest=/srv/foo/b.txt mode=600 owner=mdehaan group=mdehaan"

# 创建目录
ansible webservers -m file -a "dest=/path/to/c mode=755 owner=mdehaan group=mdehaan state=directory"
# 删除目录
ansible webservers -m file -a "dest=/path/to/c state=absent"

# 启动关闭服务
ansible webservers -m service -a "name=httpd state=started"
ansible webservers -m service -a "name=httpd state=restarted"
ansible webservers -m service -a "name=httpd state=stopped"

# 执行命令
ansible webservers -m command -a "/sbin/reboot -t now"