Featured image of post Ansible学习笔记

Ansible学习笔记

Ansible学习笔记

在官方文档中,你可以看到内置插件列表,传送门 https://docs.ansible.com/ansible/latest/collections/ansible/builtin/index.html#plugin-index

  • Ansible集合了众多优秀运维工具(Puppet、Cfengine、Chef、Func、Fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
  • Ansible是基于模块工作的。本身没有批量部署的能力。真正具有批量部署的是Ansible所运行的模块,Ansible只是提供一种框架。
  • Ansible采用paramiko协议库,通过SSH或者ZeroMQ等连接远程主机。
  • Ansible是基于一致性、安全性、高可靠性设计的轻量级自动化工具。
  • Ansible的配置管理脚本playbook语法基于YAML,它是一种可读性高、用来表达数据序列的格式标准。
  • Ansible模块是声明式的(declarative),你只需使用这些模块描述被管节点期望达到的状态。
  • Ansible内置模块都是等幂性的(idempotent),在被管节点上多次执行Ansible的剧本能达到同样的效果。
  • 等幂性是指如果系统已经处于期望的状态,则对系统什么也不操作。
  • Ansible命令都是并发执行的,我们可以针对目标主机执行任何命令。
  • 默认的并发数目由/etc/ansible/ansible.cfg中的forks值来控制的。也可以在运行Ansible命令的时候通过-f/--forks参数指定并发数。默认值是5单台主机的性能始终有限,大家根据自己机器的硬件配置做调整,建议并发数配置为CPU核数偶数倍就好。如4CPU 8GB的服务器,建议最多并发20个线程。
  • 除了增加并发数,也可以通过以下方式优化Ansible速度,详细可参考优化Ansible速度
    • 开启SSH长连接。
    • 开启pipelining。
    • 开启accelerate模式。
    • 设置facts缓存。
  • ansibleansible-playbook命令行都可以使用-e参数来添加其他环境变量,如-e "name=Nginx",从文件读取(注意在文件名前加@符号),如-e "@var.yaml",此时var.yaml内容如下即可:
1
2
---
name: Nginx
  • 可以使用register关键字可以来注册变量,注册变量后,可以在剧本后续的任务中使用该变量。可以参考: debug调试模块Command命令模块
  • 可以使用with_items对列表进行循环读取,可参考 find查找模块lineinfile文件内容修改模块, ansible循环更详细的说明请参考 ansible循环
  • 在剧本文件中可以使用when关键字来进行条件判断。when的值是一个条件表达式,如果条件判断成立,则对应的task任务会执行,否则对应的task任务不执行。这里所说的成立与不成立,就是Python语言中的TrueFalse。条件表达式也支持多个条件之间and或者or。如果我们使用一个变量进行相应的判断,一定要清楚该变量的数据类型。可以参考when条件判断
  • when条件语句类似,我们可以使用changed_when语句和failed_when语句来对命令运行的结果进行判断。对于Ansible来说,其很难判断一个命令的运行是否符合我们的实际预期,尤其是当我们使用command模块和shell模块时,如果不使用changed_when语句,Ansible将永远返回changed。大部分模块都能正确返回运行结果是否对目标主机产生影响,我们依然可以使用changed_when语句来对返回信息进行重写,根据任务返回结果来判定任务的运行结果是否真正符合我们预期。可以参考changed_when与failed_when条件判断
  • 在剧本文件中可以给任务设置不同的标签,tags可以定义任务的标签。可以参考Tags标签
  • 在任务执行完成后,可以触发指定的任务,这个时候就可以使用handlers触发器,可以参考handlers触发器
  • 可以使用ansible-vault保护敏感数据,可以参考ansible-vault数据加密
  • 如果要对数据进行某些过滤处理,可以使用过滤器filter来完成,详细可参考 过滤器
  • 自定义fact事实变量,可参考 setup事实变量模块
  • 编写facts模块,可参考 编写facts模块
  • 在剧本执行失败时,如果你想Ansible给你发邮件,可以参考callback回调插件
  • 从外部数据拉取信息存放到变量中,可以使用lookups插件,可参考 lookups插件
  • Ansible角色,可参考以下Role角色系列教程:

Ansible内置模块:

Modules

  1. add_host module – Add a host (and alternatively a group) to the ansible-playbook in-memory inventory
  2. apt module – Manages apt-packages
  3. apt_key module – Add or remove an apt key
  4. apt_repository module – Add and remove APT repositories
  5. assemble module – Assemble configuration files from fragments
  6. assert module – Asserts given expressions are true
  7. async_status module – Obtain status of asynchronous task
  8. blockinfile module – Insert/update/remove a text block surrounded by marker lines
  9. command module – Execute commands on targets
  10. copy module – Copy files to remote locations
  11. cron module – Manage cron.d and crontab entries
  12. debconf module – Configure a .deb package
  13. debug module – Print statements during execution
  14. dnf module – Manages packages with the dnf package manager
  15. dpkg_selections module – Dpkg package selection selections
  16. expect module – Executes a command and responds to prompts
  17. fail module – Fail with custom message
  18. fetch module – Fetch files from remote nodes
  19. file module – Manage files and file properties
  20. find module – Return a list of files based on specific criteria
  21. gather_facts module – Gathers facts about remote hosts
  22. get_url module – Downloads files from HTTP, HTTPS, or FTP to node
  23. getent module – A wrapper to the unix getent utility
  24. git module – Deploy software (or files) from git checkouts
  25. group module – Add or remove groups
  26. group_by module – Create Ansible groups based on facts
  27. hostname module – Manage hostname
  28. import_playbook module – Import a playbook
  29. import_role module – Import a role into a play
  30. import_tasks module – Import a task list
  31. include module – Include a task list
  32. include_role module – Load and execute a role
  33. include_tasks module – Dynamically include a task list
  34. include_vars module – Load variables from files, dynamically within a task
  35. iptables module – Modify iptables rules
  36. known_hosts module – Add or remove a host from the known_hosts file
  37. lineinfile module – Manage lines in text files
  38. meta module – Execute Ansible ‘actions’
  39. package module – Generic OS package manager
  40. package_facts module – Package information as facts
  41. pause module – Pause playbook execution
  42. ping module – Try to connect to host, verify a usable python and return pong on success
  43. pip module – Manages Python library dependencies
  44. raw module – Executes a low-down and dirty command
  45. reboot module – Reboot a machine
  46. replace module – Replace all instances of a particular string in a file using a back-referenced regular expression
  47. rpm_key module – Adds or removes a gpg key from the rpm db
  48. script module – Runs a local script on a remote node after transferring it
  49. service module – Manage services
  50. service_facts module – Return service state information as fact data
  51. set_fact module – Set host variable(s) and fact(s).
  52. set_stats module – Define and display stats for the current ansible run
  53. setup module – Gathers facts about remote hosts
  54. shell module – Execute shell commands on targets
  55. slurp module – Slurps a file from remote nodes
  56. stat module – Retrieve file or file system status
  57. subversion module – Deploys a subversion repository
  58. systemd module – Manage systemd units
  59. systemd_service module – Manage systemd units
  60. sysvinit module – Manage SysV services.
  61. tempfile module – Creates temporary files and directories
  62. template module – Template a file out to a target host
  63. unarchive module – Unpacks an archive after (optionally) copying it from the local machine
  64. uri module – Interacts with webservices
  65. user module – Manage user accounts
  66. validate_argument_spec module – Validate role argument specs.
  67. wait_for module – Waits for a condition before continuing
  68. wait_for_connection module – Waits until remote system is reachable/usable
  69. yum module – Manages packages with the yum package manager
  70. yum_repository module – Add or remove YUM repositories
  1. Ansible初体验-Ansible的基本使用
  2. Ping连接测试模块
  3. Debug调试模块
  4. Command命令模块
  5. Shell执行远程脚本模块
  6. Cron定时任务模块
  7. User用户模块
  8. Group用户组模块
  9. Copy复制模块
  10. File文件模块
  11. Yum包模块
  12. Service服务模块
  13. Script执行本地脚本模块-不推荐
  14. Setup事实变量模块
  15. Fetch从远程复制文件模块
  16. Find查找模块
  17. Firewalld防火墙模块
  18. get_url下载文件到远程节点模块
  19. git远程仓库检出模块
  20. git_config git配置模块
  21. hostname修改主机名模块
  22. htpasswd用户认证模块
  23. jenkins_job Jenkins任务管理模块
  24. jenkins_job_info Jenkins任务信息模块
  25. ldap_attrs LDAP属性模块
  26. lineinfile文件内容修改模块
  27. blockinfile文件块模块
  28. mail邮件模块
  29. make编译模块
  30. pip python库管理模块
  31. tempfile临时文件模块
  32. template模板模块
  33. timezone时区模块
  34. wait_for条件等待模块
  35. wait_for_connection等待远程主机连接模块
  36. unarchive解压模块
  37. ansible when条件判断
  38. changed_when与failed_when条件判断
  39. ansible循环
  40. ansible-vault数据加密
  41. ansible handlers触发器
  42. ansible tags标签
  43. ansible fiter过滤器
  44. lookups插件
  45. 编写facts模块
  46. ansible role角色(1)–role角色概述与相关功能概述
  47. ansible role角色(2)–创建第一个role角色
  48. ansible role角色(3)–一步一步学role角色
  49. ansible role角色(4)–include的使用
  50. 优化Ansible速度
  51. ansible role角色(5)–使用ansible配置supervisor进程管理角色
  52. ansible role角色(6)–使用ansible配置redis数据库角色

你也可以直接看大佬的博客:

朱双印大佬的ansible轻松入门系列:

  1. ansible笔记(1):ansible的基本概念
  2. ansible笔记(2):清单配置详解
  3. ansible笔记(3):ansible模块的基本使用
  4. ansible笔记(4):常用模块之文件操作
  5. ansible笔记(5):常用模块之文件操作(二)
  6. ansible笔记(6):常用模块之命令类模块
  7. ansible笔记(7):常用模块之系统类模块
  8. ansible笔记(8):常用模块之系统类模块(二)
  9. ansible笔记(9):常用模块之包管理模块
  10. ansible笔记(10):初识ansible playbook
  11. ansible笔记(11):初识ansible playbook(二)
  12. ansible笔记(12):handlers的用法
  13. ansible笔记(13):tags的用法
  14. ansible笔记(14):变量(一)
  15. ansible笔记(15):变量(二)
  16. ansible笔记(16):变量(三)
  17. ansible笔记(17):变量(四)
  18. ansible笔记(18):变量(五)
  19. ansible笔记(19):循环(一)
  20. ansible笔记(20):循环(二)
  21. ansible笔记(21):循环(三)
  22. ansible笔记(22):循环(四)
  23. ansible笔记(23):循环(五)
  24. ansible笔记(24):循环(六)
  25. ansible笔记(25):循环(七)
  26. ansible笔记(26):条件判断
  27. ansible笔记(27):条件判断与tests
  28. ansible笔记(28):条件判断与block
  29. ansible笔记(29):条件判断与错误处理
  30. ansible笔记(30):过滤器(一)
  31. ansible笔记(31):变量(六)
  32. ansible笔记(32):过滤器(二)
  33. ansible笔记(33):过滤器(三)
  34. ansible笔记(34):lookup插件
  35. ansible笔记(35):循环(八)
  36. ansible笔记(36):include
  37. ansible笔记(37):include(二)
  38. ansible笔记(38):jinja2模板(一)
  39. ansible笔记(39):jinja2模板(二)
  40. ansible笔记(40):jinja2模板(三)
  41. ansible笔记(41):jinja2模板(四)
  42. ansible笔记(42):角色
  43. ansible笔记(43):使用ansible-vault加密数据
  44. ansible笔记(44):变量(七)
  45. ansible笔记(45):常用技巧(一)
  46. ansible笔记(46):常用技巧(二)

也可以看看马龙帅(网名"骏马金龙")大佬的系列文章: 一步到位玩透 Ansible

Licensed under the GNU General Public License v3.0