0%

RHCE (EX294) - 安装和配置 Ansible

安装和配置 Ansible

按照下方所述,在控制节点 control 上安装和配置 Ansible:

安装所需的软件包

创建名为 /home/greg/ansible/inventory 的静态清单文件,以满足以下要求:

node1dev 主机组的成员

node2test 主机组的成员

node3node4prod 主机组的成员

node5balancers 主机组的成员

prod 组是 webservers 主机组的成员

创建名为 /home/greg/ansible/ansible.cfg 的配置文件,以满足以下要求:

主机清单文件为 /home/greg/ansible/inventory

playbook 中使用的角色的位置包括 /home/greg/ansible/roles

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
[kiosk@foundation0 ~]$ ssh root@172.25.250.254
[root@control ~]# su - greg
[greg@control ~]$ yum search ansible
...
========================================================================================== Name Exactly Matched: ansible ==========================================================================================
ansible.noarch : SSH-based configuration management, deployment, and task execution system
...
[greg@control ~]$ sudo yum install -y ansible.noarch
...
Installed:
ansible-2.9.15-1.el8ae.noarch python3-jmespath-0.9.0-11.el8.noarch sshpass-1.06-3.el8ae.x86_64

Complete!
[greg@control ~]$ mkdir /home/greg/ansible/
[greg@control ~]$ vim /home/greg/ansible/inventory
[dev]
node1

[test]
node2

[prod]
node3
node4

[balancers]
node5

[webservers:children]
prod
[greg@control ~]$ rpm -qc ansible
/etc/ansible/ansible.cfg
/etc/ansible/hosts
[greg@control ~]$ cp /etc/ansible/ansible.cfg /home/greg/ansible/ansible.cfg
[greg@control ~]$ vim /home/greg/ansible/ansible.cfg
...
inventory = /home/greg/ansible/inventory
...
# additional paths to search for roles in, colon separated
roles_path = /etc/ansible/roles:/home/greg/ansible/roles

# uncomment this to disable SSH key host checking
host_key_checking = False
...
[privilege_escalation]
become=True
#become_method=sudo
#become_user=root
#become_ask_pass=False
...
[greg@control ansible]$ ansible --version
ansible 2.9.15
config file = /home/greg/ansible/ansible.cfg
...
[greg@control ansible]$ ansible-inventory --graph
@all:
|--@balancers:
| |--node5
|--@dev:
| |--node1
|--@test:
| |--node2
|--@ungrouped:
|--@webservers:
| |--@prod:
| | |--node3
| | |--node4
[greg@control ansible]$ ansible all -m ping
node4 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
...