ansible-playbook之变量引用

1 执行结果传入变量register的使用

[root@ansible ansible]# cat register.yaml
--- 
- hosts: test
  remote_user: root
  tasks:
    - name: test shell
      shell: |
         echo "nihao"
      register: testvars
    - name: ck vars
      debug: var="testvars.stdout"
结果:
[root@ansible ansible]# ansible-playbook register.yaml 

PLAY [test] *******************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [192.168.10.20]
ok: [192.168.10.21]

TASK [test shell] *************************************************************************************************************************************************************************************************
changed: [192.168.10.20]
changed: [192.168.10.21]

TASK [ck vars] ****************************************************************************************************************************************************************************************************
ok: [192.168.10.21] => {
    "testvars.stdout": "nihao"
}
ok: [192.168.10.20] => {
    "testvars.stdout": "nihao"
}

PLAY RECAP ********************************************************************************************************************************************************************************************************
192.168.10.20              : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.10.21              : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

2 交互式写入变量prompt

[root@ansible ansible]# cat prompt.yaml
--- 
- hosts: test
  remote_user: root
  vars_prompt:
   - name: "your_name"
     prompt: "what is your  name"
   - name: "your_age"
     prompt: "how  old are you?"
  tasks:
    - name: output vars
      debug: msg="your name is {{your_name}} age {{your_age}}"

执行结果:
[root@ansible ansible]# ansible-playbook prompt.yaml 
what is your  name: 
how  old are you?: 

PLAY [test] *******************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [192.168.10.21]
ok: [192.168.10.20]

TASK [output vars] ************************************************************************************************************************************************************************************************
ok: [192.168.10.21] => {
    "msg": "your name is suixiaofeng age 28"
}
ok: [192.168.10.20] => {
    "msg": "your name is suixiaofeng age 28"
}

PLAY RECAP ********************************************************************************************************************************************************************************************************
192.168.10.20              : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.10.21              : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

上面输入的内容在屏幕上不显示,private: no(会把输入内容在屏幕上显示出来)      default: (默认值)
[root@ansible ansible]# cat prompt.yaml 
--- 
- hosts: test
  remote_user: root
  vars_prompt:
   - name: "your_name"
     prompt: "what is your  name"
     private: no
   - name: "your_age"
     prompt: "how  old are you?"
     private: no
  tasks:
    - name: output vars
      debug: msg="your name is {{your_name}} age {{your_age}}"

[root@ansible ansible]# ansible-playbook prompt.yaml 
what is your  name: suixiaofeng
how  old are you?: 28

PLAY [test] *******************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [192.168.10.21]
ok: [192.168.10.20]

TASK [output vars] ************************************************************************************************************************************************************************************************
ok: [192.168.10.21] => {
    "msg": "your name is suixiaofeng age 28"
}
ok: [192.168.10.20] => {
    "msg": "your name is suixiaofeng age 28"
}

PLAY RECAP ********************************************************************************************************************************************************************************************************
192.168.10.20              : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.10.21              : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

选择一个选项,不选的话,默认是A,(default)
[root@ansible ansible]# cat prompt_choise.yaml
---
- hosts: test
  remote_user: root
  vars_prompt:
     - name: "solution"
       prompt: "choose the solution you want \n
               A: solutionA\n
               B: soulutionB\n
               C: soulutionC\n
               D: soulytionD \n"
       private: no
       default: A
  tasks:
     - name: output vars
       debug: msg="your choose is {{solution}}"

[root@ansible ansible]# ansible-playbook prompt_choise.yaml 
choose the solution you want 
 A: solutionA
 B: soulutionB
 C: soulutionC
 D: soulytionD 
 [A]:  

PLAY [test] *******************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [192.168.10.21]
ok: [192.168.10.20]

TASK [output vars] ************************************************************************************************************************************************************************************************
ok: [192.168.10.21] => {
    "msg": "your choose is A"
}
ok: [192.168.10.20] => {
    "msg": "your choose is A"
}

PLAY RECAP ********************************************************************************************************************************************************************************************************
192.168.10.20              : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.10.21              : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

选择B选项

[root@ansible ansible]# ansible-playbook prompt_choise.yaml 
choose the solution you want 
 A: solutionA
 B: soulutionB
 C: soulutionC
 D: soulytionD 
 [A]: B

PLAY [test] *******************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [192.168.10.21]
ok: [192.168.10.20]

TASK [output vars] ************************************************************************************************************************************************************************************************
ok: [192.168.10.21] => {
    "msg": "your choose is B"
}
ok: [192.168.10.20] => {
    "msg": "your choose is B"
}

