ansible-playbook中become:sudo特权升级等

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

在这里插入图片描述
执行成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值