ansible配置文件及常用模块详解

学习目标:

  1. 掌握ansible.cfg配置文件内容

学习内容:

ansible.cfg配置

#inventory      = /etc/ansible/hosts	#主机配置列表
#library        = /usr/share/my_modules/	#库文件存放目录
#remote_tmp     = ~/.ansible/tmp	#临时py命令文件存放在远程主机的目录
#local_tmp      = ~/.ansible/tmp	#本机的临时命令执行目录
#forks          = 5		#默认并发数
#sudo_user      = root	#默认sudo用户
#ask_sudo_pass = True	#每次执行ansible命令是否询问密码
#ask_pass      = True	
#remote_port    = 22	#连接的端口
#record_host_keys=False	#检查对应服务器的host_key,建议取消注释
#log_path = /var/log/ansible.log	#日志文件

ansible系列命令

在这里插入图片描述

[root@zabbix_server log]# ansible  all --list
  hosts (2):
    172.28.102.130
    172.28.102.131

ansible的host-pattern
在这里插入图片描述
在这里插入图片描述
ansible执行过程
在这里插入图片描述

[root@zabbix_server lyp]# ansible LYP -m command -a 'hostname' -vvv		#-vvv可以查看最详细的执行过程
ansible 2.9.27
  config file = /etc/ansible/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 = /usr/bin/ansible
  python version = 2.7.5 (default, Jul 13 2018, 13:06:57) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)]
