Linux:Ansible Playbooks,yuam语法,roles模块的详细解释

本文详细介绍了Ansible中YAML语法的基础元素,包括列表、字典、变量和条件测试等。接着,探讨了Ansible Playbooks的组成和基础组件,如任务列表、条件测试、handlers以及roles的使用。文中还讲解了如何创建和使用roles,以及如何通过ansible-galaxy下载和安装角色。最后,提到了Jinja2模板语言的相关概念,如字面量、算术运算和逻辑运算符。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

一、YAML语法简介

二、YAML语法

Ansible中使用的YAML基础元素:变量、Inventory、条件测试、迭代

1、YMAL:list 列表

2、YMAL:dictionary  字典

3、变量命名

4、facts:setup模块

5、register

6、通过命令行传递变量

7、通过roles传递变量

8、Inventory

9、 inventory文件格式

10、主机变量

11、组变量

12、组嵌套

13、inventory参数

14、条件测试

15、when语句

16、迭代

17、交互式变量​

18、在文档中定义变量,然后在playbook中调用

二、Ansible playbooks

Ansible Playbooks的组成

三、Playbook基础组件

1、Hosts和Users

2、任务列表和action

3、例子:编写PlayBooks

4、handlers:用于当关注的资源发生变化时采取一定的操作

5、roles

6、创建role的步骤

7、role内各目录中可用的文件

8、role例子

9、通过ansible-galaxy 创建角色以及相关目录

11、通过网络下载安装角色

12、使用rhel-system-roles 软件包安装角色

 13、Tags

14、使用模板文件:template

 13、文件加密:ansible-vault

14、loop工具,可以存储多个变量以及值,利用item变量 多次执行任务

15、block 可以将多个任务合并在一起,形成一个整体

 三、Jinja2相关

1、 字面量

2、算术运算

3、比较操作符

4、逻辑运算符


对于Playbook来讲是由:Inventory、Modules、Ad Hoc Commands、Playbooks【Tasks、Variables、Templates、Handlers、Roles】组成的,想要了解Playbooks,我们需要先了解下YAML语法

一、YAML语法简介

YAML是一个可读性高的用来表达资料序列的格式。YAML参考了其他多种语言,包括:XML、C语言、Python、Perl以及电子邮件格式RFC2822等。Clark Evans在2001年在首次发表了这种语言,另外Ingy döt Net与Oren Ben-Kiki也是这语言的共同设计者。

YAML Ain't Markup Language,即YAML不是XML。不过,在开发的这种语言时,YAML的意思其实是:"Yet Another Markup Language"(仍是一种标记语言)。其特性:

  • YAML的可读性好
  • YAML和脚本语言的交互性好
  • YAML使用实现语言的数据类型
  • YAML有一个一致的信息模型
  • YAML易于实现
  • YAML可以基于流来处理
  • YAML表达能力强,扩展性好
  • 更多的内容及规范参见http://www.yaml.org。

二、YAML语法

YAML的语法和其他高阶语言类似,并且可以简单表达清单、散列表、标量等数据结构。其结构(Structure)通过空格来展示,序列(Sequence)里的项用"-"来代表,Map里的键值对用":"分隔。下面是一个示例。

YAML文件扩展名通常为.yaml,如example.yaml。

name: John Smith
age: 41
gender: Male
spouse:
    name: Jane Smith
    age: 37
    gender: Female
children:
    -   name: Jimmy Smith
        age: 17
        gender: Male
    -   name: Jenny Smith
        age 13
        gender: Female

Ansible中使用的YAML基础元素:变量、Inventory、条件测试、迭代

1、YMAL:list 列表

列表的所有元素均使用“-”打头,例如:

# A list of tasty fruits
- Apple
- Orange
- Strawberry
- Mango

2、YMAL:dictionary  字典

字典通过key与value进行标识,例如:

# An employee record
name: Example Developer
job: Developer
skill: Elite

也可以将key:value放置于{}中进行表示,例如:

---
# An employee record
{name: Example Developer, job: Developer, skill: Elite}

3、变量命名

变量命名仅能由字母、数字和下划线组成,且只能以字母开头

4、facts:setup模块

facts是由正在通信的远程目标主机发回的消息,这些消息被保存在ansible变量中。要获取指定的远程主机所支持的所有facts,可使用如下命令:

