1、配置文件详解
主配置文件/etc/ansible/ansible.cfg解析
[root@ansible ~]# vim /etc/ansible/ansible.cfg
[defaults] 默认配置
# some basic default values...
#inventory = /etc/ansible/hosts #主机列表配置文件
#library = /usr/share/my_modules/ #库文件存放目录,ansible默认搜寻模块的位置
#module_utils = /usr/share/my_module_utils/ #模块存放目录
#remote_tmp = ~/.ansible/tmp #临时py命令文件存放在远程主机目录
#local_tmp = ~/.ansible/tmp #本机的临时命令执行目录
#forks = 5 #默认并发数
#poll_interval = 15 #时间间隔
#sudo_user = root #默认sudo用户
#ask_sudo_pass = True #每次执行ansible命令是否询问sudo用户密码,默认值为no
#ask_pass = True #每次执行ansible命令是否询问ssh密码,默认值为no
#transport = smart #传输方式
#remote_port = 22 #远程端口号
#remote_user = root ----远程用户,受控主机使用什么用户进行执行ansible任务
#roles_path = /etc/ansible/roles
#host_key_checking = False
[privilege_escalation] 定义对受管主机执行特权升级,默认普通用户是没有权限来执行很多ansible任务的,但是我们可以给普通用户提权,让它有权限去执行ansible任务
become = true
become_method = sudo
become_user = root
become_ask_pass = false
[paramiko_connection]、[ssh_connection]、[accelerate]用于优化与受管主机的连接
[selinux] 定义如何配置selinux交互
2、主机清单详解
清单定义ansible将要管理的一批主机。这些主机也可以分配到组中,以进行集中管理。组可以包含子组,主机也可以是多个组的成员。清单还可以设置应用到它所定义的主机和组的变量。
可以通过两种方式定义主机清单。静态主机清单可以通过文本文件来定义。动态主机清单可以根据需要使用外部信息提供程序通过脚本或者其他程序来生成。
例如:
vim /etc/ansible/hosts
web1.example.com
web2.example.com
172.16.30.200
[webservers]
web1.example.com
web2.example.com
[db-servers]
db1.example.com
db2.example.com
[all:children]
webservers
db-servers
cd /etc/ansible
查看所有的受控主机
ansible all -i hosts –list-hosts
查看某组中包含哪些受控主机
ansible server1 -i hosts –list-hosts
3、安装和配置Ansible实操
按照下方所述,在控制节点ansible.example.com 上安装和配置Ansible:
安装所需的软件包
创建名为/home/student/ansible/inventory的静态清单文件, 以满足以下需求:
node1是dev主机组的成员
node2是test主机组的成员
node3是prod主机组的成员
prod组是webservers主机组的成员
[student@ansible ~]$ cd ansible/
[student@ansible ansible]$ ls
ansible.cfg inventory
[student@ansible ansible]$ rm -rf *
[student@ansible ansible]$ vim inventory
[dev]
node1
[test]
node2
[prod]
node3
[webservers:children]
prod
[student@ansible ansible]$ ls
inventory
创建名为/home/student/ansible/ansible.cfg的配置文件, 以满足以下要求:
主机清单文件为/home/student/ansible/inventory
playbook中使用的角色的位置包括/home/student/ansible/roles
//修改配置文件
[student@ansible ansible]$ vim ansible.cfg
inventory = /home/student/ansible/inventory
remote_user = student
roles_path = /home/student/ansible/roles
host_key_checking = False
[privilege_escalation] //去掉#注释
become=True
become_method=sudo
become_user=root
become_ask_pass=False
[student@ansible ansible]$ exit
logout
[root@ansible ~]# vim /etc/sudoers
找到root ALL=(ALL) ALL下方添加
student ALL=(ALL) NOPASSWD: ALL
[root@ansible ~]# for i in node{1..3}
> do scp /etc/sudoers.d/student root@$i:/etc/sudoers.d/
> done
root@node1's password:
student 100% 32 31.2KB/s 00:00
root@node2's password:
student 100% 32 30.4KB/s 00:00
root@node3's password:
student 100% 32 23.6KB/s 00:00
[student@ansible ansible]$ ansible all -m ping //如果这一步失败可尝试重启虚拟机
node1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
node2 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
node3 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}