PLAY RECAP ********************************************************************************************************************************************************************************************************
192.168.10.20              : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.10.21              : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

生产实例:
交互式创建批量用户
[root@ansible ansible]# cat adduser.yaml 
--- 
- hosts: test
  remote_user: root
  vars_prompt:
       - name: "usename"
         prompt: "please enter your create user"
         private: no
       - name: "password"
         prompt: "please enter your password"
         encrypt: "sha512_crypt" 
  tasks: 
       - name: create user
         user: name="{{usename}}" password="{{password}}"      

[root@ansible ansible]# ansible-playbook adduser.yaml 
please enter your create user: test3 
please enter your password: 

PLAY [test] *******************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [192.168.10.21]
ok: [192.168.10.20]

TASK [create user] ************************************************************************************************************************************************************************************************
changed: [192.168.10.20]
changed: [192.168.10.21]

PLAY RECAP ********************************************************************************************************************************************************************************************************
192.168.10.20              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.10.21              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@ansible ansible]# ssh test3@192.168.10.20 
test3@192.168.10.20's password: 
[test3@gitlab ~]$ 

确认输入的密码是否正确( confirm: yes)
[root@ansible ansible]# cat adduser.yaml 
--- 
- hosts: test
  remote_user: root
  vars_prompt:
       - name: "usename"
         prompt: "please enter your create user"
         private: no
       - name: "password"
         prompt: "please enter your password"
         encrypt: "sha512_crypt" 
         confirm: yes
  tasks: 
       - name: create user
         user: name="{{usename}}" password="{{password}}" 

[root@ansible ansible]# ansible-playbook adduser.yaml 
please enter your create user: test3
please enter your password: 
confirm please enter your password: 

PLAY [test] *******************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [192.168.10.21]
ok: [192.168.10.20]

TASK [create user] ************************************************************************************************************************************************************************************************
changed: [192.168.10.20]
changed: [192.168.10.21]

PLAY RECAP ********************************************************************************************************************************************************************************************************
192.168.10.20              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.10.21              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

3 通过命令行传入变量

[root@ansible ansible]# cat command_var.yaml
---
- hosts: test
  remote_user: root
  tasks:
     - name: test command 
       debug: msg="{{command_var}}"

[root@ansible ansible]# ansible-playbook  --extra-vars  "command_var=ansible_0817" command_var.yaml 
PLAY [test] *******************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [192.168.10.21]
ok: [192.168.10.20]

TASK [test command] ***********************************************************************************************************************************************************************************************
ok: [192.168.10.21] => {
    "msg": "ansible_0817"
}
ok: [192.168.10.20] => {
    "msg": "ansible_0817"
}

PLAY RECAP ********************************************************************************************************************************************************************************************************
192.168.10.20              : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.10.21              : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

命令行可以输入多个变量,yaml文件里得有引用才行
[root@ansible ansible]# cat command_var.yaml 
---
- hosts: test
  remote_user: root
  tasks:
     - name: test command 
       debug: msg="{{command_var}},{{two_var}}"

[root@ansible ansible]# ansible-playbook -e 'command_var="ansible_0807" two_var="thsiis two"' command_var.yaml 

PLAY [test] *******************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [192.168.10.21]
ok: [192.168.10.20]

TASK [test command] ***********************************************************************************************************************************************************************************************
ok: [192.168.10.21] => {
    "msg": "ansible_0807,thsiis two"
}
ok: [192.168.10.20] => {
    "msg": "ansible_0807,thsiis two"
}

PLAY RECAP ********************************************************************************************************************************************************************************************************
192.168.10.20              : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.10.21              : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

ansible还支持通过json的格式传入变量
[root@ansible ansible]# ansible-playbook command_var.yaml  -e '{"command_var":"ansible_0708","two_var":"thsiistwo"}'

PLAY [test] *******************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [192.168.10.21]
ok: [192.168.10.20]

TASK [test command] ***********************************************************************************************************************************************************************************************
ok: [192.168.10.21] => {
    "msg": "ansible_0708,thsiistwo"
}
ok: [192.168.10.20] => {
    "msg": "ansible_0708,thsiistwo"
}

PLAY RECAP ********************************************************************************************************************************************************************************************************
192.168.10.20              : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.10.21              : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

4 set_fact定义变量

在task里加变量
[root@ansible ansible]# cat set_fact.yaml 
---
- hosts: test
  remote_user: root
  tasks:  
    - set_fact:
           testvar: "testtets"
    - debug:
         msg: "{{testvar}}" 

[root@ansible ansible]# ansible-playbook set_fact.yaml 

PLAY [test] *******************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [192.168.10.20]
ok: [192.168.10.21]

