运维
Tcpdump抓包工具
tshark抓包工具
Ansible
Ansible配置
Ansible-远程命令模块( command / script / shell )
Ansible-常用模块
PlayBook
PlayBook-变量
PlayBook-条件/循环
PlayBook-Tags
PlayBook-常用脚本
Ansible-Vault(数据安全)
Ansible-API
Ansible实践
JMeter测试软件
JMeter性能指标
Curl
综合分析工具
磁盘/IO工具
网络分析工具
JAVA分析工具
更换硬盘
Linux启动流程
安装问题
GURB加密
修改默认启动项
Root密码忘记
重装内核、GRUB
Too many open files错误
误删文件,内存恢复
Read-only file system错误
本文档使用MrDoc发布
返回首页
-
+
PlayBook-变量
2020年2月14日 16:53
admin
#一、自定义变量 ##1. 使用vars: 定义变量 ![](/media//201911/2019-11-14220803449322.png) --- ##2. 使用var_files:定义变量文件 - hosts: web remote_user: root vars_files: #定义参数文件 - vars/server_vars.yml tasks: - name: insert firewalld rule for httpd firewalld: port={{ http_port }}/tcp permanent=true state=enabled immediate=yes #{{ http_port }}引用参数文件中的变量 --- vars/server_vars.yml文件内容: http_port: 80 --- ##3. 在组的主机后面添加变量 vim /etc/ansible/hosts ![](/media//201911/2019-11-14220911688332.png) ![](/media//201911/2019-11-14220951941938.png) --- ##4. 定义复杂变量 #定义: foo: field1: one field2: two -------------------------------------------------------- #引用 foo["field1"] 或 foo.field1 --- #二、远程主机系统变量 ####每个Playbook在执行前,都会通过setup模块收集Facts信息(远程主机信息),这些信息可以直接以变量的形式使用 [root@G-frafana_10-188-100-55 ~]# ansible 10.188.100.55 -m setup 10.188.100.55 | SUCCESS => { "ansible_facts": { "ansible_all_ipv4_addresses": [ "116.31.71.155", "150.242.57.11", "10.188.100.55" ], "ansible_all_ipv6_addresses": [], "ansible_apparmor": { "status": "disabled" }, "ansible_architecture": "x86_64", "ansible_bios_date": "04/05/2016", "ansible_bios_version": "6.00", "ansible_cmdline": { "BOOT_IMAGE": "/vmlinuz-3.10.0-1127.19.1.el7.x86_64", "LANG": "zh_CN.UTF-8", "biosdevname": "0", "crashkernel": "auto", "ipv6.disable": "1", "net.ifnames": "0", "quiet": true, "rd.lvm.lv": "VolGroup/swap", "rhgb": true, "ro": true, "root": "/dev/mapper/VolGroup-root" }, "ansible_date_time": { "date": "2022-02-14", "day": "14", --- ####引用变量 {{ ansible_date_time["date"] }} 或者 {{ ansible_date_time.date }} --- ##关闭Facts,可提升ansible性能 #在playbook中,通过gater_facts来控制是否收集fasts信息 - hosts: web gater_facts:no #不收集Facts信息 remote_user: root vars_files: - vars/server_vars.yml tasks: - name: insert firewalld rule for httpd firewalld: port={{ http_port }}/tcp permanent=true state=enabled immediate=yes --- #三、文件模板使用变量 ####playbook.yaml文件: - hosts: web vars: http_port: 80 defined_name: "Hello My name is Bao" tasks: - name: installed httpd yum: pkg=httpd state=latest - name: changed index_file template: src: templates/index2.html.j2 dest: /var/www/html/index.html - name: start httpd service: name=httpd state=restarted --- ####index2.html.j2文件: <!DOCTYPE html> <html> <head> <title>Demo</title> </head> <body> <div> <h1>Demo {{ defined_name }}</h1>> <!--defined_name为yml文件中定义的变量--> <p>Served by {{ ansible_hostname }} ({{ ansible_default_ipv4.address }})</p> <!--ansible_hostname与ansible_default_ipv4.address为远程主机系统变量--> </div> </body> </html> --- #四、注册变量 - hosts: web tasks: - name: Register_Var #注册变量 register: result ignore_errors: True - name: Echo_Register_Var #引用变量 shell: echo "{{ result.stdout }}" when: result.rc == 5 - name: Echo_Debug #引用变量 debug: msg:"{{ result.stdout }}" --- #五、额外变量(传参) ####定义变量 - hosts: web remote_user: root vars: http_port: '{{ port }}' #定义额外变量 http_name: Bao tasks: - name: insert firewalld rule for httpd firewalld: port={{ http_port }}/tcp permanent=true state=enabled immediate=yes --- ####引用变量 ansible-playbook a.yml --extra-vars "http_port=8080 http_name=Bao_test" --- ####引用变量(json格式) ansible-playbook a.yml --extra-vars "{'http_port':'8080', 'http_name':'Bao_test'}" --- ####引用变量(参数文件) ansible-playbook a.yml --extra-vars "@vars.json" --- #vars.json文件内容: {'http_port':'8080', 'http_name':'Bao_test'}
分享到: