ansible系列(21)--ansible的变量注册Register

1. 变量注册Register

register 关键字可以将某个 task 任务结果存储至变量中,最后使用 debug模块 输出变量内容,可以用于后续排障;

  • 示例一:register的基本使用:

    #playbook文件:
    [root@xuzhichao ansible_var]# cat test7.yml 
    ---
    - hosts: 192.168.20.23
      remote_user: root
    
      tasks:
        - name: Shell Command
          shell: netstat -ntlp
          register: port_status
    
        - name: Get Port Status
          debug: msg={{ port_status.stdout_lines }}  <==stdout_lines作用是只输出命令的内容。
    
    #运行playbook:
    [root@xuzhichao ansible_var]# ansible-playbook test7.yml 
    
    PLAY [192.168.20.23] *****************************************************************************************************************************************
    
    TASK [Gathering Facts] ***************************************************************************************************************************************
    ok: [192.168.20.23]
    
    TASK [Shell Command] *****************************************************************************************************************************************
    changed: [192.168.20.23]
    
    TASK [Get Port Status] ***************************************************************************************************************************************
    ok: [192.168.20.23] => {
        "msg": [
            "Active Internet connections (only servers)", 
            "Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    ", 
            "tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      668/rpcbind         ", 
            "tcp        0      0 0.0.0.0:20048           0.0.0.0:*               LISTEN      15615/rpc.mountd    ", 
            "tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      8981/nginx: master  ", 
            "tcp        0      0 0.0.0.0:40372           0.0.0.0:*               LISTEN      -                   ", 
            "tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1112/sshd           ", 
            "tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1288/master         ", 
            "tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      8981/nginx: master  ", 
            "tcp        0      0 0.0.0.0:2049            0.0.0.0:*               LISTEN      -                   ", 
            "tcp        0      0 0.0.0.0:55651           0.0.0.0:*               LISTEN      1122/rpc.statd      ", 
            "tcp6       0      0 :::111                  :::*                    LISTEN      668/rpcbind         ", 
            "tcp6       0      0 :::40688                :::*                    LISTEN      -                   ", 
            "tcp6       0      0 :::20048                :::*                    LISTEN      15615/rpc.mountd    ", 
            "tcp6       0      0 :::56211                :::*                    LISTEN      1122/rpc.statd      ", 
            "tcp6       0      0 :::22                   :::*                    LISTEN      1112/sshd           ", 
            "tcp6       0      0 ::1:25                  :::*                    LISTEN      1288/master         ", 
            "tcp6       0      0 :::2049                 :::*                    LISTEN      -                   "
        ]
    }
    
    PLAY RECAP ***************************************************************************************************************************************************
    192.168.20.23              : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    
  • 示例二:使用 register 关键字完成 jumpserver key 的创建;

    [root@xuzhichao ansible_var]# cat test8.yaml 
    - hosts: webservers 
      tasks: 
      
      - name: Run Shell Command Random string 
        shell: 
          cmd: 'if ! grep "SECRET_KEY" ~/.bashrc; then 
                  SECRET_KEY=`cat /dev/urandom | tr -dc A-Za-z0-9 | head -c 50`; 
                  echo "SECRET_KEY=$SECRET_KEY" >> ~/.bashrc; 
                  echo $SECRET_KEY; 
                else
                  echo $SECRET_KEY; 
                fi' 
         register: SECRET_KEY 
       
       - name: Run Shell Command BOOTSTRAP_TOKEN 
         shell: 
           cmd: 'if ! grep "BOOTSTRAP_TOKEN" ~/.bashrc; then 
                   BOOTSTRAP_TOKEN=`cat /dev/urandom | tr -dc A-Za- z0-9 | head -c 16`; 
                   echo "BOOTSTRAP_TOKEN=$BOOTSTRAP_TOKEN" >> ~/.bashrc; 
                   echo $BOOTSTRAP_TOKEN; 
                 else
                   echo $BOOTSTRAP_TOKEN; 
                 fi' 
         register: BOOTSTRAP_TOKEN
         
       - name: Copy Jms Configure 
         template: 
           src: ./j-config.yml 
           dest: /tmp/jms_config.yml
         
       - name: Copy Koko Configure 
         template:
           src: ./k-config.yml 
           dest: /tmp/koko_config.yml
           
    # 配置文件中: 
    SECRET_KEY: {{ SECRET_KEY.stdout.split('=')[1] }} 
    BOOTSTRAP_TOKEN: {{ BOOTSTRAP_TOKEN.stdout.split('=')[1] }}       
    
posted @ 2021-08-18 17:19  向往自由的独行者  阅读(1278)  评论(0编辑  收藏  举报