TASK [set_fact] ***************************************************************************************************************************************************************************************************
ok: [192.168.10.21]
ok: [192.168.10.20]

TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [192.168.10.21] => {
    "msg": "testtets"
}
ok: [192.168.10.20] => {
    "msg": "testtets"
}

PLAY RECAP ********************************************************************************************************************************************************************************************************
192.168.10.20              : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.10.21              : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

可以将一个变量的值赋值给另一个变量
[root@ansible ansible]# cat set_fact2.yaml 
--- 
- hosts: test
  remote_user: root
  vars:
     var01: var01test
  tasks:
    - shell: "echo var02test02"
      register: shellreture
    - set_fact: 
          test01: "{{var01}}"
          test02: "{{shellreture.stdout}}"
    - debug:
         msg: "{{test01}} {{test02}}"  

         [root@ansible ansible]# ansible-playbook set_fact2.yaml 

PLAY [test] *******************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [192.168.10.21]
ok: [192.168.10.20]

TASK  ******************************************************************************************************************************************************************************************************
changed: [192.168.10.20]
changed: [192.168.10.21]

TASK [set_fact] ***************************************************************************************************************************************************************************************************
ok: [192.168.10.21]
ok: [192.168.10.20]

TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [192.168.10.21] => {
    "msg": "var01test var02test02"
}
ok: [192.168.10.20] => {
    "msg": "var01test var02test02"
}

PLAY RECAP ********************************************************************************************************************************************************************************************************
192.168.10.20              : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.10.21              : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

5 内置变量ansible_version

通过ansible_version获取ansible版本号
[root@ansible ansible]# ansible test -m debug -a "msg={{ansible_version.full}}"
192.168.10.21 | SUCCESS => {
    "msg": "2.9.10"
}

6 内置变量hostvars

1.第二个play引用第一个play的主机变量。
[root@ansible ansible]# cat var_hosts.yaml 
---
- name: "test vars"
  hosts: test1 
  remote_user: root

- name: "get fact of test"
  hosts: utest
  remote_user: root
  tasks:
    - debug: msg="{{hostvars.test1.ansible_hostname}}" 

[root@ansible ansible]# ansible-playbook var_hosts.yaml 

PLAY [test vars] **************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [test1]

PLAY [get fact of test] *******************************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [192.168.10.228]

TASK [debug] ******************************************************************************************************************************************************************************************************
ok: [192.168.10.228] => {
    "msg": "jenkins"
}

PLAY RECAP ********************************************************************************************************************************************************************************************************
192.168.10.228             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
test1                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

也可以用"set_fact"结合"hostvars"的方式,实现了跨play获取其他主机中的变量信息的功能。

7 内置变量inventory_hostname

通过inventory_hostname变量可以获取到被操作的当前主机的主机名称,这里所说的主机名称并不是linux系统的主机名,而是对应主机在清单中配置的名称
[root@ansible ansible]# ansible te -m debug -a "msg={{inventory_hostname}}"
te1.devopstack.com | SUCCESS => {
    "msg": "te1.devopstack.com"
}
te2.devopstack.com | SUCCESS => {
    "msg": "te2.devopstack.com"
}

8 内置变量inventory_hostname_short

比inventory_hostname简短
[root@ansible ansible]# ansible te -m debug -a "msg={{inventory_hostname_short}}"
te1.devopstack.com | SUCCESS => {
    "msg": "te1"
}
te2.devopstack.com | SUCCESS => {
    "msg": "te2"
}

9 内置变量play_hosts

获取到当前play所操作的所有主机的主机名列表
[root@ansible ansible]# ansible te -m debug -a "msg={{play_hosts}}"
te1.devopstack.com | SUCCESS => {
    "msg": [
        "te1.devopstack.com", 
        "te2.devopstack.com"
    ]
}
te2.devopstack.com | SUCCESS => {
    "msg": [
        "te1.devopstack.com", 
        "te2.devopstack.com"
    ]
}

10 内置变量groups

获取所有的组的信息
}
[root@ansible ansible]# ansible te1.devopstack.com -m  debug -a "msg={{groups}}"
te1.devopstack.com | SUCCESS => {
    "msg": {
        "all": [
            "192.168.10.228", 
            "te1.devopstack.com", 
            "te2.devopstack.com", 
            "192.168.10.21", 
            "192.168.10.20"
        ], 
        "te": [
            "te1.devopstack.com", 
            "te2.devopstack.com"
        ], 
        "test": [
            "192.168.10.21", 
            "192.168.10.20"
        ], 
        "testall": [
            "te1.devopstack.com", 
            "te2.devopstack.com", 
            "192.168.10.21", 
            "192.168.10.20"
        ], 
        "ungrouped": [], 
        "utest": [
            "192.168.10.228"
        ]
    }
}

