Ansible 变量注册

playbook 运行的时候,经常需要中途收集一些数据,后面使用它。 使用 register 注册变量是最简单、最常用的一种方式。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
- hosts: all
  tasks:
  - name: define a var1
    shell: "whoami"
    register: whoami

  - name: show var
    debug:
      msg: "whoami: {{ whoami }}"

  - name: show var.stdout
    debug:
      msg: "whoami.stdout: {{ whoami.stdout }}"

执行结果输出

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
PLAY [all] ****************************************************************************************************************

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

TASK [define a var1] ******************************************************************************************************
changed: [192.168.142.20]

TASK [show var] ***********************************************************************************************************
ok: [192.168.142.20] => {
    "msg": "whoami: {'cmd': 'whoami', 'stdout': 'root', 'stderr': '', 'rc': 0, 'start': '2023-08-30 16:02:55.497249', 'end': '2023-08-30 16:02:55.501304', 'delta': '0:00:00.004055', 'changed': True, 'stdout_lines': ['root'], 'stderr_lines': [], 'failed': False}"
}

TASK [show var.stdout] ****************************************************************************************************
ok: [192.168.142.20] => {
    "msg": "whoami.stdout: root"
}

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

注意: ansible 执行结果一般都会返回一个字典类型的数据,你会看到很多你不关心的字段,可以通过指定字典的 key,例如 stdout 或stdout_lines ,只看到你关心的数据。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
- hosts: all
  vars:
    userss: []

  tasks:
    - name: define users in a loop
      shell: "grep {{ item }} /etc/passwd"
      register: users
      with_items:
        - root
        - nginx
    
    - name: show users
      debug:
        msg: "{{ users }}"

    - name: show users.results
      debug:
        msg: "{{ users.results }}"

    - name: define userss by set_fact
      set_fact: 
        userss: "{{userss +item.stdout_lines}}"
      with_list: "{{ users.results }}"

    - name: print userss
      debug:
        msg: "{{ userss }}"
    
    - name: print userss one by one
      debug:
        msg: "{{ item }}"
      with_list: "{{ userss}}"

    - name: print part of userss one by one
      debug:
        msg: "{{ item.split(':')[6] }}"
      with_list: "{{ userss}}"

执行结果输出

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
PLAY [all] ****************************************************************************************************************

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

TASK [define users in a loop] *********************************************************************************************
changed: [192.168.142.20] => (item=root)
changed: [192.168.142.20] => (item=nginx)

TASK [show users] *********************************************************************************************************
ok: [192.168.142.20] => {
    "msg": {
        "changed": true,
        "msg": "All items completed",
        "results": [
            {
                "ansible_loop_var": "item",
                "changed": true,
                "cmd": "grep root /etc/passwd",
                "delta": "0:00:00.003829",
                "end": "2023-08-30 16:05:18.237586",
                "failed": false,
                "invocation": {
                    "module_args": {
                        "_raw_params": "grep root /etc/passwd",
                        "_uses_shell": true,
                        "argv": null,
                        "chdir": null,
                        "creates": null,
                        "executable": null,
                        "removes": null,
                        "stdin": null,
                        "stdin_add_newline": true,
                        "strip_empty_ends": true,
                        "warn": true
                    }
                },
                "item": "root",
                "rc": 0,
                "start": "2023-08-30 16:05:18.233757",
                "stderr": "",
                "stderr_lines": [],
                "stdout": "root:x:0:0:root:/root:/bin/bash\noperator:x:11:0:operator:/root:/sbin/nologin",
                "stdout_lines": [
                    "root:x:0:0:root:/root:/bin/bash",
                    "operator:x:11:0:operator:/root:/sbin/nologin"
                ]
            },
            {
                "ansible_loop_var": "item",
                "changed": true,
                "cmd": "grep nginx /etc/passwd",
                "delta": "0:00:00.005357",
                "end": "2023-08-30 16:05:18.620858",
                "failed": false,
                "invocation": {
                    "module_args": {
                        "_raw_params": "grep nginx /etc/passwd",
                        "_uses_shell": true,
                        "argv": null,
                        "chdir": null,
                        "creates": null,
                        "executable": null,
                        "removes": null,
                        "stdin": null,
                        "stdin_add_newline": true,
                        "strip_empty_ends": true,
                        "warn": true
                    }
                },
                "item": "nginx",
                "rc": 0,
                "start": "2023-08-30 16:05:18.615501",
                "stderr": "",
                "stderr_lines": [],
                "stdout": "nginx:x:1000:1001::/home/nginx:/bin/bash",
                "stdout_lines": [
                    "nginx:x:1000:1001::/home/nginx:/bin/bash"
                ]
            }
        ]
    }
}

