ansible配置文件详解,在运⾏Ansible


一.配置文件详解
ansible配置文件
/etc/ansible有以下三个文件或者目录生成

1、Hosts — 主机清单配置文件

2、ansible.cfg — Ansible配置文件

3、Roles — 角色定义目录

ansible配置文件优先级
Ansible配置以ini格式存储配置数据,在Ansible中⼏乎所有配置都可以通过Ansible的Playbook或环境变量来重新赋值。在运⾏Ansible命令时,命令将会按照以下顺序查找配置⽂件。

•ANSIBLE_CONFIG :⾸先,Ansible命令会检查环境变量,及这个环境变量指向的配置⽂件。 export •ANSIBLE_CONFIG=/PATH

•./ansible.cfg :其次,将会检查当前⽬录下的ansible.cfg配置⽂件 
~/.ansible.cfg :再次,将会检查当前⽤户home⽬录下的.ansible.cfg配置⽂件。 
/etc/ansible/ansible.cfg :最后,将会检查在⽤软件包管理⼯具安装Ansible时⾃动产⽣的配置⽂件。

主配置文件/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交互

二.主机清单详解
清单定义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
案例1:在/etc/ansible目录下创建主机清单inventory,主机清单分组如下

server1:node1
server2: node2
webserver: node1
          node2

使用ansible中的ping模块进行测试

1、vim /etc/ansible/ansible.cfg
[defaults]
inventory       = /etc/ansible/inventory
2、vim /etc/ansible/inventory
[server1]
node1

[server2]
node2

[webserver]
node1
node2
3、cd /etc/ansible
ansible all -m ping 

三.安装和配置Ansible实操

按照下方所述,在控制节点ansible.example.com 上安装和配置Ansible:
安装所需的软件包

 创建名为/home/student/ansible/inventory的静态清单文件, 以满足以下需求: 
node1是dev主机组的成员 
node2是test主机组的成员 
node3是prod主机组的成员 
prod组是webservers主机组的成员 
创建名为/home/student/ansible/ansible.cfg的配置文件, 以满足以下要求: 
主机清单文件为/home/student/ansible/inventory 
playbook中使用的角色的位置包括/home/student/ansible/roles

[root@ansible ~]# su - student
Last login: Thu Oct 20 13:12:17 CST 2022 on pts/0

[student@ansible ~]$ ls
ansible

[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

[student@ansible ansible]$ pwd
/home/student/ansible

[student@ansible ansible]$ ls
inventory  roles

[student@ansible ansible]$ cp /etc/ansible/ansible.cfg .
[student@ansible ansible]$ ls
ansible.cfg  inventory  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.d/student
student ALL=(ALL) NOPASSWD: ALL

或者[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
> student                                       100%   32    23.7KB/s   00:00
> student                                       100%   32    26.1KB/s   00:00
> student                                       100%   32    15.1KB/s   00:00


[student@ansible ansible]$ ansible all -m ping
node3 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}
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"
}

相关内容