首页 > 其他分享 >playbook变量(10)

playbook变量(10)

时间:2022-11-22 14:47:29浏览次数:33  
标签:10 name 主机 ansible ssh 变量 playbook

playbook变量的使用

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

变量的定义

variable=value

示范

http_port=80

变量的调用方式

通过{{ variable_name }} 调用变量,且变量名前后建议加空格,有时用“{{ variable_name }}”才生效


ansible setup facts 远程主机的所有变量都可直接调用 (系统自带变量)
       setup模块可以实现系统中很多系统信息的显示
                可以返回每个主机的系统信息包括:版本、主机名、cpu、内存
       ansible all -m setup -a 'filter="ansible_nodename"'     查询主机名
       ansible all -m setup -a 'filter="ansible_memtotal_mb"'  查询主机内存大小
       ansible all -m setup -a 'filter="ansible_distribution_major_version"'  查询系统版本
       ansible all -m setup -a 'filter="ansible_processor_vcpus"' 查询主机cpu个数
通过命令行指定变量,优先级最高
       ansible-playbook –e varname=value

变量的来源

1.ansible 的 setup facts 远程主机的所有变量都可直接调用
2.通过命令行指定变量,优先级最高
ansible-playbook -e varname=value
3.在playbook文件中定义

 vars:      
 - var1: value1     
 - var2: value2

4.在独立的变量YAML文件中定义

- hosts: all      
 vars_files:     
  - vars.yml

5.在 /etc/ansible/hosts 中定义
主机(普通)变量:主机组中主机单独定义,优先级高于公共变量
组(公共)变量:针对主机组中所有主机定义统一变量
6.在role中定义

使用 setup 模块中变量

本模块自动在playbook调用,不要用ansible命令调用
案例:使用setup变量

---
#var.yml
- hosts: all
  remote_user: root
  gather_facts: yes

  tasks:
    - name: create log file
      file: name=/data/{{ ansible_nodename }}.log state=touch owner=zhangsan mode=600

ansible-playbook  var.yml

在playbook 命令行中定义变量

ansible-playbook -e pkname=memcached

示例:var4.yml
- hosts: webserver
  remote_user: root
  tasks:
    - name: install package
      yum: name={{ pkname }} state=present
      
 ansible-playbook -e pkname=memcached var4.yml
vim var3.yml
---
- hosts: webserver
  remote_user: root
  vars:
    - username: user1
    - groupname: group1

  tasks:
    - name: create group
      group: name={{ groupname }} state=present
    - name: create user
      user: name={{ username }} group={{ groupname }} state=present

ansible-playbook -e "username=user2 groupname=group2”  var3.yml

使用变量文件

可以在一个独立的playbook文件中定义变量,在另一个playbook文件中引用变量文件中的变量,比playbook中定义的变量优化级高

vim vars.yml
---
# variables file
package_name: mariadb-server
service_name: mariadb

vim var5.yml
---
#install package and start service
- hosts: dbsserver
  remote_user: root
  vars_files:
    - /root/vars.yml

  tasks:
    - name: install package
      yum: name={{ package_name }}
      tags: install
    - name: start service
      service: name={{ service_name }} state=started enabled=yes

范例:

cat  vars2.yml
---
var1: httpd
var2: nginx

cat  var5.yml
---         
- hosts: web
  remote_user: root
  vars_files:
    - vars2.yml

   tasks:
     - name: create httpd log
       file: name=/app/{{ var1 }}.log state=touch
     - name: create nginx log
       file: name=/app/{{ var2 }}.log state=touch

主机清单文件中定义变量

主机变量

在inventory 主机清单文件中为指定的主机定义变量以便于在playbook中使用
范例:

[webserver]
www1.zhangsan.com http_port=80 maxRequestsPerChild=808
www2.zhangsan.com http_port=8080 maxRequestsPerChild=909

组(公共)变量

在inventory 主机清单文件中赋予给指定组内所有主机上的在playbook中可用的变量,如果和主机变是同名,优先级低于主机变量

范例:

[webserver]
www1.zhangsan.com
www2.zhangsan.com

[webserver:vars]
ntp_server=ntp.zhangsan.com
nfs_server=nfs.zhangsan.com

范例:
vim /etc/ansible/hosts

