ansible playbook使用、安装nginx、管理配置文件

文章目录ansible playbook使用ansible playbook使用playbook与ansible命令行执行命令的区别就是将要执行的操作定义到配置文件中,直接执行配置文件即可创建playbook配置文件:[root@linux01 ~]# vim /etc/ansible/test.yml配置文件内容:---- hosts: 192.168.234.130 use...
摘要由CSDN通过智能技术生成

ansible playbook使用

playbook与ansible命令行执行命令的区别就是将要执行的操作定义到配置文件中,直接执行配置文件即可

创建playbook配置文件:

[root@linux01 ~]# vim /etc/ansible/test.yml

配置文件内容:

---
- hosts: 192.168.234.130
  user: root
  tasks:
    - name: test_playbook
      shell: touch /tmp/test_playbook 

说明: 第一行需要有三个杠,hosts参数指定了对哪些主机进行操作,如果是多台机器可以用逗号作为分隔,也可以使用主机组,在/etc/ansible/hosts里定义;user参数指定了使用什么用户登录远程主机操作;tasks指定了一个任务,其下面的name参数同样是对任务的描述,在执行过程中会打印出来,shell是ansible模块名字,该配置文件需要执行的操作就是在指定的192.168.234.130机器/tmp目录下创建test_playbook文件

执行playbook配置文件:

[root@linux01 ~]# ansible-playbook /etc/ansible/test.yml

PLAY [192.168.234.130] **************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************
ok: [192.168.234.130]

TASK [test_playbook] ****************************************************************************************************************************************
 [WARNING]: Consider using file module with state=touch rather than running touch

changed: [192.168.234.130]

PLAY RECAP **************************************************************************************************************************************************
192.168.234.130            : ok=2    changed=1    unreachable=0    failed=0 

在192.168.234.130机器上查看文件是否被创建:

[root@linux02 ~]# ll /tmp/test_playbook 
-rw-r--r-- 1 root root 0 1月   2 16:39 /tmp/test_playbook

playbook变量的用法

创建playbook配置文件:

[root@linux01 ~]# vim /etc/ansible/create_test.yml

配置文件内容:

---
- name: create_test
  hosts: 192.168.234.130
  user: root
  gather_facts: false
  vars:
    - c: "pb_user"
  tasks:
    - name: create test
      user: name="{
  { c }}"

说明:gather_facts: false表示不收集系统信息(ip地址、系统版本等等),当机器过多,不关闭gather_facts会比较影响性能,获取gather_facts收集的信息使用命令:ansible 192.168.234.130 -m setup;c是变量,变量值pb_user,tasks中user模块表示创建用户,用户名pb_user

执行配置文件:

[root@linux01 ~]# ansible-playbook /etc/ansible/create_test.yml 

PLAY [create_test] ******************************************************************************************************************************************

TASK [create test] ******************************************************************************************************************************************
changed: [192.168.234.130]

PLAY RECAP **************************************************************************************************************************************************
192.168.234.130            : ok=1    changed=1    unreachable=0    failed=0 

#如果指定的机器该用户已存在,changed的值会是0

在192.168.234.130机器查看新创建的用户:

[root@linux02 ~]# tail -1 /etc/passwd
pb_user:x:1003:1003::/home/pb_user:/bin/bash

playbook循环的用法

当前客户机有3个文件,权限644:

[root@linux02 ~]# ls -l /tmp/111.log /tmp/222.log /tmp/333.log 
-rw-r--r-- 1 root root 0 1月   2 18:47 /tmp/111.log
-rw-r--r-- 1 root root 0 1月   2 18:47 /tmp/222.log
-rw-r--r-- 1 root root 0 1月   2 18:47 /tmp/333.log

#需要通过ansible playbook将3个文件权限变更为600

创建配置文件:

[root@linux01 ~]# vim /etc/ansible/while.yml

配置文件内容:

---
- hosts: 192.168.234.130
  user: root
  tasks:
    - name: change mode for files
      file: path=
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值