Using /etc/ansible/ansible.cfg as config file
host_list declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
script declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
auto declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
Parsed /etc/ansible/hosts inventory source with ini plugin
Skipping callback 'actionable', as we already have a stdout callback.
Skipping callback 'counter_enabled', as we already have a stdout callback.
Skipping callback 'debug', as we already have a stdout callback.
Skipping callback 'dense', as we already have a stdout callback.
Skipping callback 'dense', as we already have a stdout callback.
Skipping callback 'full_skip', as we already have a stdout callback.
Skipping callback 'json', as we already have a stdout callback.
Skipping callback 'minimal', as we already have a stdout callback.
Skipping callback 'null', as we already have a stdout callback.
Skipping callback 'oneline', as we already have a stdout callback.
Skipping callback 'selective', as we already have a stdout callback.
Skipping callback 'skippy', as we already have a stdout callback.
Skipping callback 'stderr', as we already have a stdout callback.
Skipping callback 'unixy', as we already have a stdout callback.
Skipping callback 'yaml', as we already have a stdout callback.
META: ran handlers
<172.28.102.130> ESTABLISH SSH CONNECTION FOR USER: None
<172.28.102.130> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/c3ced534af 172.28.102.130 '/bin/sh -c '"'"'echo ~ && sleep 0'"'"''
<172.28.102.131> ESTABLISH SSH CONNECTION FOR USER: None
<172.28.102.131> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/1d1922a709 172.28.102.131 '/bin/sh -c '"'"'echo ~ && sleep 0'"'"''
<172.28.102.130> (0, '/root\n', '')
<172.28.102.130> ESTABLISH SSH CONNECTION FOR USER: None
<172.28.102.130> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/c3ced534af 172.28.102.130 '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo /root/.ansible/tmp `"&& mkdir "` echo /root/.ansible/tmp/ansible-tmp-1651156778.54-4300-201563540311760 `" && echo ansible-tmp-1651156778.54-4300-201563540311760="` echo /root/.ansible/tmp/ansible-tmp-1651156778.54-4300-201563540311760 `" ) && sleep 0'"'"''
<172.28.102.131> (0, '/root\n', '')
<172.28.102.131> ESTABLISH SSH CONNECTION FOR USER: None
<172.28.102.131> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/1d1922a709 172.28.102.131 '/bin/sh -c '"'"'( umask 77 && mkdir -p "` echo /root/.ansible/tmp `"&& mkdir "` echo /root/.ansible/tmp/ansible-tmp-1651156778.54-4302-235961852880307 `" && echo ansible-tmp-1651156778.54-4302-235961852880307="` echo /root/.ansible/tmp/ansible-tmp-1651156778.54-4302-235961852880307 `" ) && sleep 0'"'"''
<172.28.102.130> (0, 'ansible-tmp-1651156778.54-4300-201563540311760=/root/.ansible/tmp/ansible-tmp-1651156778.54-4300-201563540311760\n', '')
<172.28.102.131> (0, 'ansible-tmp-1651156778.54-4302-235961852880307=/root/.ansible/tmp/ansible-tmp-1651156778.54-4302-235961852880307\n', '')
<172.28.102.130> Attempting python interpreter discovery
<172.28.102.130> ESTABLISH SSH CONNECTION FOR USER: None
<172.28.102.131> Attempting python interpreter discovery
<172.28.102.131> ESTABLISH SSH CONNECTION FOR USER: None
<172.28.102.130> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/c3ced534af 172.28.102.130 '/bin/sh -c '"'"'echo PLATFORM; uname; echo FOUND; command -v '"'"'"'"'"'"'"'"'/usr/bin/python'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python3.7'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python3.6'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python3.5'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python2.7'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python2.6'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'/usr/libexec/platform-python'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'/usr/bin/python3'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python'"'"'"'"'"'"'"'"'; echo ENDFOUND && sleep 0'"'"''
<172.28.102.131> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/1d1922a709 172.28.102.131 '/bin/sh -c '"'"'echo PLATFORM; uname; echo FOUND; command -v '"'"'"'"'"'"'"'"'/usr/bin/python'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python3.7'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python3.6'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python3.5'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python2.7'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python2.6'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'/usr/libexec/platform-python'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'/usr/bin/python3'"'"'"'"'"'"'"'"'; command -v '"'"'"'"'"'"'"'"'python'"'"'"'"'"'"'"'"'; echo ENDFOUND && sleep 0'"'"''
<172.28.102.130> (0, 'PLATFORM\nLinux\nFOUND\n/usr/bin/python\n/usr/bin/python2.7\n/usr/bin/python\nENDFOUND\n', '')
<172.28.102.130> ESTABLISH SSH CONNECTION FOR USER: None
<172.28.102.130> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/c3ced534af 172.28.102.130 '/bin/sh -c '"'"'/usr/bin/python && sleep 0'"'"''
<172.28.102.131> (0, 'PLATFORM\nLinux\nFOUND\n/usr/bin/python\n/usr/bin/python2.7\n/usr/bin/python\nENDFOUND\n', '')
<172.28.102.131> ESTABLISH SSH CONNECTION FOR USER: None
<172.28.102.131> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/1d1922a709 172.28.102.131 '/bin/sh -c '"'"'/usr/bin/python && sleep 0'"'"''
<172.28.102.130> (0, '{"osrelease_content": "NAME=\\"CentOS Linux\\"\\nVERSION=\\"7 (Core)\\"\\nID=\\"centos\\"\\nID_LIKE=\\"rhel fedora\\"\\nVERSION_ID=\\"7\\"\\nPRETTY_NAME=\\"CentOS Linux 7 (Core)\\"\\nANSI_COLOR=\\"0;31\\"\\nCPE_NAME=\\"cpe:/o:centos:centos:7\\"\\nHOME_URL=\\"https://www.centos.org/\\"\\nBUG_REPORT_URL=\\"https://bugs.centos.org/\\"\\n\\nCENTOS_MANTISBT_PROJECT=\\"CentOS-7\\"\\nCENTOS_MANTISBT_PROJECT_VERSION=\\"7\\"\\nREDHAT_SUPPORT_PRODUCT=\\"centos\\"\\nREDHAT_SUPPORT_PRODUCT_VERSION=\\"7\\"\\n\\n", "platform_dist_result": ["centos", "7.5.1804", "Core"]}\n', '')
Using module file /usr/lib/python2.7/site-packages/ansible/modules/commands/command.py
<172.28.102.130> PUT /root/.ansible/tmp/ansible-local-42831UskiE/tmpbPh0Kp TO /root/.ansible/tmp/ansible-tmp-1651156778.54-4300-201563540311760/AnsiballZ_command.py
<172.28.102.130> SSH: EXEC sftp -b - -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/c3ced534af '[172.28.102.130]'
<172.28.102.131> (0, '{"osrelease_content": "NAME=\\"CentOS Linux\\"\\nVERSION=\\"7 (Core)\\"\\nID=\\"centos\\"\\nID_LIKE=\\"rhel fedora\\"\\nVERSION_ID=\\"7\\"\\nPRETTY_NAME=\\"CentOS Linux 7 (Core)\\"\\nANSI_COLOR=\\"0;31\\"\\nCPE_NAME=\\"cpe:/o:centos:centos:7\\"\\nHOME_URL=\\"https://www.centos.org/\\"\\nBUG_REPORT_URL=\\"https://bugs.centos.org/\\"\\n\\nCENTOS_MANTISBT_PROJECT=\\"CentOS-7\\"\\nCENTOS_MANTISBT_PROJECT_VERSION=\\"7\\"\\nREDHAT_SUPPORT_PRODUCT=\\"centos\\"\\nREDHAT_SUPPORT_PRODUCT_VERSION=\\"7\\"\\n\\n", "platform_dist_result": ["centos", "7.5.1804", "Core"]}\n', '')
Using module file /usr/lib/python2.7/site-packages/ansible/modules/commands/command.py
<172.28.102.131> PUT /root/.ansible/tmp/ansible-local-42831UskiE/tmpE309AH TO /root/.ansible/tmp/ansible-tmp-1651156778.54-4302-235961852880307/AnsiballZ_command.py
<172.28.102.131> SSH: EXEC sftp -b - -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/1d1922a709 '[172.28.102.131]'
<172.28.102.130> (0, 'sftp> put /root/.ansible/tmp/ansible-local-42831UskiE/tmpbPh0Kp /root/.ansible/tmp/ansible-tmp-1651156778.54-4300-201563540311760/AnsiballZ_command.py\n', '')
<172.28.102.130> ESTABLISH SSH CONNECTION FOR USER: None
<172.28.102.130> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/c3ced534af 172.28.102.130 '/bin/sh -c '"'"'chmod u+x /root/.ansible/tmp/ansible-tmp-1651156778.54-4300-201563540311760/ /root/.ansible/tmp/ansible-tmp-1651156778.54-4300-201563540311760/AnsiballZ_command.py && sleep 0'"'"''
<172.28.102.131> (0, 'sftp> put /root/.ansible/tmp/ansible-local-42831UskiE/tmpE309AH /root/.ansible/tmp/ansible-tmp-1651156778.54-4302-235961852880307/AnsiballZ_command.py\n', '')
<172.28.102.131> ESTABLISH SSH CONNECTION FOR USER: None
<172.28.102.131> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/1d1922a709 172.28.102.131 '/bin/sh -c '"'"'chmod u+x /root/.ansible/tmp/ansible-tmp-1651156778.54-4302-235961852880307/ /root/.ansible/tmp/ansible-tmp-1651156778.54-4302-235961852880307/AnsiballZ_command.py && sleep 0'"'"''
<172.28.102.130> (0, '', '')
<172.28.102.130> ESTABLISH SSH CONNECTION FOR USER: None
<172.28.102.130> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/c3ced534af -tt 172.28.102.130 '/bin/sh -c '"'"'/usr/bin/python /root/.ansible/tmp/ansible-tmp-1651156778.54-4300-201563540311760/AnsiballZ_command.py && sleep 0'"'"''
<172.28.102.131> (0, '', '')
<172.28.102.131> ESTABLISH SSH CONNECTION FOR USER: None
<172.28.102.131> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/1d1922a709 -tt 172.28.102.131 '/bin/sh -c '"'"'/usr/bin/python /root/.ansible/tmp/ansible-tmp-1651156778.54-4302-235961852880307/AnsiballZ_command.py && sleep 0'"'"''
<172.28.102.130> (0, '\r\n{"changed": true, "end": "2022-04-28 22:39:39.024925", "stdout": "zabbix_agent2", "cmd": ["hostname"], "rc": 0, "start": "2022-04-28 22:39:39.004164", "stderr": "", "delta": "0:00:00.020761", "invocation": {"module_args": {"creates": null, "executable": null, "_uses_shell": false, "strip_empty_ends": true, "_raw_params": "hostname", "removes": null, "argv": null, "warn": true, "chdir": null, "stdin_add_newline": true, "stdin": null}}}\r\n', 'Shared connection to 172.28.102.130 closed.\r\n')
<172.28.102.130> ESTABLISH SSH CONNECTION FOR USER: None
<172.28.102.130> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/c3ced534af 172.28.102.130 '/bin/sh -c '"'"'rm -f -r /root/.ansible/tmp/ansible-tmp-1651156778.54-4300-201563540311760/ > /dev/null 2>&1 && sleep 0'"'"''
<172.28.102.130> (0, '', '')
<172.28.102.131> (0, '\r\n{"changed": true, "end": "2022-04-28 22:39:39.048157", "stdout": "zabbix_proxy", "cmd": ["hostname"], "rc": 0, "start": "2022-04-28 22:39:39.026766", "stderr": "", "delta": "0:00:00.021391", "invocation": {"module_args": {"creates": null, "executable": null, "_uses_shell": false, "strip_empty_ends": true, "_raw_params": "hostname", "removes": null, "argv": null, "warn": true, "chdir": null, "stdin_add_newline": true, "stdin": null}}}\r\n', 'Shared connection to 172.28.102.131 closed.\r\n')
<172.28.102.131> ESTABLISH SSH CONNECTION FOR USER: None
<172.28.102.131> SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 -o ControlPath=/root/.ansible/cp/1d1922a709 172.28.102.131 '/bin/sh -c '"'"'rm -f -r /root/.ansible/tmp/ansible-tmp-1651156778.54-4302-235961852880307/ > /dev/null 2>&1 && sleep 0'"'"''
172.28.102.130 | CHANGED | rc=0 >>
zabbix_agent2
<172.28.102.131> (0, '', '')
172.28.102.131 | CHANGED | rc=0 >>
zabbix_proxy
META: ran handlers
META: ran handlers

ansible常用模块

  1. command模块
    在远程主机执行命令,默认模块,可以忽略-m选项
    在这里插入图片描述

  2. shell模块
    和command模块相似,用shell执行命令
    在这里插入图片描述

  3. script模块:运行脚本
    在这里插入图片描述

  4. copy模块:可以将管理机的文件复制到其他被管理主机上
    在这里插入图片描述

  5. fetch模块:从客户端取文件至服务端,与copy相反,目录可先tar
    在这里插入图片描述

  6. file模块:设置文件属性
    在这里插入图片描述

  7. archive和unarchive模块(打包和解包模块)
    具体用法可以通过ansible-doc -s 模块名 去查看

  8. hostname模块:管理主机名
    在这里插入图片描述

  9. cron模块:计划任务
    在这里插入图片描述

  10. yum模块:管理包
    在这里插入图片描述

  11. service模块:管理服务
    在这里插入图片描述

  12. user模块:管理用户
    在这里插入图片描述

  13. group模块:管理组

ansible-galaxy

在这里插入图片描述

学习产出:

能够自己写简单的playbook

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值