[webserver]
192.168.0.101 hname=www1 domain=zhangsan.io
192.168.0.102 hname=www2 

[webserver:vars]
mark=“-”
domain=zhansgan.org

ansible  websvrs  –m hostname –a ‘name={{ hname }}{{ mark }}{{ domain }}’
bash

命令行指定变量:

ansible websvrs –e domain=magedu.cn –m hostname –a ‘name={{ hname }}{{ mark }}{{ domain }}’

invertory参数

invertory参数:用于定义ansible远程连接目标主机时使用的参数,而非传递给playbook的变量
    ansible_ssh_host
    ansible_ssh_port
    ansible_ssh_user
    ansible_ssh_pass
    ansbile_sudo_pass

示例:
    cat /etc/ansible/hosts
    [webserver]
    192.168.0.1 ansible_ssh_user=root ansible_ssh_pass=magedu
    192.168.0.2 ansible_ssh_user=root ansible_ssh_pass=magedu
inventory参数
ansible基于ssh连接inventory中指定的远程主机时,还可以通过参数指定其交互方式;
这些参数如下所示:
ansible_ssh_host
The name of the host to connect to, if different from the alias you wishto 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 recommendusing --ask-pass or SSH keys)

ansible_sudo_pass
The sudo password to use (this is insecure, we strongly recommendusing --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/bin/env" mechanism as that requires the remote user's

path to be set right and also assumes the "python" executable is named python,where the executable might

be named something like "python26".
ansible\_\*\_interpreter

Works for anything such as ruby or perl and works just like ansible_python_interpreter.

This replaces shebang of modules which will run on that host.

标签:10,name,主机,ansible,ssh,变量,playbook
From: https://www.cnblogs.com/yutoujun/p/16915036.html

相关文章

  • playbook核心元素(8)
    playbook核心元素Hosts执行的远程主机列表Tasks任务集Variables内置变量或自定义变量在playbook中调用Templates模板,可替换模板文件中的变量并实现一些简单逻辑的......
  • playbook(9)
    基本格式ansible-playbook<filename.yml>...[options]常见选项-C--check#只检测可能会发生的改变,但不真正执行操作--list-hosts#列出运行......
  • PostgreSQL Win10 安装、创建表并添加数据(安装细节+常见错误避坑)
    1.下载:postgresql-14.5-1-windows-x64.exe  地址:DownloadPostgreSQL 2.安装:    下一步即可,注意记住期间设置的超级管理员密码和设置的端口号    ......
  • mac上解决Port already in use: 1099;
    解决方法有两种第一种:换其他的端口再次启动,例如:换1098端口,如果你不想换其他端口,则见第二种方法第二种第一步:使用lsof-itcp:1099查看时那个应用占用了此端口第二部:......
  • bdd100k数据集制作用于YOLOv3训练
    这篇博文主要记录了将bdd100k数据集整理成yolo模型的要求来训练。这里的数据格式并非官网的darknet格式,而是一般的模型格式:以图片名作为TXT名称将同一幅图中的label和对应的boundingbox整理......
  • 【数组10】数组中的逆序对
    题目描述在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数P。并将P对1000000007取模的结果......
  • 2022年11月10篇论文推荐
    随着最大的人工智能研究会议(NeurIPS2022)即将到来,我们进入了2022年的最后阶段。让我们回顾一下人工智能世界最近发生了什么。在介绍推荐论文之前,先说一个很有意思的项目......
  • PAT乙级 —— 1002 数字分类 (20)
    题目链接:​​数字分类(20)​​题目描述给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字:A1=能被5整除的数字中所有偶数的和;A2=将被5除后余1的数字按给出顺......
  • PAT乙级 —— 1004 福尔摩斯的约会 (20)
    题目链接:​​福尔摩斯的约会(20)​题目描述大侦探福尔摩斯接到一张奇怪的字条:“我们约会吧!3485djDkxh4hhGE2984akDfkkkkggEdsbs&hgsfdkd&Hyscvnm”。大侦探很快就明白......
  • PAT乙级 —— 1003 数素数 (20)
    题目链接:​​数素数(20)​​题目描述令Pi表示第i个素数。现任给两个正整数M<=N<=10000,请输出PM到PN的所有素数。输入描述:输入在一行中给出M和N,其间以空格分隔。输出描......