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