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
172.17.0.3 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "172.17.0.3"
        ],
        ...
        "ansible_default_ipv4": {
            "address": "172.17.0.3",
            "alias": "eth0",
            "broadcast": "172.17.255.255",
            "gateway": "172.17.0.1",
            "interface": "eth0",
            "macaddress": "02:42:ac:11:00:03",
            "mtu": 65535,
            "netmask": "255.255.0.0",
            "network": "172.17.0.0",
            "prefix": "16",
            "type": "ether"
        },
        ...
        "ansible_distribution": "Ubuntu",
        "ansible_distribution_file_parsed": true,
        "ansible_distribution_file_path": "/etc/os-release",
        "ansible_distribution_file_variety": "Debian",
        "ansible_distribution_major_version": "20",
        "ansible_distribution_release": "focal",
        "ansible_distribution_version": "20.04",
        "ansible_dns": {
            "nameservers": [
                "192.168.65.7"
            ]
        },
        ...
# 过滤指定的变量
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-playbook -i hosts.test set-fact-test1.yaml

PLAY [testservers] ***************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************
ok: [h-3]
ok: [h-1]
ok: [h-2]

TASK [shell] *********************************************************************************************************
changed: [h-3]
changed: [h-2]
changed: [h-1]

TASK [set_fact] ******************************************************************************************************
ok: [h-1]
ok: [h-2]
ok: [h-3]

TASK [debug] *********************************************************************************************************
ok: [h-1] => {
    "msg": "test1_string test2_string"
}
ok: [h-2] => {
    "msg": "test1_string test2_string"
}
ok: [h-3] => {
    "msg": "test1_string test2_string"
}

PLAY [testservers] ***************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************
ok: [h-1]
ok: [h-2]
ok: [h-3]

TASK [other play get testsv1] ****************************************************************************************
ok: [h-1] => {
    "msg": "test1_string"
}
ok: [h-2] => {
    "msg": "test1_string"
}
ok: [h-3] => {
    "msg": "test1_string"
}

PLAY RECAP ***********************************************************************************************************
h-1                        : ok=6    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
h-2                        : ok=6    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
h-3                        : ok=6    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

内置变量

  • 内置变量

    • 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 ...
$ ansible-playbook -i hosts.test default-vars-test1.yaml

PLAY [play 1: Gather facts of h-1] ***********************************************************************************

TASK [Gathering Facts] ***********************************************************************************************
ok: [h-1]

TASK [shell] *********************************************************************************************************
changed: [h-1]

PLAY [play 2: Get facts of h-1 when operating on testB] **************************************************************

TASK [Gathering Facts] ***********************************************************************************************
ok: [h-2]

TASK [debug] *********************************************************************************************************
ok: [h-2] => {
    "msg": "{'address': '172.17.0.3', 'broadcast': '172.17.255.255', 'netmask': '255.255.0.0', 'network': '172.17.0.0', 'prefix': '16'}, h-2, h-2, ['h-2'], {'all': ['h-1', 'h-2', 'h-3'], 'ungrouped': [], 'testservers': ['h-1', 'h-2', 'h-3']}, ['h-1', 'h-2', 'h-3'], ['testservers'], /root"
}

TASK [debug] *********************************************************************************************************
ok: [h-2] => {
    "msg": "register_var_in_play1"
}

PLAY RECAP ***********************************************************************************************************
h-1                        : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
h-2                        : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数