Ansible部署
ansible部署
- 主机清单是ansible里面最重要的部分里面,记录了受管主机的IP地址,没有这一项也就无从控制众多主机了。清单定义Ansible所管理的一批主机。这些主机也可以分配到组中,以进行集中管理。
- ansible常用路径地址
/etc/ansible/ansible.cfg 主配置文件
/etc/ansible/hosts 主机清单
/usr/bin/ansible 主程序,临时命令执行工具
/usr/bin/ansible-doc 查看配置文档,模块功能查看工具
/usr/bin/ansible-galaxy 下载/上传代码或roles模块官网
/usr/bin/ansible-playbook 定制自动化任务,编排剧本工具
/usr/bin/ansible-pull 远程执行命令工具
/usr/bin/ansible-vault 文件加密工具
/usr/bin/ansible-console console界面与用户执行的工具
#构建清单
清单分为静态和动态。静态主机清单可以通过配置文件定义。动态主机清单可以根据需要使用外部信息提供程序通过脚本或其他程序来生成。
以静态清单为例
[root@localhost ~]# cat /etc/ansible/hosts #默认地址
# This is the default ansible 'hosts' file.
#
# It should live in /etc/ansible/hosts
#
# - Comments begin with the '#' character
# - Blank lines are ignored
# - Groups of hosts are delimited by [header] elements
也可以指定其他地方
inventory = /etc/ansible/inventory
[root@localhost ansible]# ls
ansible.cfg hosts inventory roles
进入清单
[root@localhost ansible]# vim inventory
1.1.1.1
[aaa]
www.[01:03]abc.com
[webserver]
192.168.216.131
[webserver] #分组
192.168.216.131 #受管主机
可以用[]指定类似的主机
www.[01:03]abc.com = www.01abc.com,www.02abc.com.....
[root@localhost ansible]# ansible webserver --list-hosts
hosts (1): #列出指定组中所有主机
192.168.216.131
[root@localhost ansible]# ansible 1.1.1.1 --list-hosts
hosts (1): #验证主机是否存在
1.1.1.1
[root@localhost ansible]# ansible all --list-hosts
hosts (5): #列出所有受管主机
1.1.1.1
www.01abc.com
www.02abc.com
www.03abc.com
192.168.216.131
[root@localhost ansible]# ansible ungrouped --list-hosts
hosts (1): #列出不属于任何组的主机
1.1.1.1
[root@localhost ~]# ansible all -i /etc/ansible/hosts --list-hosts
hosts (1): #-i 后加其他主机清单绝对路径
2.2.2.2 可列出其他主机清单里的主机 更为灵活
管理ansible配置文件
可以通过修改 Ansible 配置文件中的设置来自定义 Ansible安装的行为。Ansible从控制节点上多个可能的位置之一选择其配置文件。
配置文件优先级(从高至低)
ANSIBLE_CONFIG环境变量 > ./ansible.cfg(当前所在目录) > ~/.ansible.cfg(家目录中.ansible) > /etc/ansible/ansible.cfg
查看ansible所用配置文件
[root@localhost ~]# ansible --version
家目录中
[root@localhost ~]# ansible --version
ansible 2.9.23
config file = /root/ansible.cfg
当前目录
[root@localhost opt]# ansible --version 当前目录
ansible 2.9.23
config file = /opt/ansible.cfg
环境变量
[root@localhost opt]# export ANSIBLE_CONFIG=/etc/ansible/ansible.cfg
[root@localhost opt]# ansible --version
ansible 2.9.23
config file = /etc/ansible/ansible.cfg
///在当前目录中也用最低优先配置文件
[root@localhost opt]# export ANSIBLE_CONFIG= #指空后恢复正常
[root@localhost opt]# ansible --version
ansible 2.9.23
config file = /opt/ansible.cfg
如果执行ansible命令的目录中存在ansible.cfg文件,则使用它,而不使用全局文件或用户的个人文件。这样,管理员可以创建一种目录结构,将不同的环境或项目存储在单独的目录中,并且每个目录包含为独特的一组设置而定制的配置文件。
推荐的做法是在需要运行Ansible命令的目录中创建ansible.cfg文件。此目录中也将包含任何供Ansible项目使用的文件,如清单和playbook。这是用于Ansible配置文件的最常用位置。实践中不常使用~/.ansible.cfg或/etc/ansible/ansible.cfg文件
管理配置文件中的设置
Ansible配置文件由几个部分组成,每一部分含有以键值对形式定义的设置。部分的标题以中括号括起来。
[defaults] 部分设置Ansible操作的默认值
[privilege_escalation] 配置Ansible如何在受管主机上执行特权升级(需要使用受管主机root时)
[root@localhost ~]# vim /etc/ansible/ansible.cfg
# ansible配置文件地址
默认文件解析
[defaults] #默认值
#inventory = /etc/ansible/hosts #清单文件路径
#library = /usr/share/my_modules/ #库文件存放目录
#module_utils = /usr/share/my_module_utils/ #模块应用程序路径
#remote_tmp = ~/.ansible/tmp #临时py命令文件存放在远程主机目录
#local_tmp = ~/.ansible/tmp #本机的临时命令执行目录
#plugin_filters_cfg = /etc/ansible/plugin_filters.yml #插件过滤器
#forks = 5 #默认并发数
#poll_interval = 15 #查询间隔数
#sudo_user = root #默认sudo用户
#ask_sudo_pass = True #每次执行是否询问密码
#ask_pass = True #连接时提升输入ssh密码
#transport = smart #自动选择连接方式默认ssh
#remote_port = 22 #远程默认端口 生产为了安全会使用其他端口
#module_lang = C
#module_set_locale = False #模块设置区域默认为关闭
常用设置
inventory 指定清单文件的路径。
remote_user 要在受管主机上登录的用户名。如果未指定则使用当前用户名
ask_pass 是否提示输入SSH密码。如果使用SSH公钥身份验证则可以是false
become 连接后是否自动在受管主机上切换用户(通常切换为root)
这也可以通过play来指定。
become_method 如何切换用户(通常为sudo,这也是默认设置,但可选择su)
become_user 要在受管主机上切换到的用户(通常是root,这也是默认值)
become_ask_pass 是否需要为become_method提示输入密码。默认为false
取决于对象是否需要密码
配置连接
nsible需要知道如何与其受管主机通信。更改配置文件的一个最常见原因是为了控制Ansible使用什么方法和用户来管理受管主机。需要的一些信息包括:
- 列出受管主机和主机组的清单的位置
- 要使用哪一种连接协议来与受管主机通信(默认为SSH),以及是否需要非标准网络端口来连接服务器
- 要在受管主机上使用哪一远程用户;这可以是root用户或者某一非特权用户
- 如果远程用户为非特权用户,Ansible需要知道它是否应尝试将特权升级为root以及如何进行升级(例如,通过sudo)
- 是否提示输入SSH密码或sudo密码以进行登录或获取特权
修改配置文件
remote_user = tom
取消权限注释
[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
对象主机
[root@localhost ~]# visudo #对象主机设置不需要密码
root ALL=(ALL) ALL
tom ALL=(ALL) NOPASSWD: ALL
发送密钥
[root@localhost ~]# ssh-copy-id tom@192.168.216.131
也可以将密钥直接写在主机清单,但此举并不安全
[webserver]
192.168.216.131 ansible_user=root ansible_password=redhat
测试
[root@localhost ~]# ansible 192.168.216.131 -m command -a 'touch 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.216.131 | CHANGED | rc=0 >> #代表执行成功
[tom@localhost ~]$ ll
total 0
-rw-r--r-- 1 root root 0 Jul 16 16:07 123
#使用了sudo所有属于root 但是tom可以删除
非ssh连接
默认情况下,Ansible用于连接受管主机的协议设置为smart,它会确定使用SHH的最高效方式。可以通过多种方式将其设置为其他的值。
例如,默认使用SSH的规则有一个例外。如果目录中没有localhost,Ansible将设置一个隐式localhost条目以便允许运行以localhost为目标的临时命令和playbook。这一特殊清单条目不包括在all或ungrouped主机组中。此外,Ansible不使用smart SSH连接类型,而是利用默认的特殊local连接类型来进行连接。
ansiable 帮助文档
[root@localhost ~]# ansible-doc -l
fortios_router_community_list Configure community lists in Fortinet's FortiOS and FortiGate
azure_rm_devtestlab_info Get Azure DevTest Lab facts
ecs_taskdefinition register a task definition in ecs
avi_alertscriptconfig Module for setup of AlertScriptConfig Avi RESTful Object
tower_receive Receive assets from Ansible Tower
netapp_e_iscsi_target NetApp E-Series manage iSCSI target configuration
azure_rm_acs Manage an Azure Container Service(ACS) instance
fortios_log_syslogd2_filter Filters for remote system server in Fortinet's FortiOS and For...
junos_rpc Runs an arbitrary RPC over NetConf on an Juniper JUNOS device
查看command模块
[root@localhost ~]# ansible-doc -l | grep command
pn_ospf CLI command to add/remove ospf protocol to a vRouter
pn_snmp_vacm CLI command to create/modify/delete snmp-vacm
icx_command Run arbitrary commands on remote Ruckus ICX 7000 series switch...
onyx_command Run commands on remote devices running Mellanox ONYX
pn_vrouterbgp CLI command to add/remove/modify vrouter-bgp
pn_snmp_trap_sink CLI command to create/delete snmp-trap-sink
routeros_command Run commands on remote devices running MikroTik RouterOS
pn_dscp_map_pri_map CLI command to modify dscp-map-pri-map
na_ontap_command NetApp ONTAP Run any cli command, the username provided needs ...
shell Execute shell commands on targets
ce_command Run arbitrary command on HUAWEI CloudEngine devices
vyos_command Run one or more commands on VyOS devices
pn_fabric_local CLI command to modify fabric-local
[root@localhost ~]# ansible-doc command
> COMMAND (/usr/lib/python2.7/site-packages/ansible/modules/commands/command.py)
The `command' module takes the command name followed by a list of space-delimited arguments.
The given command will be executed on all selected nodes. The command(s) will not be processed
through the shell, so variables like `$HOME' and operations like `"<"', `">"', `"|"', `";"' and
`"&"' will not work. Use the [shell] module if you need these features. To create `command'
tasks that are easier to read than the ones using space-delimited arguments, pass parameters
using the `args' L(task keyword,../reference_appendices/playbooks_keywords.html#task) or use
`cmd' parameter. Either a free form command or `cmd' parameter is required, see the examples.
For Windows targets, use the [win_command] module instead.
* This module is maintained by The Ansible Core Team
* note: This module has a corresponding action plugin.
官方文档 https://docs.ansible.com/ansible-core/2.11/collections/index_module.html
command模块允许管理员在受管主机的命令行中运行任意命令。要运行的命令通过-a选项指定为该模块的参数。例如,以下命令将对webservers组的受管主机运行hostname命令:
[root@localhost ~]# ansible 192.168.216.131 -a 'hostname lhj'
192.168.216.131 | CHANGED | rc=0 >>
[root@localhost ~]# ansible 192.168.216.131 -a 'hostname'
192.168.216.131 | CHANGED | rc=0 >>
lhj
Ansible常用模块
文件模块
copy: 将本地文件复制到受管主机
file: 设置文件的权限和其他属性
lineinfile: 确保特定行是否在文件中
synchronize: 使用rsync同步内容
软件包模块
package:使用操作系统本机的自动检测软件包管理器管理软件包
yum: 使用yum管理软件包
apt: 使用APT管理软件包
dnf: 使用dnf管理软件包
gem: 管理Ruby gem
pip: 从PyPI管理Python软件包
系统模块
firewalld:使用firewalld管理防火墙
reboot:重启计算机
service:管理服务
user:添加、删除和管理用户帐户
Net Tools模块 get_url:通过HTTP、HTTPS或FTP下载文件
nmcli:管理网络
uri:与Web服务交互
user模块
-m:user 模块
-a:命令参数
name=xxx 用户名字
shell=/bin/bash|/sbin/nologin 登录的shell
system=yes|no 设置为系统用户,不能在现有用户上更改
comment=添加描述信息
state=absent|present 修改/不修改内容
remove=yes|no 类似userdel 于state=absent搭配使用,会删除用户的家目录
[root@localhost ~]# ansible 192.168.216.131 -m user -a 'name=lhj uid=1003'
192.168.216.131 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"append": false,
"changed": true,
"comment": "lhj",
"group": 1000,
"home": "/home/lhj",
"move_home": false,
"name": "lhj",
"shell": "/bin/bash",
"state": "present",
"uid": 1003
改变uid
[root@localhost ~]# ansible 192.168.216.131 -m user -a 'name=lhj uid=1004'
192.168.216.131 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
删除用户
[root@localhost ~]# ansible 192.168.216.131 -m user -a 'name=lhj remove=yes state=absent'
192.168.216.131 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"force": false,
"name": "lhj",
"remove": true,
"state": "absent"
}
[root@localhost ~]# ansible 192.168.216.131 -m command -a 'id lhj'
192.168.216.131 | FAILED | rc=1 >>
id: 'lhj': no such usernon-zero return code
copy模块
[root@localhost ~]# ansible 192.168.216.131 -m copy -a 'src=/etc/ansible/hosts dest=/opt'
192.168.216.131 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "f4efa214cdc7b62e90f4b625e45493e7a47f6b02",
"dest": "/opt/hosts",
"gid": 0,
"group": "root",
"md5sum": "6a36b6fcb5097162cadf27e9cf2a27b8",
"mode": "0644",
"owner": "root",
"size": 1006,
"src": "/root/.ansible/tmp/ansible-tmp-1626430802.7-113599-164617239563505/source",
"state": "file",
"uid": 0
}
[root@localhost ~]# ansible 192.168.216.131 -m command -a 'ls /opt'
192.168.216.131 | CHANGED | rc=0 >>
hosts
(1) 检测语法
ansible-playbook --syntax-check /path/to/playbook.yaml
(2) 测试运行
ansible-playbook -C /path/to/playbook.yaml