sudo apt install ansible git sshpass
確認版本
jonathan@ct-ansible:~$ ansible --version ansible 2.9.6 config file = /etc/ansible/ansible.cfg configured module search path = ['/home/jonathan/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python3/dist-packages/ansible executable location = /usr/bin/ansible python version = 3.8.10 (default, May 26 2023, 14:05:08) [GCC 9.4.0]
sudo vi /etc/ansible/ansible.cfg
[defaults] : : # uncomment this to disable SSH key host checking #host_key_checking = False host_key_checking = False :
servers: hosts: aac: ansible_host: 192.168.11.249 ansible_port: 22 ansible_user: root ansible_ssh_pass: "mypassword" h470: ansible_host: 192.168.11.252 ansible_port: 22 ansible_connection: ssh ansible_user: root ansible_ssh_pass: "mypassword"
$ ansible all -i inventory.yaml --list-hosts hosts (2): aac h470
- hosts: servers become: true become_user: root tasks: - name: Ansible apt to install multiple packages - LAMP register: updatesys apt: update_cache: yes name: - python3-apt - snmp - libsasl2-modules state: present - name: Update apt repo and cache on all Debian/Ubuntu boxes apt: update_cache=yes force_apt_get=yes cache_valid_time=3600 - name: Upgrade all packages on servers apt: upgrade=dist force_apt_get=yes - name: Check if a reboot is needed on all servers register: reboot_required_file stat: path=/var/run/reboot-required get_md5=no - name: Reboot the box if kernel updated reboot: msg: "Reboot initiated by Ansible for kernel updates" connect_timeout: 5 reboot_timeout: 300 pre_reboot_delay: 0 post_reboot_delay: 30 test_command: uptime when: reboot_required_file.stat.exists
ansible-playbook -i inventory.yaml upgrade.yaml -e ansible_python_interpreter=/usr/bin/python --check
執行結果
$ ansible-playbook -i inventory.yaml upgrade.yaml -e ansible_python_interpreter=/usr/bin/python --check PLAY [servers] ****************************************************************************************************************************************************************************** TASK [Gathering Facts] ********************************************************************************************************************************************************************** ok: [aac] ok: [h470] TASK [Ansible apt to install multiple packages - LAMP] ************************************************************************************************************************************** changed: [h470] changed: [aac] TASK [Update apt repo and cache on all Debian/Ubuntu boxes] ********************************************************************************************************************************* ok: [h470] ok: [aac] TASK [Upgrade all packages on servers] ****************************************************************************************************************************************************** ok: [h470] ok: [aac] TASK [Check if a reboot is needed on all servers] ******************************************************************************************************************************************* ok: [h470] ok: [aac] TASK [Reboot the box if kernel updated] ***************************************************************************************************************************************************** skipping: [aac] skipping: [h470] PLAY RECAP ********************************************************************************************************************************************************************************** aac : ok=5 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0 h470 : ok=5 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
$ ansible-vault encrypt_string MyPassword --ask-vault-pass New Vault password: KeyPass Confirm New Vault password: KeyPass !vault | $ANSIBLE_VAULT;1.1;AES256 63613230353861653733633761663630643564323330613263343061656163383731386364666366 3430303131616563616634386130613461636433383730360a663130653463313465623837373335 61336333643663343535396339633165653334336236363032613130636537336664646535666666 3863306137663763610a313034383233626563336365303431313564316338653363636432386438 3736 Encryption successful
: hosts: aac: ansible_host: 192.168.11.249 ansible_ssh_pass: "MyPassword" :
改成
: hosts: aac: ansible_host: 192.168.11.249 ansible_ssh_pass: !vault | $ANSIBLE_VAULT;1.1;AES256 63613230353861653733633761663630643564323330613263343061656163383731386364666366 3430303131616563616634386130613461636433383730360a663130653463313465623837373335 61336333643663343535396339633165653334336236363032613130636537336664646535666666 3863306137663763610a313034383233626563336365303431313564316338653363636432386438 3736 :
$ ansible-playbook -i inventory.yaml upgrade.yaml --ask-vault-pass Vault password: KeyPass PLAY [servers] ****************************************************************************************************************************************************************************** TASK [Gathering Facts] ********************************************************************************************************************************************************************** ok: [nuc] :
$ ansible-playbook -i inventory.yaml upgrade.yaml --vault-password-file ./.vault_pass