获取某一个组的信息
[root@ansible ansible]# ansible te1.devopstack.com -m  debug -a "msg={{groups.te}}"
te1.devopstack.com | SUCCESS => {
    "msg": [
        "te1.devopstack.com", 
        "te2.devopstack.com"
    ]
}

[root@ansible ansible]# ansible te1.devopstack.com -m  debug -a "msg={{groups['te']}}"
te1.devopstack.com | SUCCESS => {
    "msg": [
        "te1.devopstack.com", 
        "te2.devopstack.com"
    ]
}

11 内置变量group_names

获取主机所在组的组名
[root@ansible ansible]# ansible te1.devopstack.com -m debug -a "msg={{group_names}}"
te1.devopstack.com | SUCCESS => {
    "msg": [
        "te", 
        "testall"
    ]
}

12 内置变量inventory_dir

获取ansible主机中清单文件的存放路径。
[root@ansible ansible]# ansible te1.devopstack.com -m debug -a "msg={{inventory_dir}}"
te1.devopstack.com | SUCCESS => {
    "msg": "/etc/ansible"
}

13 加载文件或者目录的变量include_vars

  tasks:
  - include_vars:
      dir: /testdir/ansible/test/
      ignore_files: ["^var_.*",varintest.yaml]
      name: trans_var
  - debug:
      msg: "{{trans_var}}"

    以"var_"开头的变量文件和varintest.yaml变量文件将不会被加载, 'files_matching'参数和'ignore_files'参数能够同时使用,当它们同时出现时,会先找出正则匹配到的文件,然后从中排除那些需要忽略的文件

  tasks:
  - include_vars:
      dir: /testdir/ansible/test/
      files_matching: "^var_.*"
      name: trans_var
  - debug:
      msg: "{{trans_var}}"
使用'files_matching'参数可以指定正则表达式,当指定目录中的文件名称符合正则时,则可以被加载

  tasks:
  - include_vars:
      dir: /testdir/ansible/test/
      depth: 1
      name: trans_var
  - debug:
      msg: "{{trans_var}}"
depth的值为1表示递归深度为1,默认值为0,表示递归到最底层的子目录。

14 主机变量和组变量

主机变量这样定义
[te]
te1.devopstack.com ansible_host=192.168.10.21 testvar1=1021
te2.devopstack.com ansible_host=192.168.10.20

[te:vars]
testvar=testGroupVar

使用"group_vars"目录引入组变量
我在hosts文件的同级目录中创建了一个group_vars目录,并且在group_vars目录中创建了一个名为test的文件,test文件的内容如下

[root@ansible ansible]# cat group_vars/te 
testvar2: testGroupVar1

"group_vars"目录中的文件之所以命名为"te",是因为在上例的hosts文件中,有一个名为"te"的组,这个"变量文件的文件名"与"hosts文件中的组名"其实是对应的,定义在group_vars/te文件中的变量就相当于定义在test组中的"组变量",同理,如果hosts文件中的其他组需要定义"组变量",也可以在"group_vars"目录中创建一个与其组名同名的变量文件

执行
[root@ansible ansible]# ansible-playbook zhu_group.yaml 

PLAY [te1.devopstack.com] ************************************************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************************************************
ok: [te1.devopstack.com]

TASK [debug] *************************************************************************************************************************************************************************************************
ok: [te1.devopstack.com] => {
    "msg": 1021
}

TASK [debug] *************************************************************************************************************************************************************************************************
ok: [te1.devopstack.com] => {
    "msg": "testGroupVar"
}

TASK [debug] *************************************************************************************************************************************************************************************************
ok: [te1.devopstack.com] => {
    "msg": "testGroupVar1"
}

当组变量和主机变量重复的时候,组变量优先级高
如下
[root@ansible ansible]# cat group_vars/te 
testvar: testvar in group_vars/te
testvar2: testGroupVar1

执行结果
[root@ansible ansible]# ansible-playbook zhu_group.yaml 

PLAY [te1.devopstack.com] ************************************************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************************************************
ok: [te1.devopstack.com]

TASK [debug] *************************************************************************************************************************************************************************************************
ok: [te1.devopstack.com] => {
    "msg": 1021
}

TASK [debug] *************************************************************************************************************************************************************************************************
ok: [te1.devopstack.com] => {
    "msg": "testvar in group_vars/te"
}

TASK [debug] *************************************************************************************************************************************************************************************************
ok: [te1.devopstack.com] => {
    "msg": "testGroupVar1"
}

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin
avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: