هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
الهدف
Write the Ansible project you would really use to manage web servers: an inventory with groups and variables, a playbook that installs and configures Nginx through a role, a template, a handler that reloads only on change, a secret kept with Ansible Vault, and a safe workflow with --check --diff before every change. Run it twice to prove it is idempotent.
قبل أن تبدأ
- Control node:
ansible --version(ansible-core 2.15+). Two VMsweb1andweb2with a user that has SSH key login andsudo. - Make the folder
~/ansible-lab. Test the connection:ansible all -i web1,web2, -m ping -u youruser.
المهام
ضع علامة على المهمة عندما تنتهي منها. متصفحك فقط يتذكر ذلك.
- Write
ansible.cfg(inventory file, remote user,become, no host key prompts for the lab) andinventory.iniwith the groupweb(both VMs) and a group variablehttp_port: 8080. - Create a role
webserverwithansible-galaxy role init roles/webserver. - In the role: install
nginx, deploytemplates/site.conf.j2(listen onhttp_port, a server name from a host variable), make sure the service is started and enabled, and open the port in the firewall. - Use a handler
reload nginxthat runs only when the template changed. - Keep a secret API token in
group_vars/web/vault.ymlencrypted withansible-vault, and write it into/etc/nginx/api_tokenwith mode0600. - Write
site.ymlthat applies the role toweb. Run it with--check --difffirst, then for real. - Run it a second time: the result must be
changed=0(idempotent). Change the port to 8081 and see that only the template task and the handler run. - Use an ad-hoc command to check the page on both servers, and
--limit web1to change only one server.
تحقّق من عملك
ansible-inventory --graph
ansible-playbook site.yml --ask-vault-pass
ansible-playbook site.yml --ask-vault-pass | tail -4
ansible web -m uri -a "url=http://localhost:{{ http_port }} return_content=false"
ansible-vault view group_vars/web/vault.yml --ask-vault-pass- The graph shows
@webwithweb1andweb2. - The second run shows
changed=0for both hosts. - The
uricheck gives"status": 200on both servers. vault.ymlis unreadable in Git ($ANSIBLE_VAULT;1.1;AES256header) and readable only with the password.
تلميحات
اعرض التلميحات
- Modules:
ansible.builtin.package,ansible.builtin.template,ansible.builtin.service,ansible.posix.firewalld(RHEL) orcommunity.general.ufw(Ubuntu),ansible.builtin.copy. notify: reload nginxon the template task; the handler with the samenamelives inroles/webserver/handlers/main.yml.no_log: trueon the task that writes the secret, so it never appears in the output.- A broken template would make the reload fail: test the full config in the handler chain (
nginx -t) or with the template optionvalidate:(it needs a%splaceholder and a file that is valid on its own). - On RHEL with SELinux, a port other than 80/443/8080 also needs
community.general.seport(8080 is already allowed ashttp_port_t).
الحل
اعرض الحل
- Project files.ansible.cfg
[defaults] inventory = inventory.ini remote_user = youruser host_key_checking = False roles_path = roles [privilege_escalation] become = Trueinventory.ini[web] web1 ansible_host=192.168.56.11 site_name=web1.lab.internal web2 ansible_host=192.168.56.12 site_name=web2.lab.internal [web:vars] http_port=8080 - The role (only the files you change).roles/webserver/tasks/main.yml
- name: Install nginx ansible.builtin.package: name: nginx state: present - name: Deploy the site config ansible.builtin.template: src: site.conf.j2 dest: /etc/nginx/conf.d/site.conf mode: "0644" notify: reload nginx - name: Write the API token ansible.builtin.copy: content: "{{ vault_api_token }}\n" dest: /etc/nginx/api_token mode: "0600" no_log: true - name: Open the port (RHEL family) ansible.posix.firewalld: port: "{{ http_port }}/tcp" permanent: true immediate: true state: enabled when: ansible_os_family == "RedHat" - name: Start and enable nginx ansible.builtin.service: name: nginx state: started enabled: trueroles/webserver/handlers/main.yml- name: reload nginx ansible.builtin.service: name: nginx state: reloadedroles/webserver/templates/site.conf.j2# managed by Ansible - do not edit by hand server { listen {{ http_port }}; server_name {{ site_name }}; location / { return 200 "hello from {{ inventory_hostname }}\n"; } }site.yml- name: Web servers hosts: web roles: - webserver - The secret and the runs.
mkdir -p group_vars/web && ansible-vault create group_vars/web/vault.yml ansible-playbook site.yml --ask-vault-pass --check --diff ansible-playbook site.yml --ask-vault-pass ansible-playbook site.yml --ask-vault-pass web1 : ok=6 changed=0 unreachable=0 failed=0 skipped=0 web2 : ok=6 changed=0 unreachable=0 failed=0 skipped=0In the vault file write one line:
vault_api_token: "abc123-lab". - A small change, one server.
sed -i 's/http_port=8080/http_port=8081/' inventory.ini ansible-playbook site.yml --ask-vault-pass --limit web1 --diff ansible web -m uri -a "url=http://localhost:{{ http_port }}"On RHEL, port 8081 also needs the SELinux port type (
community.general.seportwithsetype: http_port_t), or change it back to 8080.
ملاحظاتي
لا توجد ملاحظات بعد.