ansible

关于

Ansible 是一个 IT 自动化工具。它可以配置系统、部署软件并协调更高级的 IT 任务,例如持续部署或零停机时间滚动更新

配置ansible管理环境:

  • 创建ansible工作目录,并进入,目录名自己定义,不是固定的。

  • 创建配置文件。默认的配置文件是/etc/ansible/ansible.cfg,但是一般不使用它,而是在工作目录下创建自己的配置文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    vim ansible.cfg
    [defaults]
    inventory = hosts # 管理的主机,配置在当前目录的hosts文件中,hosts名是自定义的。=号两边空格可有可无。

    forks = 100 #并发数

    host_key_checking = False #是否检查host_key

    log_path = /var/log/ansible.log #log路径
  • 创建主机清单文件。写在[]里的是组名,[]下面的是组内的主机名

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    [test]
    node1

    [proxy]
    node2

    [webservers]
    node[3:4] # node3和node4的简化写法,表示从3到4

    [database]
    node5

    # cluster是组名,自定义的;:children是固定写法,表示下面的组名是cluster的子组。
    [cluster:children]
    webservers
    database
  • ansible all –list-hosts # 查看主机。注意,一定在工作目录下执行命令。

  • ansible webservers –list-hosts # 查看webservers组中所有的主机

临时命令语法:#ansible 主机或组列表 -m 模块 -a “参数” # -a是可选的

分发脚本:

ansible all -m copy -a “src=/tmp/test.sh dest=/tmp/test.sh mode=0755”

执行脚本

ansible all -m shell -a “ sh /root/test.sh”

切换root执行命令:

ansible all -a “ command “ -S -R root

ping:

ansible all -m ping

grep UNREACHABLE -n /var/log/ansible.log

修改关掉dhcp

ansible all -m shell -a “ sed -i ‘s/dhcp/none/g’ /etc/sysconfig/network-scripts/ifcfg-e* “

ansible常用模块

1
2
ansible-doc -l | grep yum  # 查看与yum相关的模块
ansible-doc yum # 查看yum模块的使用说明,主要查看下方的EXAMPLE示例

command 执行命令的模块

shell 执行命令的模块

script 远程执行脚本的模块

file 改文件目录链接权限属性等的模块

copy 上传模块

fetch 下载模块

lineinfile 替换某一行的模块

replace 替换关键词模块

user 用户管理模块

group 组管理模块

yum_repository 配置yum模块

yum 用于rpm软件包管理,如安装、升级、卸载的模块

service 用于控制服务模块

lvg 创建、删除卷组,修改卷组大小的模块

lvol 创建、删除逻辑卷,修改逻辑卷大小的模块

filesystem 用于格式化,也就是创建文件系统的模块

mount 用于挂载文件系统的模块

firewalld 配置防火墙模块

template 可以上传具有特定格式的文件(包含变量可自动转换)的模块

Playbook剧本

1
2
3
4
5
# 文件位置和名字是固定的,用于设置vim的格式
[root@control ansible]# vim ~/.vimrc
set ai # 设置自动缩进
set ts=2 # 设置按tab键,缩进2个空格
set et # 将tab转换成相应个数的空格
  • 执行剧本:# ansible-playbook *.yml

  • 剧本例子

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    ---
    - name: install pkgs
    hosts: test
    tasks:
    - name: install web pkgs
    yum:
    name:
    - httpd
    - php
    - php-mysqlnd
    state: present
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    ---
    - name: block tasks
    hosts: test
    tasks:
    - name: define a group of tasks
    block: # 任务块可多个任务组合在一起
    - name: install httpd # 通过yum安装httpd
    yum:
    name: httpd
    state: present

    - name: start httpd # 通过service启动httpd服务
    service:
    name: httpd
    state: started
    enabled: yes

    when: ansible_distribution=="RedHat" # 条件为真才会执行上面的任务
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    ---
    - name: create users
    hosts: test
    tasks:
    - name: create multiple users
    user:
    name: "{{item.uname}}"
    password: "{{item.upass|password_hash('sha512')}}"
    loop:
    - {"uname": "zhangsan", "upass": "123"}
    - {"uname": "lisi", "upass": "456"}

任务失败继续执行: ignore_errors: yes

when条件: 只有满足某一条件时,才执行当前模块任务

handlers触发任务: 通过notify通知,如果任务执行成功并changed状态,则执行触发任务,否则不执行

rescue和always:

  • block和rescue、always联合使用:
    • block中的任务都成功,rescue中的任务不执行
    • block中的任务出现失败(failed),rescue中的任务执行
    • block中的任务不管怎么样,always中的任务总是执行

loop循环: loop: [aaa,bbb,ccc,ddd,eee]