TASK [show users.results] *************************************************************************************************
ok: [192.168.142.20] => {
    "msg": [
        {
            "ansible_loop_var": "item",
            "changed": true,
            "cmd": "grep root /etc/passwd",
            "delta": "0:00:00.003829",
            "end": "2023-08-30 16:05:18.237586",
            "failed": false,
            "invocation": {
                "module_args": {
                    "_raw_params": "grep root /etc/passwd",
                    "_uses_shell": true,
                    "argv": null,
                    "chdir": null,
                    "creates": null,
                    "executable": null,
                    "removes": null,
                    "stdin": null,
                    "stdin_add_newline": true,
                    "strip_empty_ends": true,
                    "warn": true
                }
            },
            "item": "root",
            "rc": 0,
            "start": "2023-08-30 16:05:18.233757",
            "stderr": "",
            "stderr_lines": [],
            "stdout": "root:x:0:0:root:/root:/bin/bash\noperator:x:11:0:operator:/root:/sbin/nologin",
            "stdout_lines": [
                "root:x:0:0:root:/root:/bin/bash",
                "operator:x:11:0:operator:/root:/sbin/nologin"
            ]
        },
        {
            "ansible_loop_var": "item",
            "changed": true,
            "cmd": "grep nginx /etc/passwd",
            "delta": "0:00:00.005357",
            "end": "2023-08-30 16:05:18.620858",
            "failed": false,
            "invocation": {
                "module_args": {
                    "_raw_params": "grep nginx /etc/passwd",
                    "_uses_shell": true,
                    "argv": null,
                    "chdir": null,
                    "creates": null,
                    "executable": null,
                    "removes": null,
                    "stdin": null,
                    "stdin_add_newline": true,
                    "strip_empty_ends": true,
                    "warn": true
                }
            },
            "item": "nginx",
            "rc": 0,
            "start": "2023-08-30 16:05:18.615501",
            "stderr": "",
            "stderr_lines": [],
            "stdout": "nginx:x:1000:1001::/home/nginx:/bin/bash",
            "stdout_lines": [
                "nginx:x:1000:1001::/home/nginx:/bin/bash"
            ]
        }
    ]
}

TASK [define userss by set_fact] ******************************************************************************************
ok: [192.168.142.20] => (item={'cmd': 'grep root /etc/passwd', 'stdout': 'root:x:0:0:root:/root:/bin/bash\noperator:x:11:0:operator:/root:/sbin/nologin', 'stderr': '', 'rc': 0, 'start': '2023-08-30 16:05:18.233757', 'end': '2023-08-30 16:05:18.237586', 'delta': '0:00:00.003829', 'changed': True, 'invocation': {'module_args': {'_raw_params': 'grep root /etc/passwd', '_uses_shell': True, 'warn': True, 'stdin_add_newline': True, 'strip_empty_ends': True, 'argv': None, 'chdir': None, 'executable': None, 'creates': None, 'removes': None, 'stdin': None}}, 'stdout_lines': ['root:x:0:0:root:/root:/bin/bash', 'operator:x:11:0:operator:/root:/sbin/nologin'], 'stderr_lines': [], 'failed': False, 'item': 'root', 'ansible_loop_var': 'item'})
ok: [192.168.142.20] => (item={'cmd': 'grep nginx /etc/passwd', 'stdout': 'nginx:x:1000:1001::/home/nginx:/bin/bash', 'stderr': '', 'rc': 0, 'start': '2023-08-30 16:05:18.615501', 'end': '2023-08-30 16:05:18.620858', 'delta': '0:00:00.005357', 'changed': True, 'invocation': {'module_args': {'_raw_params': 'grep nginx /etc/passwd', '_uses_shell': True, 'warn': True, 'stdin_add_newline': True, 'strip_empty_ends': True, 'argv': None, 'chdir': None, 'executable': None, 'creates': None, 'removes': None, 'stdin': None}}, 'stdout_lines': ['nginx:x:1000:1001::/home/nginx:/bin/bash'], 'stderr_lines': [], 'failed': False, 'item': 'nginx', 'ansible_loop_var': 'item'})

