ÔõÑùÔÚ÷è÷ë²Ù×÷ϵͳÉϾÙÐÐϵͳµÄ×Ô¶¯»¯¾ç±¾ºÍʹÃüÖÎÀí£¿
ÔõÑùÔÚ÷è÷ë²Ù×÷ϵͳÉϾÙÐÐϵͳµÄ×Ô¶¯»¯¾ç±¾ºÍʹÃüÖÎÀí£¿
÷è÷ë²Ù×÷ϵͳÊÇÒ»ÖÖ»ùÓÚLinuxµÄ²Ù×÷ϵͳ£¬Ëü¾ßÓÐÎȹÌÐÔºÍÇå¾²ÐԸߵÄÌص㡣ÔÚ÷è÷ë²Ù×÷ϵͳÉÏ£¬ÎÒÃÇ¿ÉÒÔͨ¹ý±àд×Ô¶¯»¯¾ç±¾ºÍʹÃüÀ´Ìá¸ßϵͳµÄЧÂʺÍÎȹÌÐÔ¡£
Ò»¡¢Shell¾ç±¾µÄ±àдºÍÖ´ÐÐ
Shell¾ç±¾ÊÇÒ»ÖÖÓÃÀ´±àдºÍÖ´ÐÐÏÂÁîµÄ¾ç±¾ÓïÑÔ¡£Í¨¹ý±àдShell¾ç±¾£¬ÎÒÃÇ¿ÉÒÔÔÚ÷è÷ë²Ù×÷ϵͳÉÏ×Ô¶¯»¯Íê³ÉһϵÁÐÖØ´óµÄ²Ù×÷¡£
½¨ÉèÒ»¸öShell¾ç±¾
ÔÚ÷è÷ë²Ù×÷ϵͳÉÏ£¬¿ÉÒÔʹÓÃÎı¾±à¼Æ÷£¨Èçvi»ònano£©½¨ÉèÒ»¸öеÄÎļþ£¬ÎļþÃûÒÔ.sh×îºó£¬ÌåÏÖ¸ÃÎļþΪShell¾ç±¾¡£ÀýÈ磬ÎÒÃǽ¨ÉèÒ»¸öÃûΪtest.shµÄShell¾ç±¾Îļþ¡£
vi test.sh
µÇ¼ºó¸´ÖÆ
±àдShell¾ç±¾
ÔÚtest.shÎļþÖУ¬ÎÒÃÇ¿ÉÒÔ±àдһЩÏÂÁÓÃÀ´ÊµÏÖϵͳµÄ×Ô¶¯»¯²Ù×÷¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂëÔÚÖÕ¶ËÖÐÊä³ö”Hello World!”¡£
#!/bin/bash echo "Hello World!"
µÇ¼ºó¸´ÖÆ
Ö´ÐÐShell¾ç±¾
ÉúÑIJ¢Í˳ötest.shÎļþºó£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´Ö´ÐÐShell¾ç±¾¡£
chmod +x test.sh ./test.sh
µÇ¼ºó¸´ÖÆ
ÕâÑù£¬Öն˽«Êä³ö”Hello World!”¡£
¶þ¡¢ÍýÏëʹÃüµÄÖÎÀí
³ýÁËͨ¹ýShell½ÅÔÀ´×Ô¶¯»¯Ö´ÐÐÒ»´ÎÐÔʹÃüÍ⣬ÎÒÃÇ»¹¿ÉÒÔʹÓÃÍýÏëʹÃüÀ´°´ÆÚÖ´ÐÐһЩÌض¨µÄʹÃü¡£
Ìí¼ÓÍýÏëʹÃü
ÔÚ÷è÷ë²Ù×÷ϵͳÉÏ£¬¿ÉÒÔʹÓÃcrontabÏÂÁîÀ´Ìí¼ÓÍýÏëʹÃü¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´Ìí¼ÓÒ»¸öÌìÌìÔçÉÏ8µãÖ´ÐеÄʹÃü¡£
crontab -e
µÇ¼ºó¸´ÖÆ
ÔÚ·¿ªµÄÎı¾±à¼Æ÷ÖУ¬ÎÒÃÇ¿ÉÒÔÌí¼ÓÒÔÏÂÄÚÈÝ¡£
0 8 * * * /path/to/your/script.sh
µÇ¼ºó¸´ÖÆ
ÕâÀïµÄ/path/to/your/script.shΪÄúÐèÒªÖ´ÐеÄShell¾ç±¾µÄ·¾¶¡£
Éó²éÍýÏëʹÃüÁбí
ÈôÊÇÏëÒªÉó²éÄ¿½ñÒѾÌí¼ÓµÄÍýÏëʹÃüÁÐ±í£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁî¡£
crontab -l
µÇ¼ºó¸´ÖÆ
ÕâÑù£¬ÏµÍ³½«ÏÔʾĿ½ñÒѾÌí¼ÓµÄÍýÏëʹÃüÁÐ±í¡£
Èý¡¢×Ô¶¯»¯¹¤¾ßAnsibleµÄʹÓÃ
³ýÁËʹÓÃshell¾ç±¾ºÍÍýÏëʹÃüÀ´ÖÎÀíϵͳ£¬ÎÒÃÇ»¹¿ÉÒÔʹÓÃ×Ô¶¯»¯¹¤¾ßAnsibleÀ´ÊµÏÖϵͳÉèÖúÍʹÃüµÄÖÎÀí¡£
×°ÖÃAnsible
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚ÷è÷ë²Ù×÷ϵͳÉÏ×°ÖÃAnsible¡£¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´×°ÖÃAnsible¡£
sudo apt-get update sudo apt-get install ansible
µÇ¼ºó¸´ÖÆ
±àдAnsible playbook
ÔÚAnsibleÖУ¬ÎÒÃÇʹÓÃplaybookÎļþÀ´±àдÉèÖúÍʹÃüµÄ×Ô¶¯»¯¾ç±¾¡£¿ÉÒÔʹÓÃÎı¾±à¼Æ÷½¨ÉèÒ»¸öеÄplaybookÎļþ£¬ÎļþÃûÒÔ.yml×îºó¡£
ÀýÈ磬ÎÒÃǽ¨ÉèÒ»¸öÃûΪtest.ymlµÄplaybookÎļþ¡£
--- - name: Test Playbook hosts: all tasks: - name: Print Hello World ansible.builtin.debug: msg: "Hello World!"
µÇ¼ºó¸´ÖÆ
Ö´ÐÐAnsible playbook
ÉúÑIJ¢Í˳ötest.ymlÎļþºó£¬ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´Ö´ÐÐAnsible playbook¡£
ansible-playbook test.yml
µÇ¼ºó¸´ÖÆ
ÕâÑù£¬Ansible½«ÔÚ÷è÷ë²Ù×÷ϵͳÉÏÖ´ÐÐtest.ymlÖнç˵µÄʹÃü£¬Êä³ö”Hello World!”¡£
×ܽ᣺
±¾ÎÄÏÈÈÝÁËÔõÑùÔÚ÷è÷ë²Ù×÷ϵͳÉϾÙÐÐϵͳµÄ×Ô¶¯»¯¾ç±¾ºÍʹÃüÖÎÀí¡£Í¨¹ý±àдShell¾ç±¾¡¢Ê¹ÓÃÍýÏëʹÃüºÍʹÓÃAnsible¹¤¾ß£¬ÎÒÃÇ¿ÉÒÔÔÚ÷è÷ë²Ù×÷ϵͳÉÏʵÏÖϵͳµÄ×Ô¶¯»¯ÉèÖúÍʹÃüµÄ°´ÆÚÖ´ÐС£ÕâЩҪÁì¿ÉÒÔ×ÊÖúÌá¸ßϵͳµÄЧÂʺÍÎȹÌÐÔ£¬ïÔÌÈ˹¤²Ù×÷µÄÊÂÇéÁ¿¡£
ÒÔÉϾÍÊÇÔõÑùÔÚ÷è÷ë²Ù×÷ϵͳÉϾÙÐÐϵͳµÄ×Ô¶¯»¯¾ç±¾ºÍʹÃüÖÎÀí£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