ansible变量

  • facts事实变量:# ansible test -m setup # 通过setup查看所有facts变量

    • 常用facts事实变量
      • ansible_all_ipv4_addresses:所有的IPV4地址
      • ansible_bios_version:BIOS版本信息
      • ansible_memtotal_mb:总内存大小
      • ansible_hostname:主机名
    1
    2
    3
    4
    5
    6
    7
    8
    9
    # 显示远程主机的主机名和内存大小。在ansible中,变量使用{{}}表示
    # debug模块用于输出信息,常用的参数是msg,用于输出指定内容
    ---
    - name: display host info
    hosts: test
    tasks:
    - name: display hostname and memory
    debug: # debug是模块,它的选项msg可以输出指定信息
    msg: "hostname: {{ansible_hostname}}; mem: {{ansible_memtotal_mb}} MB"
  • 自定义变量

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    # 在playbook中定义变量
    # 在test组中的主机上创建用户jack,他的密码是123456
    ---
    - name: create user
    hosts: test
    vars: # 固定格式,用于声明变量
    username: "jack" # 此处引号可有可无
    mima: "123456" # 此处引号是需要的,表示数字字符
    tasks:
    - name: create some users
    user:
    name: "{{username}}" # {}出现在开头,必须有引号
    state: present
    password: "{{mima|password_hash('sha512')}}"
    # 也可将变量定义在文件中,vars_files用于声明变量文件后使用变量

role角色

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 使用常规playbook,修改/etc/motd的内容
# 1. 修改默认配置
[root@control ansible]# vim ansible.cfg
[defaults]
inventory = hosts

# 2. 创建motd模板文件
[root@control ansible]# vim motd.j2
Hostname: {{ansible_hostname}} # facts变量,主机名
Date: {{ansible_date_time.date}} # facts变量,日期
Contact to: {{admin}} # 自定义变量

# 3. 编写playbook
[root@control ansible]# vim motd.yml
---
- name: modifty /etc/motd
hosts: test
vars:
admin: root@tedu.cn # 自定义名为admin的变量
tasks:
- name: modify motd
template:
src: motd.j2
dest: /etc/motd

[root@control ansible]# ansible-playbook motd.yml
[root@node1 ~]# cat /etc/motd
Hostname: node1
Date: 2021-11-01
Contact to: root@tedu.cn


# 创建角色
# 1. 声明角色存放的位置
[root@control ansible]# vim ansible.cfg
[defaults]
inventory = hosts
roles_path = roles # 定义角色存在当前目录的roles子目录中

# 2. 创建角色目录
[root@control ansible]# mkdir roles

# 3. 创建名为motd的角色
[root@control ansible]# ansible-galaxy init roles/motd
[root@control ansible]# ls roles/
motd # 生成了motd角色目录
[root@control ansible]# yum install -y tree
[root@control ansible]# tree roles/motd/
roles/motd/
├── defaults # 定义变量的目录,优先级最低
│   └── main.yml
├── files # 保存上传的文件(如copy模块用到的文件)
├── handlers # handlers任务写到这个目录的main.yml中
│   └── main.yml
├── meta # 保存说明数据,如角色作者、版本等
│   └── main.yml
├── README.md # 保存角色如何使用之类的说明
├── tasks # 保存任务
│   └── main.yml
├── templates # 保存template模块上传的模板文件
├── tests # 保存测试用的playbook。可选
│   ├── inventory
│   └── test.yml
└── vars # 定义变量的位置,推荐使用的位置
└── main.yml

# 4. 将不同的内容分别写到对应目录的main.yml中
# 4.1 创建motd.j2模板文件
[root@control ansible]# vim roles/motd/templates/motd.j2
Hostname: {{ansible_hostname}}
Date: {{ansible_date_time.date}}
Contact to: {{admin}}

# 4.2 创建变量
[root@control ansible]# vim roles/motd/vars/main.yml # 追加一行
admin: zzg@tedu.cn

# 4.3 创建任务
[root@control ansible]# vim roles/motd/tasks/main.yml # 追加
- name: modify motd
template:
src: motd.j2 # 这里的文件,自动到templates目录下查找
dest: /etc/motd

# 5. 创建playbook,调用motd角色
[root@control ansible]# vim role_motd.yml
---
- name: modify motd with role
hosts: test
roles:
- motd

# 6. 执行playbook
[root@control ansible]# ansible-playbook role_motd.yml

返回结果颜色

绿色:执行成功

黄色:执行成功并且对目标主机做过修改

红色:执行失败

紫色:提示警告

附录

官方手册:

https://docs.ansible.com/ansible/2.9/installation_guide/intro_configuration.html

更新于

请我喝[茶]~( ̄▽ ̄)~*

Chen 微信支付

微信支付

Chen 支付宝

支付宝

Chen 贝宝

贝宝