register 说明
在 Ansible 中,register 是一个关键字,用于捕获任务执行结果以及任务执行过程中产生的输出和值。使用 register 关键字可以将任务的执行结果存储到一个变量中,您可以在 playbook 后续的任务中使用这个变量。
register 返回值类型
使用register关键字时,Ansible会将任务的输出存储在指定的变量中。这个变量是一个字典(dictionary),其中包括各种信息:
stdout:任务的标准输出
stderr:任务的标准错误输出
rc: 任务的退出码,通常0表示成功,非0表示失败
failed:一个布尔值,指示任务是否执行失败
changed:一个布尔值,指示是否更改了目标系统的状态
ansible_facts:包含由Ansible模块生成的Tasible模块的事实信息
stdout_lines: 存储了任务的标准输出,并以行为单位组成的列表形式提供访问。
"bash_users": {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"cmd": "awk -F: '$7 == \"/bin/bash\" {print $1}' /etc/passwd\n",
"delta": "0:00:00.004821",
"end": "2024-04-25 01:49:26.473014",
"failed": false,
"rc": 0,
"start": "2024-04-25 01:49:26.468193",
"stderr": "",
"stderr_lines": [],
"stdout": "root\nyunwei",
"stdout_lines": [
"root",
"yunwei"
]
}
register 示例
hosts
[centos]
192.168.174.131 ansible_ssh_port=22
Ansible Vault 文件
创建 Ansible Vault 文件
# ansible-vault create passwords.yml
New Vault password: # 12345678
Confirm New Vault password:
编辑 Ansible Vault 文件
# ansible-vault edit passwords.yml
Vault password:
passwords.yml
hosts_passwords:
192.168.174.131:
yunwei_password: yunwei_131
示例 一
register_test.yaml
- hosts: 192.168.174.131
remote_user: yunwei
become: yes
become_method: sudo
gather_facts: no
vars_files:
- passwords.yml
vars:
ansible_ssh_pass: "{{ hosts_passwords[inventory_hostname].yunwei_password }}"
ansible_become_pass: "{{ hosts_passwords[inventory_hostname].yunwei_password }}"
tasks:
- name: Get users with /bin/bash shell excluding specified users
ansible.builtin.shell:
cmd: |
awk -F: '$7 == "/bin/bash" {print $1}' /etc/passwd
register: bash_users
changed_when: false
- name: Display users
ansible.builtin.debug:
var: bash_users
# msg: "{{ bash_users }}"
测试 playbook
# ansible-playbook -i hosts register_test.yaml --ask-vault-pass
Vault password:
PLAY [192.168.174.131] ********************************************************************************************************************************************************
TASK [Get users with /bin/bash shell excluding specified users] ***************************************************************************************************************
ok: [192.168.174.131]
TASK [Display users] *********************************************************************************************************************************************************
ok: [192.168.174.131] => {
"bash_users": {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"cmd": "awk -F: '$7 == \"/bin/bash\" {print $1}' /etc/passwd\n",
"delta": "0:00:00.004821",
"end": "2024-04-25 01:49:26.473014",
"failed": false,
"rc": 0,
"start": "2024-04-25 01:49:26.468193",
"stderr": "",
"stderr_lines": [],
"stdout": "root\nyunwei",
"stdout_lines": [
"root",
"yunwei"
]
}
}
PLAY RECAP ********************************************************************************************************************************************************************
192.168.174.131 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
示例 二
register_test-2.yaml
- hosts: 192.168.174.131
remote_user: yunwei
become: yes
become_method: sudo
gather_facts: no
vars_files:
- passwords.yml
vars:
ansible_ssh_pass: "{{ hosts_passwords[inventory_hostname].yunwei_password }}"
ansible_become_pass: "{{ hosts_passwords[inventory_hostname].yunwei_password }}"
tasks:
- name: Get users with /bin/bash shell excluding specified users
ansible.builtin.shell:
cmd: |
awk -F: '$7 == "/bin/bash" {print $1}' /etc/passwd
register: bash_users
changed_when: false
- name: Check user
ansible.builtin.shell:
cmd: |
echo "{{ item }}"
loop: "{{ bash_users.stdout_lines }}"
register: list_users
- name: Display users
ansible.builtin.debug:
var: list_users
测试 playbook
# ansible-playbook -i hosts register_test-2.yaml --ask-vault-pass
Vault password:
PLAY [192.168.174.131] ********************************************************************************************************************************************************
TASK [Get users with /bin/bash shell excluding specified users] ***************************************************************************************************************
ok: [192.168.174.131]
TASK [Check user] ************************************************************************************************************************************************************
changed: [192.168.174.131] => (item=root)
changed: [192.168.174.131] => (item=yunwei)
TASK [Display users] *********************************************************************************************************************************************************
ok: [192.168.174.131] => {
"list_users": {
"changed": true,
"msg": "All items completed",
"results": [
{
"ansible_loop_var": "item",
"changed": true,
"cmd": "echo \"root\"\n",
"delta": "0:00:00.003384",
"end": "2024-04-25 02:29:06.758282",
"failed": false,
"invocation": {
"module_args": {
"_raw_params": "echo \"root\"\n",
"_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": "2024-04-25 02:29:06.754898",
"stderr": "",
"stderr_lines": [],
"stdout": "root",
"stdout_lines": [
"root"
]
},
{
"ansible_loop_var": "item",
"changed": true,
"cmd": "echo \"yunwei\"\n",
"delta": "0:00:00.003156",
"end": "2024-04-25 02:29:07.081986",
"failed": false,
"invocation": {
"module_args": {
"_raw_params": "echo \"yunwei\"\n",
"_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": "yunwei",
"rc": 0,
"start": "2024-04-25 02:29:07.078830",
"stderr": "",
"stderr_lines": [],
"stdout": "yunwei",
"stdout_lines": [
"yunwei"
]
}
]
}
}
PLAY RECAP ********************************************************************************************************************************************************************
192.168.174.131 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
示例 三
register_test-3.yaml
- hosts: 192.168.174.131
remote_user: yunwei
become: yes
become_method: sudo
gather_facts: no
vars_files:
- passwords.yml
vars:
ansible_ssh_pass: "{{ hosts_passwords[inventory_hostname].yunwei_password }}"
ansible_become_pass: "{{ hosts_passwords[inventory_hostname].yunwei_password }}"
tasks:
- name: Get users with /bin/bash shell excluding specified users
ansible.builtin.shell:
cmd: |
awk -F: '$7 == "/bin/bash" {print $1}' /etc/passwd
register: bash_users
changed_when: false
- name: Check user
ansible.builtin.shell:
cmd: |
echo "{{ item }}"
loop: "{{ bash_users.stdout_lines }}"
register: list_users
- name: Display users
ansible.builtin.debug:
msg: "{{ list_users }}"
- name: print user
ansible.builtin.debug:
msg: "user info: { list_users.results | map(attribute='item') | list }}"
测试 playbook
# ansible-playbook -i hosts register_test-3.yaml --ask-vault-pass
Vault password:
PLAY [192.168.174.131] ********************************************************************************************************************************************************
TASK [Get users with /bin/bash shell excluding specified users] ***************************************************************************************************************
ok: [192.168.174.131]
TASK [Check user] ************************************************************************************************************************************************************
changed: [192.168.174.131] => (item=root)
changed: [192.168.174.131] => (item=yunwei)
TASK [Display users] *********************************************************************************************************************************************************
ok: [192.168.174.131] => {
"msg": {
"changed": true,
"msg": "All items completed",
"results": [
{
"ansible_loop_var": "item",
"changed": true,
"cmd": "echo \"root\"\n",
"delta": "0:00:00.003570",
"end": "2024-04-25 02:40:58.620563",
"failed": false,
"invocation": {
"module_args": {
"_raw_params": "echo \"root\"\n",
"_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": "2024-04-25 02:40:58.616993",
"stderr": "",
"stderr_lines": [],
"stdout": "root",
"stdout_lines": [
"root"
]
},
{
"ansible_loop_var": "item",
"changed": true,
"cmd": "echo \"yunwei\"\n",
"delta": "0:00:00.004940",
"end": "2024-04-25 02:40:58.974209",
"failed": false,
"invocation": {
"module_args": {
"_raw_params": "echo \"yunwei\"\n",
"_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": "yunwei",
"rc": 0,
"start": "2024-04-25 02:40:58.969269",
"stderr": "",
"stderr_lines": [],
"stdout": "yunwei",
"stdout_lines": [
"yunwei"
]
}
]
}
}
TASK [print user] *************************************************************************************************************************************************************
ok: [192.168.174.131] => {
"msg": [
"root",
"yunwei"
]
}
PLAY RECAP ********************************************************************************************************************************************************************
192.168.174.131 : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
标签:yunwei,users,register,192.168,ansible,174.131,null
From: https://www.cnblogs.com/wangguishe/p/18156019