企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
组成结构: Inventory Modules Ad Hoc Comands Playbooks Tasks: 任务 variables: 变量 templates: 模板 handlers: 案例1: ansible添加具有sudo权限的用户 1)生成密码 * 安装pip [admin@node1 ~]$ sudo wget "https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz#md5=834b2904f92d46aaa333267fb1c922bb" --no-check-certificate * 生成密码 #sudo pip install passlib [admin@node1 ~]$ python -c "from passlib.hash import sha512_crypt; import getpass; print sha512_crypt.encrypt(getpass.getpass())" Password: 输入密码 $6$rounds=656000$SiOogbD5vf7soBFY$bDX2gXdy5bpSBBx9KwglMhmYCLGfiZ0G4zCYhOzDOZcIANkcTf1ASuYtwFNH8zy145k7CMolOu7816VGFeihT0 2)编写playbook ~~~ [admin@node1 ~]$ vim sudo_user.yml --- - hosts: webserver user: admin become: yes vars: user: test password: $6$rounds=656000$SiOogbD5vf7soBFY$bDX2gXdy5bpSBBx9KwglMhmYCLGfiZ0G4zCYhOzDOZcIANkcTf1ASuYtwFNH8zy145k7CMolOu7816VGFeihT0 tasks: - name: Add user {{ user }} user: name={{user}} comment="test user" uid=1057 - name: Config /etc/sudoers lineinfile: dest=/etc/sudoers state=present line='{{item}}' with_items: - "{{ user }} ALL=(ALL) NOPASSWD: ALL" ~~~ 3、执行过程 ~~~ [admin@node1 ~]$ ansible-playbook sudo_user.yml PLAY [webserver] *************************************************************************************************************************************************************************************************** TASK [Gathering Facts] ********************************************************************************************************************************************************************************************* ok: [192.168.20.138] ok: [192.168.20.137] TASK [Add user test] *********************************************************************************************************************************************************************************************** ok: [192.168.20.138] ok: [192.168.20.137] TASK [Config /etc/sudoers] ***************************************************************************************************************************************************************************************** changed: [192.168.20.138] => (item=test ALL=(ALL) NOPASSWD: ALL) changed: [192.168.20.137] => (item=test ALL=(ALL) NOPASSWD: ALL) PLAY RECAP ********************************************************************************************************************************************************************************************************* 192.168.20.137 : ok=3 changed=1 unreachable=0 failed=0 192.168.20.138 : ok=3 changed=1 unreachable=0 failed=0 ~~~