TASK [print userss] *******************************************************************************************************
ok: [192.168.142.20] => {
    "msg": [
        "root:x:0:0:root:/root:/bin/bash",
        "operator:x:11:0:operator:/root:/sbin/nologin",
        "nginx:x:1000:1001::/home/nginx:/bin/bash"
    ]
}

TASK [print userss one by one] ********************************************************************************************
ok: [192.168.142.20] => (item=root:x:0:0:root:/root:/bin/bash) => {
    "msg": "root:x:0:0:root:/root:/bin/bash"
}
ok: [192.168.142.20] => (item=operator:x:11:0:operator:/root:/sbin/nologin) => {
    "msg": "operator:x:11:0:operator:/root:/sbin/nologin"
}
ok: [192.168.142.20] => (item=nginx:x:1000:1001::/home/nginx:/bin/bash) => {
    "msg": "nginx:x:1000:1001::/home/nginx:/bin/bash"
}

TASK [print part of userss one by one] ************************************************************************************
ok: [192.168.142.20] => (item=root:x:0:0:root:/root:/bin/bash) => {
    "msg": "/bin/bash"
}
ok: [192.168.142.20] => (item=operator:x:11:0:operator:/root:/sbin/nologin) => {
    "msg": "/sbin/nologin"
}
ok: [192.168.142.20] => (item=nginx:x:1000:1001::/home/nginx:/bin/bash) => {
    "msg": "/bin/bash"
}

PLAY RECAP ****************************************************************************************************************
192.168.142.20             : ok=8    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

playbook

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
- hosts: all
  tasks:
    - name: define a var1
      shell: "whoami"
      register: whoami

    - debug:
        msg: "whoami: {{ whoami }}"

    - debug:
        msg: "whoami.stdout: {{whoami.stdout}}"

    - name: define a var by set_fact
      set_fact:
         whoami: "{{ whoami.stdout  }}"

    - name:  show a var after defined by set_fact
      debug:
         msg: "{{ whoami }}"

结果输出

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
PLAY [all] ****************************************************************************************************************

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

TASK [define a var1] ******************************************************************************************************
changed: [192.168.142.20]

TASK [debug] **************************************************************************************************************
ok: [192.168.142.20] => {
    "msg": "whoami: {'cmd': 'whoami', 'stdout': 'root', 'stderr': '', 'rc': 0, 'start': '2023-08-30 16:08:28.677690', 'end': '2023-08-30 16:08:28.682671', 'delta': '0:00:00.004981', 'changed': True, 'stdout_lines': ['root'], 'stderr_lines': [], 'failed': False}"
}

TASK [debug] **************************************************************************************************************
ok: [192.168.142.20] => {
    "msg": "whoami.stdout: root"
}

TASK [define a var by set_fact] *******************************************************************************************
ok: [192.168.142.20]

TASK [show a var after defined by set_fact] *******************************************************************************
ok: [192.168.142.20] => {
    "msg": "root"
}

PLAY RECAP ****************************************************************************************************************
192.168.142.20             : ok=6    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

来源: https://blog.51cto.com/byygyy/3858303