Ansible pre_tasks คืออะไร? จะอัปเดต OS, ติดตั้ง Python และติดตั้ง JRE บน Remote Host [Linux] ได้อย่างไร
เผยแพร่แล้ว: 2019-05-10pre_tasks ใน Ansible คืออะไร?
pre_tasks
เป็นงานที่ Ansible ดำเนินการก่อนที่จะดำเนินการ tasks
ใด ๆ ที่กล่าวถึงในไฟล์ . .yml
พิจารณาสถานการณ์นี้ คุณได้จัดเตรียมอินสแตนซ์ใหม่บน Amazon EC2
cloud หรือ Google Cloud
สิ่งแรกที่คุณต้องทำคือติดตั้งการอัปเดตระบบปฏิบัติการ จากนั้นติดตั้ง Python ล่าสุด ติดตั้ง Java และอื่นๆ
เมื่องานเบื้องต้นทั้งหมดข้างต้นเสร็จสิ้น คุณจะต้องคัดลอกแอปพลิเคชันของคุณและเริ่มแอปพลิเคชันเหล่านั้น จำเป็นอย่างยิ่งที่จะต้องติดตั้งไบนารีพื้นฐานทั้งหมดก่อนที่คุณจะคัดลอกการขึ้นต่อกันของแอปพลิเคชันของคุณ
ในบทช่วยสอนนี้ เราจะพูดถึงรายละเอียดทั้งหมดเกี่ยวกับวิธีดำเนินการงานล่วงหน้าโดยใช้แท็ก pre_task
เราจะปฏิบัติตามสถานการณ์ด้านล่างในบทช่วยสอนนี้:
- สร้างไฟล์
crunchify-hosts
file และเพิ่ม IP ซึ่งเราจะดำเนินการ pre_task - สร้างไฟล์
crunchify-install-python-java.yml
ซึ่งเป็น playbook ที่เล่นได้- pre_task: ติดตั้ง python-simplejson
- pre_task: ติดตั้ง python-minimal
- pre_task: ติดตั้งการอัปเดตระบบ
- pre_task: ติดตั้ง JRE . ล่าสุด
- รับเวอร์ชัน Python
- รับเวอร์ชัน Java
- พิมพ์ผลการตรวจแก้จุดบกพร่องทั้งหมด
- รันคำสั่ง ansible-playbook -i ./crunchify-hosts crunchify-install-python-java.yml ซึ่งจะทำงานทั้งหมดของเรา
ไฟล์ crunchify-hosts
1 2 3 4 5 6 7 8 9 10 |
[ local ] localhost ansible_connection = local ansible_python_interpreter = python [ crunchify ] 13.58.187.197 [ crunchify : vars ] ansible_ssh_user = ubuntu ansible_ssh_private_key_file =/ Users / crunchify / Documents / ansible / crunchify . pem ansible_python_interpreter =/ usr / bin / python3 |
อย่างที่คุณเห็นฉันกำลังใช้ไฟล์ crunchify.pem
สำหรับการตรวจสอบรหัสผ่านน้อยกว่า ฉันสามารถเชื่อมต่อกับโฮสต์ของฉันได้โดยไม่ต้องใช้รหัสผ่าน
ไฟล์ crunchify-install-python-java.yml
เราใช้คีย์เวิร์ด register
ใน Ansible เพื่อลงทะเบียนตัวแปร มันเก็บค่าส่งคืนของงาน raw
ด้วยความช่วยเหลือของ debug
และ stdout_lines
คุณสามารถพิมพ์ผลลัพธ์บนบรรทัดคำสั่ง
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
--- - hosts : crunchify become : yes pre_tasks : - raw : sudo apt - get - y install python - simplejson register : py_simple_output - raw : sudo apt - get - y install python - minimal register : py_minimal_output - raw : sudo apt - get update register : system_output - raw : sudo apt - get install - y default - jre register : java_output tasks : - debug : var = py_simple_output . stdout_lines - debug : var = py_minimal_output . stdout_lines - debug : var = system_output . stdout_lines - debug : var = java_output . stdout_lines - name : get Python version shell : python -- version 2 > &1 register : py_output - debug : var = py_output . stdout_lines - name : get Java version shell : java -- version 2 > &1 register : java_output - debug : var = java_output . stdout_lines |
เรียกใช้คำสั่ง:
ansible-playbook -i ./crunchify-hosts crunchify-install-python-java.yml
ผลลัพธ์ที่ได้:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
bash1 . 2 $ ansible - playbook - i . / crunchify - hosts crunchify - install - python - java . yml PLAY [ crunchify ] *************************************************************************************************************************************************** TASK [ Gathering Facts ] ********************************************************************************************************************************************* ok : [ 13.58.187.197 ] TASK [ raw ] ********************************************************************************************************************************************************* changed : [ 13.58.187.197 ] TASK [ raw ] ********************************************************************************************************************************************************* changed : [ 13.58.187.197 ] TASK [ raw ] ********************************************************************************************************************************************************* changed : [ 13.58.187.197 ] TASK [ raw ] ********************************************************************************************************************************************************* changed : [ 13.58.187.197 ] TASK [ debug ] ******************************************************************************************************************************************************* ok : [ 13.58.187.197 ] = > { "py_simple_output.stdout_lines" : [ "" , "Reading package lists... 0%" , "" , "Reading package lists... 100%" , "" , "Reading package lists... Done" , "" , "" , "Building dependency tree... 0%" , "" , "Building dependency tree... 50%" , "" , "Building dependency tree " , "" , "" , "Reading state information... 0%" , "" , "Reading state information... Done" , "" , "python-simplejson is already the newest version (3.13.2-1)." , "0 upgraded, 0 newly installed, 0 to remove and 76 not upgraded." ] } TASK [ debug ] ******************************************************************************************************************************************************* ok : [ 13.58.187.197 ] = > { "py_minimal_output.stdout_lines" : [ "" , "Reading package lists... 0%" , "" , "Reading package lists... 100%" , "" , "Reading package lists... Done" , "" , "" , "Building dependency tree... 0%" , "" , "Building dependency tree... 50%" , "" , "Building dependency tree " , "" , "" , "Reading state information... 0%" , "" , "Reading state information... Done" , "" , "python-minimal is already the newest version (2.7.15~rc1-1)." , "0 upgraded, 0 newly installed, 0 to remove and 76 not upgraded." ] } TASK [ debug ] ******************************************************************************************************************************************************* ok : [ 13.58.187.197 ] = > { "system_output.stdout_lines" : [ "" , "0% [Working]" , " " , "Hit:1 http://us-east-2.ec2.archive.ubuntu.com/ubuntu bionic InRelease" , "" , "0% [Connecting to security.ubuntu.com (91.189.88.162)]" , " " , "Hit:2 http://us-east-2.ec2.archive.ubuntu.com/ubuntu bionic-updates InRelease" , "" , " " , "Get:3 http://us-east-2.ec2.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]" , "" , "0% [Connecting to security.ubuntu.com (91.189.88.162)]" , "0% [1 InRelease gpgv 242 kB] [Connecting to security.ubuntu.com (91.189.88.162)" , " " , "0% [Connecting to security.ubuntu.com (91.189.88.162)]" , "0% [2 InRelease gpgv 88.7 kB] [Connecting to security.ubuntu.com (91.189.88.162" , " " , "0% [Waiting for headers]" , "0% [3 InRelease gpgv 74.6 kB] [Waiting for headers]" , " " , "Hit:4 http://security.ubuntu.com/ubuntu bionic-security InRelease" , "" , " " , "0% [3 InRelease gpgv 74.6 kB]" , " " , "0% [Working]" , "0% [4 InRelease gpgv 88.7 kB]" , " " , "100% [Working]" , " " , "Fetched 74.6 kB in 0s (249 kB/s)" , "" , "Reading package lists... 0%" , "" , "Reading package lists... 5%" , "" , "Reading package lists... 8%" , "" , "Reading package lists... 53%" , "" , "Reading package lists... 79%" , "" , "Reading package lists... 99%" , "" , "Reading package lists... Done" , "" ] } TASK [ debug ] ******************************************************************************************************************************************************* ok : [ 13.58.187.197 ] = > { "java_output.stdout_lines" : [ "" , "Reading package lists... 0%" , "" , "Reading package lists... 100%" , "" , "Reading package lists... Done" , "" , "" , "Building dependency tree... 0%" , "" , "Building dependency tree... 50%" , "" , "Building dependency tree " , "" , "" , "Reading state information... 0%" , "" , "Reading state information... Done" , "" , "default-jre is already the newest version (2:1.11-68ubuntu1~18.04.1)." , "0 upgraded, 0 newly installed, 0 to remove and 76 not upgraded." ] } TASK [ get Python version ] ****************************************************************************************************************************************** changed : [ 13.58.187.197 ] TASK [ debug ] ******************************************************************************************************************************************************* ok : [ 13.58.187.197 ] = > { "py_output.stdout_lines" : [ "Python 2.7.15rc1" ] } TASK [ get Java version ] ******************************************************************************************************************************************** changed : [ 13.58.187.197 ] TASK [ debug ] ******************************************************************************************************************************************************* ok : [ 13.58.187.197 ] = > { "java_output.stdout_lines" : [ "openjdk 11.0.2 2019-01-15" , "OpenJDK Runtime Environment (build 11.0.2+9-Ubuntu-3ubuntu118.04.3)" , "OpenJDK 64-Bit Server VM (build 11.0.2+9-Ubuntu-3ubuntu118.04.3, mixed mode, sharing)" ] } PLAY RECAP * ******************************************************************************************************************************************************** 13.58.187.197 : ok = 13 changed = 6 unreachable = 0 failed = 0 |
แค่นั้นแหละ.

อย่างที่คุณเห็น ในบทช่วยสอนนี้ เราได้ติดตั้งการอัปเดต Python, java และระบบบนโฮสต์ระยะไกล ส่งคืนผลลัพธ์กลับไปที่หน้าต่างเทอร์มินัล Mac
อะไรต่อไป?
ลองอ่านบทช่วยสอนเกี่ยวกับวิธีการคัดลอกไฟล์ ไดเรกทอรี หรือสคริปต์จาก localhost ไปยังโฮสต์ระยะไกล