question1
There is an ansible inventory file located at ~/playbooks/inventory on iac-jumphost.
1,Create a playbook ~/playbooks/apache.yml on iac-jumphost and perform the following tasks on server1.
2,Install apache package (whatever default version is available in apt repo)
3,Start the service but make it disabled so that it doesn’t start at the boot time.
在~/playbooks/目录下创建apache.yaml
未修改yaml文件前,报错如下:
错误显示无权限:
修改:添加become
---
- name: Ansible Playbook to Install and Setup Apache on Ubuntu
hosts: server1
become: yes
tasks:
- name: Install present version of Apache
apt: name=apache2 update_cache=yes state=present
- name: ensure apache2 running
service: name=apache2 state=started enabled=no
Brief description:
host: is hostname-group as defined in inventory file (hosts or hosts.yaml)
become: yes is privilege escalation for being sudo
tasks: five tasks are defined. Installing latest apache, copying index page, setting up virtual host, enable site, allowing http from UFW firewall if not enabled already.
handlers: To restart apache if all the other tasks are successful.
执行:
ansible-playbook -i inventory apache.yaml
成功执行
question2:
1,Create a playbook called archive.yml under /home/bob/playbooks on iac-jumphost using the existing an inventory file that is already available in the same directory.
2,Create an archive backup.tar.gz (archive format should be tar.gz) of the directory /usr/src/security/ directory (on server1 ) and copy it to /opt/security/ directory on the same server. The user and group owner of the archive file - backup.tar.gz should be bob.
---
- hosts: server1
become: yes
tasks:
- name: Create an archive backup.tar.gz
shell: tar -cf backup.tar.gz /usr/src/security/ . && cp /home/bob/backup.tar.gz /opt/security/ && chown bob:bob /opt/security/backup.tar.gz
执行
ansible-playbook -i inventory archive.yml
执行成功