1.管理Ansible配置文件
Ansible的配置文件
通过修改 Ansible 的配置文件来自定义 Ansible安装的行为。Ansible的配置文件可以存在于多个位置,根据不同的情况来选择使用不同的配置文件。
使用/etc/ansible/ansible.cfg
/etc/ansible/ansible.cfg是默认的配置文件,如果找不到其他的配置文件,执行ansible命令时,则默认使用此文件。
使用./ansible.cfg
执行ansible命令时,如果当前目录下存在ansible.cfg文件,则ansible会使用当前目录下的ansible.cfg文件而不使用ansible的默认配置文件。
使用~/.ansible.cfg
执行ansible命令时,如果当前目录下不存在ansible.cfg文件时,则会在用户的家目录下寻找.ansible.cfg文件,如果家目录下存在此配置文件,则会使用用户家目录下的.ansible.cfg配置文件。
使用ANSIBLE_CONFIG环境变量
通过环境变量来定义配置文件的位置,定义以后则不管在什么位置都默认使用环境变量定义的配置文件,不使用上面提到的任何配置文件。
[root@server ~]# export ANSIBLE_CONFIG=/tmp/ansible.cfg
[root@server ~]# ansible --version
ansible 2.9.23
config file = /tmp/ansible.cfg ##可以看到使用的配置文件路径已更改
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.6/site-packages/ansible
executable location = /usr/bin/ansible
python version = 3.6.8 (default, Dec 3 2020, 18:11:24) [GCC 8.4.1 20200928 (Red Hat 8.4.1-1)]
推荐的做法是在需要运行ansible命令的目录中创建ansible.cfg文件,在此目录中也创建需要使用的文件,如受管主机清单和playbook。这样能更加方便的管理不同项目的主机。
配置文件优先级
ANSIBLE_CONFIG > ./ansible.cfg > ~/.ansible.cfg > /etc/ansible/ansible.cfg
ANSIBLE_CONFIG环境变量指定的任何文件将覆盖所有其他配置文件。如果没有设置该环境变量,则会寻找执行ansible命令的目录下是否存在ansible.cfg文件。如果执行ansible命令的目录下不存在配置文件,则检查用户家目录下是否存在 .ansible.cfg文件。只有在找不到其他的配置文件时,才会使用默认的 /etc/ansible/ansible.cfg配置文件
我们可以执行下面这个命令来查看当前使用的是哪个配置文件
[root@server ~]# ansible --version
ansible 2.9.23
config file = /etc/ansible/ansible.cfg ##此行就是显示当前使用的配置文件
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.6/site-packages/ansible
executable location = /usr/bin/ansible
python version = 3.6.8 (default, Dec 3 2020, 18:11:24) [GCC 8.4.1 20200928 (Red Hat 8.4.1-1)]
管理配置文件中的设置
Ansible配置文件由几个部分组成,每一部分含有以键值对形式定义的设置。部分的标题以中括号括起来。
基本操作的修改有两个部分:
1. [defaults] 部分设置Ansible操作的默认值
2. [privilege_escalation] 配置Ansible如何在受管主机上执行特权升级
[defaults] ##通用默认配置
#inventory = /etc/ansible/hosts ##指定清单文件的路径
#library = /usr/share/my_modules/ ##模块的路径
#module_utils = /usr/share/my_module_utils/ ##模块工具的路径
#remote_tmp = ~/.ansible/tmp ##指定远程执行的路径
#local_tmp = ~/.ansible/tmp ##管理节点的执行路径
#plugin_filters_cfg = /etc/ansible/plugin_filters.yml
#forks = 5 ##并行处理受管主机的个数,可以根据控制主机的性能和被管理节点的数量来确定
#poll_interval = 15 ##轮询间隔
#sudo_user = root ##sudo使用的默认用户,默认是root
#ask_sudo_pass = True ##是否需要用户输入sudo密码
#ask_pass = True ##是否需要用户输入连接密码
#transport = smart ##通信机制
#remote_port = 22 ##指定连接对端节点的管理端口,默认是22,除非设置了特殊的SSH端口
#remote_user = root ##指定受管主机执行的用户,默认为root
#module_lang = C ##默认模块和系统之间通信的计算机语言,默认为'C'语言
#module_set_locale = False
#host_key_checking = False ##是否检查远程主机密钥
#sudo_exe = sudo ##sudo远程执行命令
#sudo_flags = -H -S -n ##传递sudo之外的参数
#timeout = 10 ##SSH超时时间
#remote_user = root ##远程登录用户名
#log_path = /var/log/ansible.log ##日志文件存放路径
#module_name = command ##Ansible命令默认执行的模块
#executable = /bin/sh ##执行的shell环境,用户shell模块
#hash_behaviour = replace ##特定的优先级覆盖变量
#jinja2_extensions = jinja2.ext.do,jinja2.ext.i18 ##允许开启jinja2扩展模块
#private_key_file = /path/to/file ##私钥文件存储位置
#display_skipped_hosts = True ##显示跳过任何任务的状态
#system_warnings = True ##禁用系统运行Ansible潜在问题警告
#deprecation_warnings = True ##PlayBook输出禁用“不建议使用”警告
#command_warnings = False ##command模块Ansible默认发出警告
#nocolor = 1 ##输出带上颜色区别,0表示开启,1表示关闭
#pipelining = False ##开启pipe SSH通道优化
让受管主机使用tom用户进行操作
##先修改ansible的配置文件
[root@server ansible]# vim /etc/ansible/ansible.cfg
# default user to use for playbooks if user is not specified
# (/usr/bin/ansible will use current user as default)
#remote_user = root
remote_user = tom ##找到此行,复制一个下来修改用户为tom
##受管主机先不创建tom用户看看效果,发现没有找到用户则会报错
[root@server ansible]# ansible 192.168.10.201 -m ping
192.168.10.201 | UNREACHABLE! => {
"changed": false,
"msg": "Invalid/incorrect password: Permission denied, please try again.",
"unreachable": true
}
[root@server ansible]#
##然后创建tom用户,则发现可以通信
[root@client ~]# useradd tom ##受管主机创建tom用户
[root@server ansible]# ansible 192.168.10.201 -m ping
192.168.10.201 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
[root@server ansible]#
让tom用户使用sudo来执行命令
##先在受管主机上添加tom用户的sudo权限
[root@client ~]# visudo
## Allow root to run any commands anywhere
root ALL=(ALL) ALL
tom ALL=(ALL) ALL ##找到此行添加tom用户的sudo权限
##然后修改ansible的配置文件
[root@server ansible]# vim /etc/ansible/ansible.cfg
[privilege_escalation]
become=True ##找到这几行,将注释删除
become_method=sudo
become_user=root
become_ask_pass=False
##然后在清单文件内,在受管主机后加上受管主机用户tom的sudo密码
[root@server ansible]# vim /etc/ansible/inventory
192.168.10.201 ansible_sudo_pass=1
##然后再执行命令,发现已经有了权限可以创建
[root@server ~]# ansible 192.168.10.201 -a "touch /root/123"
[WARNING]: Consider using the file module with state=touch rather than running 'touch'.
If you need to use command because file is insufficient you can add 'warn: false' to this
command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
192.168.10.201 | CHANGED | rc=0 >>
[root@server ~]#
2.ansible模块的使用
模块帮助文档的查看
ansible拥有上千个插件和模块,可以实现各种软件、平台、版本的管理,并且支持虚拟容器多层级的部署。
可以通过以下命令查看ansible模块和插件的帮助文档
[root@server ~]# ansible-doc -l
a10_server Manage A10 Networks AX/...
a10_server_axapi3 Manage A10 Networks AX/...
a10_service_group Manage A10 Networks AX/...
a10_virtual_server Manage A10 Networks AX/...
aci_aaa_user Manage AAA users (aaa:U...
aci_aaa_user_certificate Manage AAA user certifi...
############################略略略
[root@server ~]# ansible-doc -l |grep user ##可以使用过滤
aci_aaa_user Manage AAA users (aaa:U...
aci_aaa_user_certificate Manage AAA user certifi...
avi_cloudconnectoruser Module for setup of Clo...
avi_user Avi User Module
avi_useraccount Avi UserAccount Module
avi_useraccountprofile Module for setup of Use...
bigip_apm_acl Manage user-defined APM...
#############################略略略
也可以直接上官网进行查看帮助文档
user模块的使用
user模块用于管理受控机的用户帐号。
##在受控机上添加一个系统用户,用户名为apache,uid为306,设置其shell为/sbin/nologin,无家目录
[root@server ~]# ansible 192.168.10.201 -m user -a "name=apache uid=306 system=yes shell=/sbin/nologin create_home=no"
192.168.10.201 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"comment": "",
"create_home": false,
"group": 1002,
"home": "/home/apache",
"name": "apache",
"shell": "/sbin/nologin",
"state": "present",
"system": true,
"uid": 306
}
[root@server ~]#
##然后在受管主机查看是否创建成功
[root@client ~]# grep apache /etc/passwd
apache:x:306:1002::/home/apache:/sbin/nologin
[root@client ~]# ls /home/
abc
[root@client ~]#
##修改apache用户的uid为399
[root@server ~]# ansible 192.168.10.201 -m user -a "name=apache uid=399"
192.168.10.201 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"append": false,
"changed": true,
"comment": "",
"group": 1002,
"home": "/home/apache",
"move_home": false,
"name": "apache",
"shell": "/sbin/nologin",
"state": "present",
"uid": 399
}
[root@server ~]#
##然后在受管主机查看是否修改成功
[root@client ~]# id apache
uid=399(apache) gid=1002(apache) groups=1002(apache)
[root@client ~]#
##删除受管主机上的apache用户
[root@server ~]# ansible 192.168.10.201 -m user -a "name=apache state=absent"
192.168.10.201 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"force": false,
"name": "apache",
"remove": false,
"state": "absent"
}
[root@server ~]#
##然后在受管主机查看是否删除成功
[root@client ~]# id apache
id: ‘apache’: no such user
[root@client ~]#
这就是user模块的使用
本文介绍了Ansible配置文件的管理,包括配置文件的位置、优先级以及如何通过环境变量设定。此外,还详细讲解了如何通过配置文件设置默认用户和使用sudo权限。同时,文章还展示了Ansible的`user`模块的使用,包括添加、修改和删除用户,并提供了相关模块的文档查看方法。
2053

被折叠的 条评论
为什么被折叠?



