紧接上文:Ansible基础(复习1)
一、配置文件
对于 ansible 而言,它要执行任务时需要读取 ansible.cfg 配置文件,读取的顺序如下:
1、读取环境变量中的配置
2、读取项目下的 ansible.cfg 文件
3、当前用户家目录下的 .ansible.cfg
4、读取默念路径下的 ansible.cfg 文件(/etc/ansible/ansible.cfg),该文件在安装了ansible之后会自动创建
二、主机清单
可预先设置免密登录,避免在实验中频繁地输入密码。
2.1定义方式
1、使用IP地址
[root@ansible ~]# cd demo1/
[root@ansible demo1]# cat inventory
192.168.72.64
192.168.72.65
2、使用主机名(需配置/etc/hosts)
[root@ansible demo1]# cat inventory
node1
node2
3、使用范围
[root@ansible demo1]# cat inventory
node[1:2]
192.168.72.6[4:5]
4、主机组(需组名,在运行时可指定组)
[root@ansible demo1]# cat inventory
[server]
node[1:2]
192.168.72.6[4:5]
5、子主机组[子组名:children]
[server]
node[1:2]
192.168.72.6[4:5]
[db]
192.168.72.6[4:5]
[web:children]
server
db
2.2查看主机组
(输出受inventory主机清单影响)
[root@ansible demo1]# ansible all --list-hosts
hosts (4):
node1
node2
192.168.72.64
192.168.72.65
2.3常用命令
# 用于设置Ansible配置文件
ansible-config
# 查看Ansible相关模块的文档
ansible-doc
# 查看ansible控制台
ansible-console
# 用于生成角色目录结构
ansible-galaxy
# 查看 ansible 的主机清单
ansible-inventory
# 用于执行剧本
ansible-playbook
# 拉取模块的
ansible-pull
# ansible 相关文件加密或解密
ansible-vault
1、ansible-config
[root@ansible demo2]# ansible-config init > ansible.cfg
[root@ansible demo2]# vim ansible.cfg
打开后可看到默认主机清单文件配置为inventory=/etc/ansible/hosts,需结合实际修改
......
# (pathlist) Comma separated list of Ansible inventory sources
inventory=/etc/ansible/hosts
......
2、ansible-doc
[root@ansible ~]# ansible-doc ping
> ANSIBLE.BUILTIN.PING (/usr/lib/python3.9/site-packages/ansible/modules/ping.py)
A trivial test module, this module always returns `pong' on successful contact.
It does not make sense in playbooks, but it is useful from `/usr/bin/ansible' to
verify the ability to login and that a usable Python is configured. This is NOT
ICMP ping, this is just a trivial test module that requires Python on the remote-
node. For Windows targets, use the [ansible.windows.win_ping] module instead. For
Network targets, use the [ansible.netcommon.net_ping] module instead.
ADDED IN: historical
OPTIONS (= is mandatory):
- data
Data to return for the `ping' return value.
If this parameter is set to `crash', the module will cause an exception.
default: pong
type: str
3、ansible-console
[root@ansible ~]# ansible-console
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not
match 'all'
Welcome to the ansible console. Type help or ? to list commands.
root@all (0)[f:5]$
4、ansible-inventory
[root@ansible ~]# cd demo1
[root@ansible demo1]# ansible-inventory --list
{
"_meta": {
"hostvars": {}
},
"all": {
"children": [
"ungrouped",
"server"
]
},
"server": {
"hosts": [
"node1",
"node2"
]
}
}
ansible-playbook
ansible-pull
ansible-vault将在下文介绍
三、模块与集合
3.1、yaml文件
ansible剧本=ymal文件,使用playbook运行剧本
文件扩展名:.yml或.yaml
严格缩进:用2个空格(不能用Tab)
键值对格式:key:value(冒号后必须加空格)
列表用-开头:-项目
注释以#开头
3.2、file
path(必须):指定远程主机目录或文件目录
recurse(可选):递归授权
state(可选):指定模块创建类型,可以是以下值之一:
directory:在远端创建目录。
touch:在远端创建文件。
link:在远端创建链接文件。
absent:确保文件或目录不存在,如果文件或目录存在,则删除文件或目录。
mode(可选):设置文件或目录权限。
owner(可选):设置文件或目录所属者信息。
group(可选):设置文件或目录所属组信息。
例如:
[root@ansible demo2]# ls
ansible.cfg inventory
[root@ansible demo2]# cat ansible.cfg
[defaults]
inventory=inventory/hosts
[root@ansible demo2]# cat inventory/hosts
[server]
node[1:2]
[root@ansible demo2]# vim site.yml
[root@ansible demo2]# ansible-playbook site.yml
PLAY [create file module] ******************************************************************
TASK [Gathering Facts] *********************************************************************
ok: [node1]
ok: [node2]
TASK [create a directory] ******************************************************************
changed: [node1]
changed: [node2]
PLAY RECAP *********************************************************************************
node1 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@ansible demo2]# cat site.yml
- name: create file module
hosts: server
tasks:
- name: create a directory
ansible.builtin.file:
path: /root/d1
state: directory
mode: 0775
owner: root
group: root
其中hosts中的之与hosts中的定义相同,全部就all
[root@node1 ~]# ls
d1
3.3、template(重要)
template 模块用于定义项目需要的文件。模板采用的是 Jinja2 语法
(https://jinja.palletsprojects.com/en/stable/ )。
template 模块常用参数说明:
src(必须):模板文件源路径
dest(必须):存放模板文件目标路径
owner:指定复制后文件的所有者。
group:指定复制后文件的所属用户组。
mode:指定复制后文件的权限模式。可以使用数字或符号表示法(如:0644 或 u=rw,g=r,o=r)。
[root@ansible demo3]# tree
.
├── ansible.cfg
├── inventory
│ ├── hosts
│ └── host_vars
│ ├── node1.yml
│ └── node2.yml
├── site.yml
└── templates
└── test.j2
其中---是指可以添加多个任务,将在下文具体体现
[root@ansible demo3]# cat ansible.cfg
[defaults]
inventory=inventory/hosts
[root@ansible demo3]# cat inventory/hosts
[server]
node[1:2]
[root@ansible demo3]# cat inventory/host_vars/node1.yml
http_port: 81
host_prefix: test1-
ip_prefix: 192.168.1
[root@ansible demo3]# cat inventory/host_vars/node2.yml
http_port: 81
host_prefix: test2-
ip_prefix: 192.168.2.
[root@ansible demo3]# cat site.yml
---
- name: template module demo
hosts: server
tasks:
- name: template demo
ansible.builtin.template:
src: templates/test.j2
dest: /root/test.conf
[root@ansible demo3]# cat templates/test.j2
[default]
http_port = {{ http_port }}
[demo]
{% for id in range(201, 210) %}
{{ host_prefix }}{{ "%02d" | format(id-200) }} = {{ ip_prefix }}{{ id }}
{% endfor %}
test.j2会自动读取host_vars中的变量
'{{ }}'类似shell指令
[root@ansible demo3]# ansible-playbook site.yml
PLAY [template module demo] ****************************************************************
TASK [Gathering Facts] *********************************************************************
ok: [node1]
ok: [node2]
TASK [template demo] ***********************************************************************
changed: [node2]
changed: [node1]
PLAY RECAP *********************************************************************************
node1 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@node1 ~]# cat test.conf
[default]
http_port = 81
[demo]
test1-01 = 192.168.1201
test1-02 = 192.168.1202
test1-03 = 192.168.1203
test1-04 = 192.168.1204
test1-05 = 192.168.1205
test1-06 = 192.168.1206
test1-07 = 192.168.1207
test1-08 = 192.168.1208
test1-09 = 192.168.1209
[root@node2 ~]# cat test.conf
[default]
http_port = 81
[demo]
test2-01 = 192.168.2.201
test2-02 = 192.168.2.202
test2-03 = 192.168.2.203
test2-04 = 192.168.2.204
test2-05 = 192.168.2.205
test2-06 = 192.168.2.206
test2-07 = 192.168.2.207
test2-08 = 192.168.2.208
test2-09 = 192.168.2.209
此外还有多种ansible.bulitin工具,如dnf,copy,fetch,nginx等等。可前往文档查看。