#ansible hostame -m setup

  • setup模块:gather_facts: yes 开启收集(默认),要关闭用no
  • ansible_hostname  主机名称
    ansible_fqdn  完整主机名
    ansible_bios_version    bios版本信息
    ansible_memfree_mb   内存剩余空间大小
    ansible_devices.vda.partitions.vda1.size   查看vda1分区的大小
    ansible_lvm.lvs.root.size_g   查看名称是root的逻辑卷的大小
    ansible_kernel  查看内核版本
    ansible_eth0.ipv4.address   查看eth0网卡的ip地址

参数来源
ins

inventory

5、register

把任务的输出定义为变量,然后用于其他任务,示例如下:

  tasks:
     - shell: /usr/bin/foo
       register: foo_result
       ignore_errors: True

6、通过命令行传递变量

在运行playbook的时候也可以传递一些变量playbook使用,示例如下:

	ansible-playbook test.yml --extra-vars "hosts=www user=baidu"

7、通过roles传递变量

当给一个主机应用角色的时候可以传递变量,然后再角色内使用这些变量,示例如下:

- hosts: webservers
	  roles:
	    - common
	    - { role: foo_app_instance, dir: '/web/htdocs/a.com',  port: 8080 }

8、Inventory

ansible的主要功用在于批量主机操作,为了便捷地使用其中的部分主机,可以再inventory file 中将其成分组命名。默认的inventory file:/etc/ansible/hosts

9、 inventory文件格式

inventory文件遵循INI文件风格,中括号中的字符为组名。可以将同一个主机同时归并到多个不同的组中;此外,当如若目标主机使用了非默认的SSH端口,还可以在主机名称之后使用冒号加端口号来标明。

    ntp.test1.com

	[webservers]
	www1.test2.com:2222
	www2.tes3.com

	[dbservers]
	db1.test1.com
	db2.test1.com
	db3.test1.com

如果主机名称遵循相似的命名模式,还可以使用列表的方式标识各主机,例如:

[webservers]
www[01:50].example.com

[databases]
db-[a:f].example.com

10、主机变量

可以在inventory中定义主机时为其添加主机变量以便于在playbook中使用。例如:

[webservers]
www1.test1.com http_port=80 maxRequestsPerChild=808   #maxRequestsPerChild 任务最大的请求的数量
www2.test2.com http_port=8080 maxRequestsPerChild=909

11、组变量

组变量是指赋予给指定组内所有主机上的在playbook中可用的变量。例如:

[webservers]
www1.test1.com
www2.test2.com

[webservers:vars]
ntp_server=ntp.test1.com
nfs_server=nfs.test2.com

12、组嵌套

inventory中,组还可以包含其它的组,并且也可以向组中的主机指定变量。不过,这些变量只能在ansible-playbook中使用,而ansible不支持。例如:

[apache]
httpd1.test1.com
httpd2.test2.com

[nginx]
ngx1.test1.com
ngx2.test2.com

[webservers:children]
apache
nginx

[webservers:vars]
ntp_server=ntp.test1.com

13、inventory参数

ansible基于ssh连接inventory中指定的远程主机时,还可以通过参数指定其交互方式;这些参数如下所示:

ansible_ssh_host
  The name of the host to connect to, if different from the alias you wish to give to it.
ansible_ssh_port
  The ssh port number, if not 22
ansible_ssh_user
  The default ssh user name to use.
ansible_ssh_pass
  The ssh password to use (this is insecure, we strongly recommend using --ask-pass or SSH keys)
ansible_sudo_pass
  The sudo password to use (this is insecure, we strongly recommend using --ask-sudo-pass)
ansible_connection
  Connection type of the host. Candidates are local, ssh or paramiko.  The default is paramiko before Ansible 1.2, and 'smart' afterwards which detects whether usage of 'ssh' would be feasible based on whether ControlPersist is supported.
ansible_ssh_private_key_file
  Private key file used by ssh.  Useful if using multiple keys and you don't want to use SSH agent.
ansible_shell_type
  The shell type of the target system. By default commands are formatted using 'sh'-style syntax by default. Setting this to 'csh' or 'fish' will cause commands executed on target systems to follow those shell's syntax instead.
ansible_python_interpreter
  The target host python path. This is useful for systems with more
  than one Python or not located at "/usr/bin/python" such as \*BSD, or where /usr/bin/python
  is not a 2.X series Python.  We do not use the "/usr/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值