首页 > 其他分享 >ansible register

ansible register

时间:2024-04-27 10:12:09浏览次数:17  
标签:yunwei users register 192.168 ansible 174.131 null

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

相关文章

  • ansible剧本
    ansible剧本ansible中有两种模式,分别是ad-hoc模式和playbook模式ad-hoc简而言之,就是"临时命令"-临时的看下远程机器的内存信息-临时的批量分发一个配置文件特别小的需求,临时命令就解决大需求,部署软件这样的复杂需求,就写剧本https://docs.ansible.com/ansible/latest/us......
  • ansible剧本进阶
    ansible剧本进阶一.剧本的高级特性剧本高级特性是完全遵循python的循环结构来的编程语言特有的逻辑控制语句变量循环等等你的剧本,可以考虑用高级特性,也可以不用高级特性是为了简化剧本比如,创建10个系统的用户不用循环手写ansiblebakcup-muser-a"name=cc01"an......
  • 使用ansible-playbook自动化安装Oracle DG数据库19c
    【用剧本安装OracleDG数据库】源库是RAC,安装的话可以参考之前内容,目标库DG是单机文件系统说明:源库是RAC架构,DG是单机文件系统架构,管理使用dgbroker管理(这里使用命令行操作),安装单机数据库然后使用createDuplicateDB方式创建DG库,由于剧本脚本较多,可以留言发送脚本所有剧本目录信......
  • ansible相比shell脚本自动化有什么优势
    Ansible自动化与Shell脚本自动化相比,具有以下几个显著的优势:轻量级与无需代理:Ansible的一个关键优势在于其轻量级和无需在被控制服务器上安装任何代理或客户端的特点。这极大地简化了部署和维护的流程,降低了复杂性和出错的可能性。而Shell脚本通常需要在每台服务器上单独执行,这......
  • ansible 安装以及简单使用
    自动化运维工具-ansible1.ansible的安装我选择的是rpm包安装下载地址:链接:https://pan.baidu.com/s/1dqm_QDGjd9oSNS5qEZzD5Q?pwd=34s5提取码:34s5 下载之后直接rpm-ivh sshpass-1.06-2.el7.x86_64.rpmrpm-ivh ansible-2.9.5-1.el7.ans.noarch.rpm2.ansible配置修改......
  • ansible 删除密码过期账户
    hosts[centos-root]192.168.174.129ansible_ssh_port=22192.168.174.130ansible_ssh_port=22192.168.174.131ansible_ssh_port=22AnsibleVault文件创建AnsibleVault文件#ansible-vaultcreatepasswords.ymlNewVaultpassword:#12345......
  • 在Linux中,如何使用Jenkins和Ansible进行虚拟化环境的自动化和持续集成/持续部署(CI/CD)?
    在Linux中,使用Jenkins和Ansible进行虚拟化环境的自动化和持续集成/持续部署(CI/CD)是一个强大的组合,可以大大提高开发和部署的效率。下面将详细阐述如何配置和使用这两个工具来实现这一目标。1.Jenkins配置安装Jenkins你可以从Jenkins官网下载最新的安装包,或者使用包管理器(如a......
  • 【小提示】消除Ansible执行rpm命令时显示的警告
    你好。我是AmazonWebServices团队的Sureing。最近,我们的麒麟系统存在openssl漏洞,我把它修好了。于是我使用命令对使用rpm包更新后的openssl进行版本检查当我运行shell模块使用rpm命令时,我收到以下警告:忽略它不会立即导致问题,但它很恶心,因此本文的目的是解决它。[root@host-Am......
  • 【小提示】指定Python版本以消除运行Ansible时显示的警告
    我正在编写Ansible手册来设置Mac。当我运行它时,我收到警告,所以我会调查它并分享我解决的问题。你好。我是AmazonWebServices团队的Sureing。最近,我的Mac键盘无法正常工作,我把它修好了。在此期间,将提供替换设备,但设置为默认设置。您可以手动更改设置,但让我们使用Ansible......
  • Ansible:更快点,执行过程分析、异步、效率优化【转】
    Ansible你快点:Ansible执行过程分析、异步、效率优化Ansible虽然方便,但有个"为人诟病"的问题:任务执行速度太慢了,在有大量任务、大量循环任务时,其速度之慢真的是会让人等到崩溃的。Ansible官方给了一些优化选项供用户选择,还可以去网上寻找优化Ansible相关的插件。但在调优Ansible......