playbook 变量

发布时间: 更新时间: 总字数:1156 阅读时间:3m 作者:IP:上海 网址

ansible playbook Vars 变量

变量

变量名应该由字母、数字、下划线组成,变量名需要以字母开头,且非ansible关键字

playbook 定义变量

  • vars-test1.yaml
---
- hosts: testservers
  vars:
    testvar1: testfile  # 引用 {{ testvar1 }},若在开头位置,需要使用引号 "{{ testvar1 }}"
    # - testvar1: testfile
    # test:
    #   var1: testfile  # 引用 {{ test.var1 }}
  remote_user: root
  tasks:
  - name: create file {{ testvar1 }}
    file:
      path: /tmp/{{ testvar1 }}
      # path={{ testvar1 }}  # 当使用`等号`为模块的参数赋值时,则不用考虑引用变量时是否使用`引号`的问题
      state: touch

变量文件定义

  • vars-test2.yaml
testvar1: testfile
# - testvar1: testfile
test:
  var1: testfile
  • 引用
---
- hosts: testservers
  remote_user: root
  vars_files:
  - vars-test2.yaml
  # - vars-xxx.yaml
  tasks:
  ...
  # 变量引用 {{test.var1}} {{test["var1"]}}

setup 模块

ansible 通过 [Gathering Facts] 收集的信息会保存到对应的变量中,可以通过 setup模块 查看对象的信息

ansible 172.17.0.3 -m setup ...
# 过滤指定的变量
ansible 172.17.0.3 -m setup -a 'filter=ansible_env'
ansible 172.17.0.3 -m setup -a "filter=*mb*"

facts 文件

ansible 默认会去目标主机的 /etc/ansible/facts.d 目录下查找主机中的自定义信息,并且规定,自定义信息需要写在以 .fact 为后缀的文件中

$ cat /etc/ansible/facts.d/info.fact
[testmsg]
msg1=This is the first custom test message
msg2=This is the second custom test message

获取变量

$ ansible 172.17.0.3 -m setup -a "filter=ansible_local"
$ ansible 172.17.0.3 -m setup -a "filter=ansible_local fact_path=/etc/ansible/facts.d"
172.17.0.3 | SUCCESS => {
    "ansible_facts": {
        "ansible_local": {
            "info": {
                "testmsg": {
                    "msg1": "This is the first custom test message",
                    "msg2": "This is the second custom test message"
                }
            }
        },
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false
}
  • 使用 {{testmsg.msg1}} 获取对应的变量值
  • 指定 facts 目录 ansible 172.17.0.3 -m setup -a "fact_path=/testdir"

set_fact 定义变量

  • set_fact 模块可以在 tasks 中创建变量,也可以将一个变量的值赋值给另一个变量

  • set-fact-test1.yaml

---
- hosts: testservers
  remote_user: root
  vars:
    testvar1: test1_string
  tasks:
  - shell: "echo test2_string"
    register: shellreturn
  - set_fact:
      testsv1: "{{testvar1}}"
      testsv2: "{{shellreturn.stdout}}"
  - debug:
      msg: "{{testsv1}} {{testsv2}}"
      #var: shellreturn

- hosts: testservers
  remote_user: root
  tasks:
  - name: other play get testsv1
    debug:
      msg: "{{testsv1}}"
  # - name: other play get testvar1  # 获取失败,没有定义 testvar1
    # debug:
      # msg: "{{testvar1}}"
  • 执行
ansible-playbook -i hosts.test set-fact-test1.yaml
  • 结果
ansible-playbook -i hosts.test set-fact-test1.yaml ...

内置变量

  • 内置变量

    • ansible_version ansible 版本
    • inventory_hostname hosts 中注册的主机名
    • inventory_hostname_short 短主机名,. 之前的值
    • play_hosts 获取到当前 playbook 操作的所有主机的主机名列表
    • groups 获取到清单中 所有分组信息
      • {{groups.<group-name>}}{{groups[group-name]}}
      • {{groups.ungrouped}} 未分组主机
    • group_names 获取到当前主机所在分组的组名
    • inventory_dir 变量获取 ansible 主机中清单文件的存放路径,默认为 /etc/ansible/
  • default-vars-test1.yaml

---
- name: "play 1: Gather facts of h-1"
  hosts: h-1
  remote_user: root
  # gather_facts: no  # 关闭 gather_facts,默认为开启 yes
  tasks:
  - shell: "echo register_var_in_play1"
    register: shellreturn  # 注册变量

- name: "play 2: Get facts of h-1 when operating on h-2"
  hosts: h-2
  remote_user: root
  tasks:
  - debug:
      msg: "{{hostvars['h-1'].ansible_eth0.ipv4}}, {{inventory_hostname}}, {{inventory_hostname_short}}, {{play_hosts}}, {{groups}}, {{groups.testservers}}, {{group_names}}, {{inventory_dir}}"
  - debug:
      msg: "{{hostvars['h-1'].shellreturn.stdout}}"  # 跨节点输出,{{hostvars.h1.shellreturn.stdout}}
  • 运行
ansible all -m debug -a "msg={{ansible_version}}"
ansible-playbook -i hosts.test default-vars-test1.yaml
  • 结果
ansible-playbook -i hosts.test default-vars-test1.yaml ...
本文总阅读量 次 本站总访问量 次 本站总访客数