栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Chapter2.2 管理Ansible配置文件

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Chapter2.2 管理Ansible配置文件

2.管理Ansible配置文件 2.1 目标

学完这一章节,学员可以描述Ansible配置文件的位置,Ansible如何选择配置文件,如何修改配置文件。

2.2 配置Ansible

Ansible的配置行为能被自定义通过修改Ansible的配置文件,Ansible选择它的配置文件从如下几个可能的位置。

  • /etc/ansible/ansible.cfg Ansible软件安装时提供的一个基础配置文件,在没有其他配置文件被找到的情况下,使用该配置文件,优先级最低。
  • ~/.ansible.cfg Ansible查找一个.ansible.cfg文件在用户的家目录中,它被优先使用去代替/etc/ansible/ansible.cfg,优先级次低。
  • ./ansible.cfg 如果执行ansible命令的当前目录下存在ansible.cfg,它被用来代替Ansible的全局配置文件/etc/ansible/ansible.cfg和用户私有文件~/.ansible.cfg,优先级最高,推荐使用。

创建一个ansible.cfg在执行ansible命令的目录下,这个目录应该包含Ansible project用到的所有文件,包括inventory和playbook文件,这是最常用的一种放置Ansible配置文件的方式。

2.3 ANSIBLE_CONFIG环境变量

在不同的目录执行Ansible命令会用到不同的配置文件,但这种方法随着配置文件的增长很难去管理,一个更灵活的方式定义配置文件,通过使用ANSIBLE_CONFIG环境变量。当ANSIBLE_CONFIG环境变量被指定,优先使用环境变量中指定的配置文件,优先级最高。

export ANSIBLE_CONFIG = */path*
如果需要永久增加,将以上行增加到/etc/profile中。

由于众多的配置文件能被使用,那么当前Ansible究竟使用的是哪个配置文件,比较迷惑。

  • 可以执行ansible --version命令去查看哪个ansible版本被使用,哪个配置文件被使用。
[root@ansible ~]# cd /project
[root@ansible project]# ansible --version
ansible 2.9.7
  config file = /project/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /bin/ansible
  python version = 2.7.5 (default, Oct 14 2020, 14:45:30) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
[root@ansible project]#
  • 另一种方法查看ansible的配置文件是执行ansible命令时使用-v选项
[root@ansible inspect]# ansible all --list-hosts -v
Using /inspect/ansible.cfg as config file
...output omitted...

Ansible只使用最高优先级的配置文件,即使其他低优先级的配置文件存在,也会被忽略。

2.4 管理配置文件中的设置

Ansible的配置文件包含几个区域,每个区域包含的设置定义为键值对,每个区域的标题放在中括号中,最基本的操作使用两个区域

  • 【defaults】设置默认的Ansible选项
  • 【privilege_escalation】配置权限提升在受管主机上
[defaults]
inventory = ./inventory
remote_user = user
ask_pass = false


[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False

指令解释参见下表

指令描述
inventory指定清单文件的路径
remote_user在受管主机上的登录用户,如果未指定,使用当前用户
ask_pass是否提示输入SSH密码,如配置了SSH密钥登录,则设false
become连接后,是否自动切换受管主机的用户,尤其是切到root
become_method如何去切换用户,默认的是sud,su也是一个选项
become_user受管主机上切换到的目标用户
become_ask_pass切换用户时,是否提示输入密码
2.5 配置基于密钥的SSH登录

Step1.控制节点的用户生产一个SSH密钥对在~/.ssh目录下,你可以使用ssh-keygen命令去完成。

  • 对于一个单独的受管主机,你可以安装你的公钥到受管主机使用ssh-copy-id命令。这个命令将服务端的公钥更新到你的~/.ssh/known_hosts文件中。
[sysadmin@ansible security]$ ssh-copy-id sysadmin@192.168.160.14
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/sysadmin/.ssh/id_rsa.pub"
The authenticity of host '192.168.160.14 (192.168.160.14)' can't be established.
RSA key fingerprint is SHA256:VSpOpVJAcVOYEbFpQYxwht/2KXt1Ot3NFuI9EI8NEf8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
sysadmin@192.168.160.14's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'sysadmin@192.168.160.14'"
and check to make sure that only the key(s) you wanted were added.
  • 也可以使用Ansible Playbook去部署你的公钥到目标主机的目标用户账号通过使用authorized_key模块
- name: Public key is deployed to managed hosts for Ansible
  hosts: all
  
  tasks:
    - name: Ensure key is in root‘s ~/.ssh/authorized_hosts
      authorized_key:
        user: root
        state: present
        key: '{{ item }}'
      witch_file:
        - ~/.ssh/id_rsa.pub

由于受管主机本身没有基于SSH的密钥认证,你可以执行ansible-playbook with --ask-pass 选项为了初次验证远程用户。

2.6 权限提升

为了安全和审计的原因,Ansible需要连接目标主机使用低权限用户在提升权限获得管理员权限之前。这能被配置到配置文件中的【privilege_escalation】区域

在RedHat7版本中,默认的配置文件/etc/sudoers授权所有属于wheel组的用户在输入密码后sudo到root用户的权限。
一种使一个用户在不输入密码的情况sudo到root权限,去创建一个文件在/etc/sudoers.d目录中。

## password-less sudo for Ansible user
someuser ALL=(ALL) NOPASSWD:ALL

仔细考虑你选择的任何权限提升的方法的安全性,不同的组织和部门有不同的取舍和考虑。

下面是一个ansible.cfg文件的示例,假设你连接受管主机使用someuser基于SSH密钥验证,并且一些用户能sudo到root用户执行命令不需要输入密码。

[defaults]
inventory = ./inventory
remote_user = someuser
ask_pass = false

[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = false
2.6 Non-SSH Connections

Ansible连接到目标主机默认使用smart机制,这也是基于SSH最高效的一种方式。

对于默认使用SSH规则有一个例外情况,如果你没有设置localhost在你的清单文件中,Ansible设置了一个隐含的localhost条目允许你执行 ad hoc指令和playbooks在目标的localhost主机,这个特殊的条目没有包含在all或者ungrouped主机组。另外,代替使用smart SSH连接类型,Ansible连接自己默认使用local连接类型

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/844214.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号