首页 > 其他分享 >ansible配置基础

ansible配置基础

时间:2022-10-23 23:23:22浏览次数:56  
标签:主机 配置 基础 ansible inventory student become root

配置文件详解

[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将要管理的一批主机。这些主机也可以分配到组中,以进行集中管理。组可以包含子组,主机也可以是多个组的成员。清单还可以设置应用到它所定义的主机和组的变量。

可以通过两种方式定义主机清单。静态主机清单可以通过文本文件来定义。动态主机清单可以根据需要使用外部信息提供程序通过脚本或者其他程序来生成。

安装和配置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
//配置sudo权限
[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%   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"
}

标签:主机,配置,基础,ansible,inventory,student,become,root
From: https://www.cnblogs.com/Archer-x/p/16820014.html

相关文章