ansible配置文件详解与部署ansible

1:ansible配置文件

ansible下载下来默认配置文件路径为/etc/ansible
 /etc/ansible路径下有三个文件或目录生成
 1:ansible.cfg
 ansible的默认配置文件2:Hosts
 ansible的主机清单配置文件3:Roles
 ansible的角色定义目录

2:ansible配置优先级

ansible执行命令查找配置文件时会按照以下顺序查找

1:ANSIBLE_CONFIG ansible会优先检查环境变量,及这个环境变量所指定的配置文件的路径
 2:ansible.cfg 次之会检查当前目录有没有ansible.cfg这个配置文件
 3:~/.ansible.cfg 如果上面都没有找到就会查找当前用户家目录下有没有.ansible.cfg这个配置文件
 4:/etc/ansible/ansible.cfg 上面都没有的话,ansible会去使用安装ansible时自动生成的/etc/ansible/ansible.cfg这个配置文件

3:主配置文件ansible.cfg详解

[defaults] 默认配置 
#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  //关闭第一次使用ansible连接客户端是输入命令提示

[privilege_escalation]  定义对受管主机执行特权升级,默认普通用户是没有权限来执行很多ansible任务的,但是我们可以给普通用户提权,让它有权限去执行ansible任务
become = true //是否开启become模式
become_method = sudo //	定义become方式
become_user = root  //定义become方式
become_ask_pass = false //	是否定义become提示密码

[paramiko_connection]、[ssh_connection]、[accelerate]用于优化与受管主机的连接

[selinux] 定义如何配置selinux交互

4:主机清单

清单定义了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

安装和配置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 ~]# yum  -y install ansible
[root@ansible ~]# su  - student
[student@ansible ~]$ mkdir ansible
[student@ansible ~]$ cp /etc/ansible/ansible.cfg  /home/student/ansible/
[student@ansible ~]$ vim /home/student/ansible/ansible.cfg
inventory      = /home/student/ansible/inventory
remote_user = student
roles_path    = /home/student/ansible/roles
become=True
become_method=sudo
become_user=root
become_ask_pass=False
[student@ansible ansible]$ mkdir roles
[student@ansible ~]$ mkdir /home/student/ansible/inventory
[dev]
node1

[test]
node2

[prod]
node3

[webservers:children]
prod

[student@ansible ansible]$ su -
Password:
[root@ansible ~]# vim /etc/sudoers.d/student
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%   34    38.2KB/s   00:00
student                                            100%   34    45.1KB/s   00:00
student                                            100%   34    38.6KB/s   00:00
[root@ansible ~]# su - student
[student@ansible ~]$ cd ansible/
[student@ansible ansible]$ ansible all -m ping
node1 | 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"
}
node2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}