# Ansible学习笔记
在官方文档中,你可以看到内置插件列表,传送门 https://docs.ansible.com/ansible/latest/collections/ansible/builtin/index.html#plugin-index (opens new window)
- 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
参数指定并发数。默认值是5
。 ansible
或ansible-playbook
命令行都可以使用-e
参数来添加其他环境变量,如-e "name=Nginx"
,从文件读取(注意在文件名前加@
符号),如-e "@var.yaml"
,此时var.yaml
内容如下即可:
---
name: Nginx
1
2
2
- 可以使用
register
关键字可以来注册变量,注册变量后,可以在剧本后续的任务中使用该变量。可以参考: debug调试模块 或 Command命令模块 - 可以使用
with_items
对列表进行循环读取,可参考 find查找模块 或lineinfile文件内容修改模块, ansible循环更详细的说明请参考 ansible循环 。 - 在剧本文件中可以使用
when
关键字来进行条件判断。when
的值是一个条件表达式,如果条件判断成立,则对应的task任务会执行,否则对应的task任务不执行。这里所说的成立与不成立,就是Python语言中的True
与False
。条件表达式也支持多个条件之间and
或者or
。如果我们使用一个变量进行相应的判断,一定要清楚该变量的数据类型。可以参考when条件判断 。 - 在剧本文件中可以给任务设置不同的标签,
tags
可以定义任务的标签。可以参考Tags标签 。 - 在任务执行完成后,可以触发指定的任务,这个时候就可以使用handlers触发器,可以参考handlers触发器 。
- 可以使用ansible-vault保护敏感数据,可以参考ansible-vault数据加密 。
- 如果要对数据进行某些过滤处理,可以使用过滤器
filter
来完成,详细可参考 过滤器 。 - 自定义fact事实变量,可参考 setup事实变量模块。
- 编写facts模块,可参考 编写facts模块。
- 从外部数据拉取信息存放到变量中,可以使用lookups插件,可参考 lookups插件。
- Ansible角色,可参考以下Role角色系列教程:
- ansible role角色(1),role角色概述与相关功能概述。
- ansible role角色(2)--创建第一个role角色,创建第一个测试role角色。
- ansible role角色(3)--一步一步学role角色。
Ansible内置模块:
Modules
- add_host module – Add a host (and alternatively a group) to the ansible-playbook in-memory inventory
- apt module – Manages apt-packages
- apt_key module – Add or remove an apt key
- apt_repository module – Add and remove APT repositories
- assemble module – Assemble configuration files from fragments
- assert module – Asserts given expressions are true
- async_status module – Obtain status of asynchronous task
- blockinfile module – Insert/update/remove a text block surrounded by marker lines
- command module – Execute commands on targets
- copy module – Copy files to remote locations
- cron module – Manage cron.d and crontab entries
- debconf module – Configure a .deb package
- debug module – Print statements during execution
- dnf module – Manages packages with the dnf package manager
- dpkg_selections module – Dpkg package selection selections
- expect module – Executes a command and responds to prompts
- fail module – Fail with custom message
- fetch module – Fetch files from remote nodes
- file module – Manage files and file properties
- find module – Return a list of files based on specific criteria
- gather_facts module – Gathers facts about remote hosts
- get_url module – Downloads files from HTTP, HTTPS, or FTP to node
- getent module – A wrapper to the unix getent utility
- git module – Deploy software (or files) from git checkouts
- group module – Add or remove groups
- group_by module – Create Ansible groups based on facts
- hostname module – Manage hostname
- import_playbook module – Import a playbook
- import_role module – Import a role into a play
- import_tasks module – Import a task list
- include module – Include a task list
- include_role module – Load and execute a role
- include_tasks module – Dynamically include a task list
- include_vars module – Load variables from files, dynamically within a task
- iptables module – Modify iptables rules
- known_hosts module – Add or remove a host from the known_hosts file
- lineinfile module – Manage lines in text files
- meta module – Execute Ansible ‘actions’
- package module – Generic OS package manager
- package_facts module – Package information as facts
- pause module – Pause playbook execution
- ping module – Try to connect to host, verify a usable python and return pong on success
- pip module – Manages Python library dependencies
- raw module – Executes a low-down and dirty command
- reboot module – Reboot a machine
- replace module – Replace all instances of a particular string in a file using a back-referenced regular expression
- rpm_key module – Adds or removes a gpg key from the rpm db
- script module – Runs a local script on a remote node after transferring it
- service module – Manage services
- service_facts module – Return service state information as fact data
- set_fact module – Set host variable(s) and fact(s).
- set_stats module – Define and display stats for the current ansible run
- setup module – Gathers facts about remote hosts
- shell module – Execute shell commands on targets
- slurp module – Slurps a file from remote nodes
- stat module – Retrieve file or file system status
- subversion module – Deploys a subversion repository
- systemd module – Manage systemd units
- systemd_service module – Manage systemd units
- sysvinit module – Manage SysV services.
- tempfile module – Creates temporary files and directories
- template module – Template a file out to a target host
- unarchive module – Unpacks an archive after (optionally) copying it from the local machine
- uri module – Interacts with webservices
- user module – Manage user accounts
- validate_argument_spec module – Validate role argument specs.
- wait_for module – Waits for a condition before continuing
- wait_for_connection module – Waits until remote system is reachable/usable
- yum module – Manages packages with the yum package manager
- yum_repository module – Add or remove YUM repositories
- Ansible初体验-Ansible的基本使用
- Ping连接测试模块
- Debug调试模块
- Command命令模块
- Shell执行远程脚本模块
- Cron定时任务模块
- User用户模块
- Group用户组模块
- Copy复制模块
- File文件模块
- Yum包模块
- Service服务模块
- Script执行本地脚本模块-不推荐
- Setup事实变量模块
- Fetch从远程复制文件模块
- Find查找模块
- Firewalld防火墙模块
- get_url下载文件到远程节点模块
- git远程仓库检出模块
- git_config git配置模块
- hostname修改主机名模块
- htpasswd用户认证模块
- jenkins_job Jenkins任务管理模块
- jenkins_job_info Jenkins任务信息模块
- ldap_attrs LDAP属性模块
- lineinfile文件内容修改模块
- blockinfile文件块模块
- mail邮件模块
- make编译模块
- pip python库管理模块
- tempfile临时文件模块
- template模板模块
- timezone时区模块
- wait_for条件等待模块
- wait_for_connection等待远程主机连接模块
- unarchive解压模块
- ansible when条件判断
- ansible循环
- ansible-vault数据加密
- ansible handlers触发器
- ansible tags标签
- ansible fiter过滤器
- lookups插件
- 编写facts模块
- ansible role角色(1)
- ansible role角色(2)--创建第一个role角色
- ansible role角色(3)--一步一步学role角色
你也可以直接看大佬的博客:
朱双印大佬的ansible轻松入门系列:
- ansible笔记(1):ansible的基本概念 (opens new window)
- ansible笔记(2):清单配置详解 (opens new window)
- ansible笔记(3):ansible模块的基本使用 (opens new window)
- ansible笔记(4):常用模块之文件操作 (opens new window)
- ansible笔记(5):常用模块之文件操作(二) (opens new window)
- ansible笔记(6):常用模块之命令类模块 (opens new window)
- ansible笔记(7):常用模块之系统类模块 (opens new window)
- ansible笔记(8):常用模块之系统类模块(二) (opens new window)
- ansible笔记(9):常用模块之包管理模块 (opens new window)
- ansible笔记(10):初识ansible playbook (opens new window)
- ansible笔记(11):初识ansible playbook(二) (opens new window)
- ansible笔记(12):handlers的用法 (opens new window)
- ansible笔记(13):tags的用法 (opens new window)
- ansible笔记(14):变量(一) (opens new window)
- ansible笔记(15):变量(二) (opens new window)
- ansible笔记(16):变量(三) (opens new window)
- ansible笔记(17):变量(四) (opens new window)
- ansible笔记(18):变量(五) (opens new window)
- ansible笔记(19):循环(一) (opens new window)
- ansible笔记(20):循环(二) (opens new window)
- ansible笔记(21):循环(三) (opens new window)
- ansible笔记(22):循环(四) (opens new window)
- ansible笔记(23):循环(五) (opens new window)
- ansible笔记(24):循环(六) (opens new window)
- ansible笔记(25):循环(七) (opens new window)
- ansible笔记(26):条件判断 (opens new window)
- ansible笔记(27):条件判断与tests (opens new window)
- ansible笔记(28):条件判断与block (opens new window)
- ansible笔记(29):条件判断与错误处理 (opens new window)
- ansible笔记(30):过滤器(一) (opens new window)
- ansible笔记(31):变量(六) (opens new window)
- ansible笔记(32):过滤器(二) (opens new window)
- ansible笔记(33):过滤器(三) (opens new window)
- ansible笔记(34):lookup插件 (opens new window)
- ansible笔记(35):循环(八) (opens new window)
- ansible笔记(36):include (opens new window)
- ansible笔记(37):include(二) (opens new window)
- ansible笔记(38):jinja2模板(一) (opens new window)
- ansible笔记(39):jinja2模板(二) (opens new window)
- ansible笔记(40):jinja2模板(三) (opens new window)
- ansible笔记(41):jinja2模板(四) (opens new window)
- ansible笔记(42):角色 (opens new window)
- ansible笔记(43):使用ansible-vault加密数据 (opens new window)
- ansible笔记(44):变量(七) (opens new window)
- ansible笔记(45):常用技巧(一) (opens new window)
- ansible笔记(46):常用技巧(二) (opens new window)
也可以看看马龙帅(网名"骏马金龙")大佬的系列文章: 一步到位玩透 Ansible (opens new window)