Ansible基础(复习2)

紧接上文: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

官方文档:https://docs.ansible.com/projects/ansible/latest/collections/ansible/builtin/file_module.html#ansible-collections-ansible-builti

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等等。可前往文档查看。

相关推荐
大树887 小时前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠8 小时前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质8 小时前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
bush48 小时前
嵌入式linux学习记录十四、术语
linux·嵌入式
载数而行5208 小时前
Linux 11 动态监控指令top
linux
小宇宙Zz8 小时前
Maven依赖冲突
java·服务器·maven
Inhand陈工9 小时前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
酣大智9 小时前
ARP代理--工作原理
运维·网络·arp·arp代理
不会C语言的男孩9 小时前
Linux 系统编程 · 第 8 章:进程基础
linux·c语言
shushangyun_10 小时前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化