ansible--自动化运维

Managing Ansible Configuration Files

[kiosk@foundation0 ~]$ ssh student@workstation

[student@workstation ~]$ lab manage setup

[student@workstation ~]$ mkdir /home/student/dep-manage

[student@workstation ~]$ cd /home/student/dep-manage/

[student@workstation dep-manage]$ vim ansible.cfg

[defaults]
inventory = ./inventory

[student@workstation dep-manage]$ vim inventory

[myself]
localhost

[intranetweb]
servera.lab.example.com

[everyone:children]
myself
intranetweb

[student@workstation dep-manage]$ ansible everyone --list-hosts

[student@workstation dep-manage]$ ansible all --list-hosts

[student@workstation dep-manage]$ vim ansible.cfg

[defaults]
inventory = ./inventory

[privilege_escalation]
become = true
become_method = sudo
become_user = root
become_ask_pass = true
'-v' see the location of the current configuration file

[student@workstation dep-manage]$ ansible all --list-hosts -v

[student@workstation dep-manage]$ ansible all -m ping

password:student

Running Ad Hoc Commands

[student@workstation dep-manage]$ lab adhoc setup

[student@workstation dep-manage]$ sudo cat /etc/sudoers.d/devops

password:student

[student@workstation dep-manage]$ ssh devops@servera.lab.example.com

[devops@servera ~]$ sudo cat /etc/sudoers.d/devops

[student@workstation dep-manage]$ cd /home/student/dep-adhoc/

[student@workstation dep-adhoc]$ cat ansible.cfg

[defaults]
inventory=inventory
[student@workstation dep-adhoc]$ cat inventory 
[myself]
localhost

[intranetweb]
servera.lab.example.com

[everyone:children]
myself
intranetweb

[student@workstation dep-adhoc]$ ansible all -m ping

[student@workstation dep-adhoc]$ ansible localhost -m command -a 'id'

[student@workstation dep-adhoc]$ ansible localhost -m command -a 'id' -u devops

[student@workstation dep-adhoc]$ ansible localhost -m command -a 'cat /etc/motd' -u devops

'--become' option to switch to root

[student@workstation dep-adhoc]$ ansible localhost -m copy -a 'content="Managed by Ansible\n" dest=/etc/motd' -u devops

[student@workstation dep-adhoc]$ ansible localhost -m copy -a 'content="Managed by Ansible\n" dest=/etc/motd' -u devops --become

in the everyone host group

[student@workstation dep-adhoc]$ ansible everyone -m copy -a 'content="Managed by Ansible\n" dest=/etc/motd' -u devops --become

[student@workstation dep-adhoc]$ ansible everyone -m command -a 'cat /etc/motd' -u devops

Managed Dynamic Inventories

[student@workstation dep-adhoc]$ lab deploy-dynamic setup

[student@workstation dep-adhoc]$ cd /home/student/dep-dynamic/

[student@workstation dep-dynamic]$ vim ansible.cfg

[defaults]
inventory = inventory

[student@workstation dep-dynamic]$ mkdir inventory

[student@workstation dep-dynamic]$ wget http://materials.example.com/dynamic/inventorya.py -O inventory/inventorya.py

[student@workstation dep-dynamic]$ wget http://materials.example.com/dynamic/inventoryw.py -O inventory/inventoryw.py

[student@workstation dep-dynamic]$ wget http://materials.example.com/dynamic/hosts -O inventory/hosts

[student@workstation dep-dynamic]$ ansible -i inventory/inventorya.py webservers --list-hosts

[student@workstation dep-dynamic]$ chmod 755 inventory/inventorya.py

[student@workstation dep-dynamic]$ chmod 755 inventory/inventoryw.py

[student@workstation dep-dynamic]$ inventory/inventorya.py --list

[student@workstation dep-dynamic]$ inventory/inventoryw.py --list

[student@workstation dep-dynamic]$ cat inventory/hosts

[student@workstation dep-dynamic]$ ansible webservers --list-hosts

[student@workstation dep-dynamic]$ vim inventory/hosts

[webservers]

[servers:children]
webservers

[student@workstation dep-dynamic]$ ansible webservers --list-hosts

Lab:Deploying Ansible

[student@workstation dep-dynamic]$ lab deploy setup

[student@workstation dep-dynamic]$ yum list installed ansible

[student@workstation dep-dynamic]$ ansible --version

[student@workstation dep-dynamic]$ mkdir /home/student/dep-lab

[student@workstation dep-dynamic]$ cd /home/student/dep-lab/

disable privilege escalation and without password authentication

[student@workstation dep-lab]$ vim ansible.cfg

[defaults]
remote_user = devops
inventory = inventory

[privilege_escalation]
become = False
become_method = sudo
becom_user = root
become_ask_pass = False

[student@workstation dep-lab]$ wget http://materials.example.com/dynamic/inventory -O inventory/inventory

[student@workstation dep-lab]$ wget http://materials.example.com/dynamic/binventory.py -O inventory/binventory.py

[student@workstation dep-lab]$ chmod 755 inventory/binventory.py

[student@workstation dep-lab]$ vim inventory/inventory

[internetweb]

[intranetweb]
servera.lab.example.com
serverc.lab.example.com
serverd.lab.example.com

[everyone:children]
intranetweb
internetweb

[student@workstation dep-lab]$ ansible everyone -m command -a 'id'

[student@workstation dep-lab]$ ansible everyone -m copy -a 'content="This server is managed by Ansible.\n" dest=/etc/motd' --become

[student@workstation dep-lab]$ ansible everyone -m command -a 'cat /etc/motd'

[student@workstation dep-lab]$ lab deploy grade

转载于:https://blog.51cto.com/11732619/2061768

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值