企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 1、安装并启动httpd 1、查看hosts vim /etc/ansible/hosts 指定要连接远程主机的IP地址,用户名密码 ``` [test] 192.168.56.11 ansible_ssh_user="root" ansible_ssh_pass="tuna" ``` 2、编写playbook vim httpd.yml ``` - hosts: test vars: http_port: 80 max_clients: 200 tasks: - name: ensure apache is at the latest version yum: name: httpd state: latest - name: start httpd service: name: httpd state: started ``` ## 2、 修改用户密码 ### 2.1 使用user模块 1、 使用Python加密密码 ``` [root@localhost playbook]# python Python 2.7.5 (default, Aug 4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import crypt >>> crypt.crypt('666666') '$6$fS8goXMs4bO9YRn.$8lQZ3xCOJMMEj/ov1KmSvZpNtQF22WUsxRb6Mp1zgTJq2SZ.XiXCs.I5Sx5985hvOCpC9S5NPEdYNLIP9veVP.' >>> ``` 或者使用脚本 ``` #!/usr/bin/python # -*- coding:utf-8 -*- import crypt import sys def generate_pwd(password): print("加密前:%s" % password) print("加密后:%s" % crypt.crypt(password)) if __name__ == "__main__": if(len(sys.argv) != 2): print("eg:sys.argv[0] arg1") exit(1) else: generate_pwd(sys.argv[1]) ``` 对789加密 ``` [root@localhost playbook]# ./generate_password.py 789 加密前:789 加密后:$6$JF0I6DXqV5W9osb7$trt3TC0RopaBWC4IgV058ajZukULsw.Uhbym.dDRmkJMdveHlAzLdoBwzg62feONam4DrR0DzwkgI8EUdrl2v1 [root@localhost playbook]# ansible test -m user -a 'name=tuna password="$6$JF0I6DXqV5W9osb7$trt3TC0RopaBWC4IgV058ajZukULsw.Uhbym.dDRmkJMdveHlAzLdoBwzg62feONam4DrR0DzwkgI8EUdrl2v1"' ``` 2、使用user模块修改密码 将加密后的密码,进行使用,修改密码 ``` ansible test -m user -a 'name=tuna password="$6$fS8goXMs4bO9YRn.$8lQZ3xCOJMMEj/ov1KmSvZpNtQF22WUsxRb6Mp1zgTJq2SZ.XiXCs.I5Sx5985hvOCpC9S5NPEdYNLIP9veVP."' ``` ### 2.2 shell命令 ``` [root@localhost ~]# ansible test -m shell -a "echo 'tuna:9DA7ewq#81YN' | chpasswd" 192.168.56.11 | SUCCESS | rc=0 >> ``` 注意:密码中$会转义其他字符,所以修改后的秘密和预设不一样 ### 2.3 playbook 不存在特殊字符的限制 ``` - hosts: test tasks: - name: chang user password shell: echo 'tuna:\!@#$%.%^&*?\$q' | chpasswd ``` 执行playbook ``` [root@localhost playbook]# ansible-playbook change_password.yml PLAY [test] *************************************************************************************************************** TASK [Gathering Facts] **************************************************************************************************** ok: [192.168.56.11] TASK [chang user password] ************************************************************************************************ changed: [192.168.56.11] PLAY RECAP **************************************************************************************************************** 192.168.56.11 : ok=2 changed=1 unreachable=0 failed=0 ``` ## 3、copy文件 ``` [root@localhost playbook]# ansible test -m copy -a "src=/root/playbook/generate_password.py dest=/root/ owner=tuna group=tuna mode=0644" 192.168.56.11 | SUCCESS => { "changed": true, "checksum": "44c5fbbfd82fd30daf1bb5d924635ffe890d3426", "dest": "/root/generate_password.py", "gid": 1000, "group": "tuna", "md5sum": "77b0058ae5852046f1f7ae2c699231b3", "mode": "0644", "owner": "tuna", "secontext": "system_u:object_r:admin_home_t:s0", "size": 327, "src": "/root/.ansible/tmp/ansible-tmp-1557106908.87-184674353262296/source", "state": "file", "uid": 1000 } ```