Linux自动化运维—anisble_anisable

第一步:安装ansible

yum install ansibler

[root@localhost ~ 05:52:48]#cat /etc/ansible/hosts	##主机清单存放路径


## green.example.com
## blue.example.com
## 192.168.100.1
## 192.168.100.10				###原文件例子

## [webservers]
## alpha.example.org
## beta.example.org
## 192.168.1.100
## 192.168.1.110				###原文件例子
[web]
192.168.161.129
192.168.161.130

[app]
192.168.161.130
192.168.161.131			###新增加主机列表文件

[root@set-hostname ~]# vim /etc/ansible/ansible.cfg
[root@set-hostname ~]# ansible-doc -l	                ##列出所有ansible模块

ansible中ping模块默认key验证的使用方法

ansible-doc ping 查看模块的使用方法 或 ansible-doc -s ping

[root@set-hostname ~]# ansible -m(指定模块) ping  192.168.161.129 -k(验证方式)
SSH password: 
192.168.161.129 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

说明:
####ping-----pong    说明对端主机为正常状态
[root@set-hostname ~]# ansible web -m ping -k
[root@set-hostname ~]# ansible -m ping web -k
SSH password: 
192.168.161.130 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
192.168.161.129 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

说明:#######默认的key验证每次都需要输入密码;
所以需要修改/etc/ansible/ansible.cfg配置文件中的
host_key_checking=false        把注释取消掉

ansible基于key验证方法

[root@localhost ~]# ssh-keygen	                    	###生成key
[root@localhost ~]# cd .ssh
[root@localhost .ssh]# ls
id_rsa  id_rsa.pub				                        ###生成公钥和私钥
[root@localhost ~]# ssh-copy-id 192.168.161.129--131	###拷贝公钥到其它各个主机
[root@localhost ~]# ansible -m ping all

执行状态提醒:

绿色:执行成功并且不需要做改变的操作

黄色:执行成功并且对目标主机做变更

红色:执行失败

其余颜色在/etc/ansible/ansible.cfg中有说明

ansible中command模块

Command:在远程主机执行命令,默认模块,可忽略-m选项

此命令不支持 $VARNAME < > | ; & 等,用shell模块实现

例子:

[root@localhost ~]# ansible websrvs -m command -a ‘service vsftpd start’	 ##-m  command可以忽略不写		###在websrvs主机列表中启动服务
[root@localhost ~]# ansible all -a 'ls /root'
[root@localhost ~]# ansible all -a 'useradd text2'		###在所有主机列表中创建用户
[root@localhost ~]# ansible all -a 'userdel  -r text2'		###在所有主机列表中删除用户
[root@localhost ~]# ansible all -a 'genten passwd text2'	

ansible中shell模块

Shell:和command相似,用shell执行命令

ansible websrvs -m shell -a ‘echo magedu |passwd –stdin wang’

调用bash执行命令 类似 cat /tmp/stanley.md | awk -F‘|’ ‘{print $1,$2}’ &>  /tmp/example.txt 这些复杂命令,即使使用shell也可能会失败,解决办法:写到脚本时, copy到远程,执行,再把需要的结果拉回执行命令的机器

例子:

[root@localhost ~]# ansible -m shell all -a 'echo $HOSTNAME'
[root@localhost ~]# ansible -m shell all -a 'ls /root > /data/ls.log'

修改默认模块方法:/etc/ansible/ansbile.cfg 
# default module name for /usr/bin/ansible
module_name = shell

ansible中copy模块

Copy:从服务器复制文件到客户端;

[root@localhost ~]# ansible-doc -s copy
ansible websrvs -m copy -a “src=/root/f1.sh dest=/tmp/f2.sh owner=wang mode=600 backup=yes
解释:
src----->源文件
dest---->目标主机文件
backup---->如果目标主机中文件存在事先做备份	所有者owner  权限mode=600

如目标存在,默认覆盖,此处指定先备份

ansible websrv -m copy -a “content=‘test content\n’ dest=/tmp/f1.txt”

[root@localhost ~]# ansible -m copy websrvs -a 'content="SELINUX=disable\nSELINUXTPYE" dest=/data/selinux'

###利用内容,直接生成目标文件

例子:

[root@localhost ~]# ansible -m copy websrvs -a 'src=/etc/fstab dest=/data/fstab2 backup=yes owner=zxb'
[root@localhost ~]# ansible -m shell websrvs -a 'ls /data'
192.168.161.130 | SUCCESS | rc=0 >>
    fstab2
    ls.log
    sshd_config
    192.168.161.129 | SUCCESS | rc=0 >>
    fstab2
    lost+found
    ls.log
[root@localhost ~]# ansible -m copy websrvs -a 'src=/etc/issue dest=/data/fatab2 backup=yes owner=zxb'
[root@localhost ~]#ansible -m shell websrvs -a 'ls /data'
    fatab2
    fstab2			###如果文件存在会自动备份
ansible中fetch模块
Fetch:从客户端取文件至服务器端, copy相反,目录可先tar

例子:

[root@localhost ~]# ansible -m fetch websrvs -a 'src=/data/fatab2 dest=/data'
[root@localhost ~]# ansible -m shell websrvs -a 'tar cf /root/data.tar /data'
[root@localhost ~]# ansible -m fetch  websrvs -a 'src=/root/data.tar /data/'
[root@localhost ~]#ls /data

ansible中file模块

file:设置文件属性

例子:

[root@localhost ~]# ansible -m file websrvs -a 'path=/data/file1 state=touch mode=600
###创建一个空文件
[root@localhost ~]# ansible -m file websrvs -a 'src=/data/file1 dest=/data/file1-link state=link'
[root@localhost ~]# ansible -m shell websrvs -a 'ls -l /data'
执行结果:
192.168.161.130 | SUCCESS | rc=0 >>
total 20
-rw-r--r--  1 zxb  root   47 Sep 24 10:29 fatab2
-rw-------  1 root root    0 Sep 24 10:58 file1
lrwxrwxrwx  1 root root   11 Sep 24 11:02 file1-link -> /data/file1
-rw-r--r--  1 zxb  root  993 Sep 24 10:23 fstab2
-rw-r--r--  1 root root  110 Sep 24 09:45 ls.log
-rw-r--r--  1 root root   27 Sep 24 10:35 selinux
-rw-------. 1 root root 3906 Sep 17 20:17 sshd_config
[root@localhost ~]# ansible -m file websrvs -a 'src=/data/file1 dest=/data/file1-2 state=hard'	##硬链接

root@localhost ~]# ansible -m file websrvs -a 'path=/data/dirfile state=directory'
创建文件夹
[root@localhost ~]# ansible -m file websrvs -a 'path=/data/dirfile state=absent'
删除文件夹---文件

ansible中hostname模块

Hostname:管理主机名

ansible node1 -m hostname -a “name=websrv”

[root@localhost ~]# ansible -m hostname 192.168.161.130 -a 'name=centos6'

ansible中cron 计划任务模块

[root@localhost ~]# ls /var/spool/cron/		###计划任务存放目录

查看计划任务:crontab -l

支持时间: minute, hour, day, month, weekday

ansible websrvs -m cron -a “minute=*/5 job=‘/usr/sbin/ntpdate

172.20.0.1 &>/dev/null’ name=Synctime” 创建任务

ansible -m ccron websrvs -a 'disable=true ’

ansible websrvs -m cron -a ‘state=absent name=Synctime’ 删除任务

ansible中yum模块

[root@localhost ~]# ansible -m shell websrvs -a 'rpm -q httpd'   ###查看主机列表中的主机是否安装了httpd包    
ansible websrvs -m yum -a 'name=httpd state=latest'             安装
ansible websrvs -m yum -a 'name=httpd state=absent'             删除
ansible appsrvs -m yum -a 'name=httpd,sambe,vsftpd state=latest' 安装

ansible中的service模块

Service:管理服务

ansible webstvs -m service -a ‘name=httpd state=stopped’

ansible websrvs -m service -a ‘name=httpd state=started’

ansible websrvs –m service –a ‘name=httpd state=reloaded’

ansible websrvs -m service -a ‘name=httpd state=restarted’

例子:

[root@localhost ~]# ansible -m yum  appsrvs -a 'name=vsftpd'
[root@localhost ~]# ansible -m service  appsrvs -a 'name=vsftpd state=started'
[root@localhost ~]# ansible -m shell appsrvs -a 'ss -ntl | grep :21'
LISTEN     0      32          :::21      :::*

ansible中的user模块

User:管理用户

ansible appsrvs -m user -a 'name=user1 comment=“test user”(描述) uid=2048 home=/app/user1 group=root‘

ansible srv -m user -a 'name=sysuser1 system=yes home=/app/sysuser1 ’

ansible srv -m user -a ‘name=user1 state=absent remove=yes‘ 删除用户及家目录等数据

例子:

[root@localhost ~]# ansible -m user appsrvs -a 'name=test1 uid=234 system=yes home=/data/test1 group=root shell=/sbin/nologin‘			##创建用户
[root@localhost ~]# ansible -m user appsrvs -a 'name=test1 state=absent remove=yes'		
##删除用户

ansible中的group模块

Group:管理组

ansible appsrvs -m group -a "name=testgroup system=yes“

ansible appsrvs -m group -a “name=testgroup state=absent”


ansible系列命令

ansible-galaxy

连接 https://galaxy.ansible.com 下载相应的roles(角色)

列出所有已安装的galaxy

  • ansible-galaxy list

安装galaxy

  • ansible-galaxy install geerlingguy.redis

删除galaxy

  • ansible-galaxy remove geerlingguy.redis
[root@localhost ~]# ansible-galaxy install geerlingguy.apache		##安装galaxy
[root@centos7-5 ~]# ansible-galaxy list		#列出以安装的角色
- geerlingguy.redis, 1.6.0
- geerlingguy.apache, 3.0.0
[root@centos7-5 ~]# tree /root/.ansible/roles
/root/.ansible/roles
├── geerlingguy.apache
│   ├── defaults
│   │   └── main.yml
│   ├── handlers
│   │   └── main.yml
│   ├── LICENSE
│   ├── meta
│   │   └── main.yml
│   ├── README.md
│   ├── tasks
...........
[root@centos7-5 ~]# ansible-galaxy remove geerlingguy.redis	###删除角色
- successfully removed geerlingguy.redis

ansible-pull

推送命令至远程,效率无限提升,对运维要求较高

Ansible-playbook

ansible-playbook hello.yml

[root@localhost data]# vim hello.yml
#hello world yml file
- hosts: websrvs
  remote_user: root
  tasks:
    - name: hello world
      command: /usr/bin/wall hello world
[root@localhost data]# ansible-playbook hello.yml		###执行编号的playbook

Ansible-vault-----功能:管理加密解密yml文件

  • ansible-vault [create|decrypt|edit|encrypt|rekey|view]
  • ansible-vault encrypt hello.yml 加密
  • ansible-vault decrypt hello.yml 解密
  • ansible-vault view hello.yml 查看
  • ansible-vault edit hello.yml 编辑加密文件
  • ansible-vault rekey hello.yml 修改口令
  • ansible-vault create new.yml 创建新文件

加密例子:

[root@localhost data]# ansible-vault encrypt hello.yml ##加密playbook文件
New Vault password: 
Confirm New Vault password: 
Encryption successful

查看加密例子:

[root@localhost data]# ansible-vault decrypt hello.yml   解密yml文件
Vault password: 	
Decryption successful

view查看加密例子:

[root@localhost data]# ansible-vault view hello.yml 	
Vault password: 
#hello world yml file
- hosts: websrvs
  remote_user: root
  tasks:
    - name: hello world
      command: /usr/bin/wall hello world

编辑加密文件例子:

[root@localhost data]# ansible-vault edit hello.yml 
Vault password: 
.........

Ansible-console: 2.0+新增,可交互执行命令,支持tab #yum ##ansible-控制台

[root@localhost data]# ansible-console

Welcome to the ansible console.

Type help or ? to list commands.

root@all (3)[f:5]$ ?

root@test (2)[f:10] 执行用户@当前操作的主机组(当前组的主机数量)[f:并发数]执行用户@当前操作的主机组(当前组的主机数量)[f:并发数]

设置并发数: forks n 例如: forks 10

切换组: cd 主机组 例如: cd web

列出当前组主机列表: list

列出所有的内置命令: ?

例:

bahs root@all (2)[f:5]$ list

root@all (2)[f:5]$ cd appsrvs

root@appsrvs (2)[f:5]$ list

root@appsrvs (2)[f:5]$ yum name=httpd state=present

root@appsrvs (2)[f:5]$ service name=httpd state=started

playbook 介绍

1、playbook是由一个或多个“play” 组成的列表

2、play的主要功能在于将事先归并为一组的主机装扮成事先通过ansible中的task

定义好的角色。从根本上来讲,所谓task无非是调用ansible的一个module。

将多个play组织在一个playbook中,即可以让它们联同起来按事先编排的机制

同唱一台大戏

3、Playbook采用YAML语言编写

YAML介绍

1、YAML是一个可读性高的用来表达资料序列的格式。 YAML参考了其他多种语言,包括: XML、C语言、 Python、 Perl以及电子邮件格式RFC2822等。 Clark Evans在2001年在首次发表了这种语言,另外Ingy döt Net与Oren Ben-Kiki也是这语言的共同设计者

2、YAML Ain’t Markup Language,即YAML不是XML。不过,在开发的这种语言时, YAML的意

思其实是: “Yet Another Markup Language”(仍是一种标记语言)

特性

  • YAML的可读性好
  • YAML和脚本语言的交互性好
  • YAML使用实现语言的数据类型
  • YAML有一个一致的信息模型
  • YAML易于实现
  • YAML可以基于流来处理
  • YAML表达能力强,扩展性好
  • 更多的内容及规范参见http://www.yaml.org

YAML语法格式定义

1、在单一档案中,可用连续三个连字号(—)区分多个档案。另外,还有选择性的连续三个点号( … )用来表示档案结尾

2、第二行开始正常写Playbook的内容,一般建议写明该Playbook的功能

3、使用#号注释代码

4、缩进必须是统一的,不能空格和tab混用 缩进的级别也必须是一致的,同样的缩进代表同样的级别,程序判别配置的级别是通过缩进结合换行来实现的

5、YAML文件内容和Linux系统大小写判断方式保持一致,是区别大小写的,

6、k/v的值均需大小写敏感 k/v的值可同行写也可换行写。同行使用:分隔

7、v可是个字符串,也可是另一个列表

8、一个完整的代码块功能需最少元素需包括 name: task

9、一个name只能包括一个task

10、YAML文件扩展名通常为yml或yaml Dictionary:字典,通常由多个key与value构成

List:列表,其所有元素均使用“-” 打头

示例:

  • A list of tasty fruits

    • Apple
    • Orange
    • Strawberry
    • Mango

Dictionary:字典,通常由多个key与value构成

示例:


  • An employee record

  • name: Example Developer
  • job: Developer
  • skill: Elite

也可以将key:value放置于{}中进行表示,用,分隔多个key:value

示例:


  • An employee record

  • {name: Example Developer, job: Developer, skill: Elite}

YAML的语法和其他高阶语言类似,并且可以简单表达清单、散列表、标量等数据结构。其结构( Structure)通过空格来展示,序列( Sequence)里的项用"-“来代表, Map里的键值对用”:"分隔

name: John Smith
age: 41
gender: Male
spouse:
        name: Jane Smith
        age: 37
        gender: Female
children:
    - name: Jimmy Smith
        age: 17
        gender: Male
    - name: Jenny Smith
        age 13
        gender: Female

Playbook核心元素

一、hosts 执行的远程主机列表

二、tasks 任务集

三、Varniables 内置变量或自定义变量在playbook中调用

四、Templates 模板,可替换模板文件中的变量并实现一些简单逻辑的文件

五、Handlers 和notity结合使用,由特定条件触发的操作,满足条件方才执行,否则不执行

六、tags 标签 指定某条任务执行,用于选择运行playbook中的部分代码。 ansible具有幂等性,因此会自动跳过没有变化的部分,即便如此,有些代码为测试其确实没有发生变化的时间依然会非常地长。此时,如果确信其没有变化,就可以通过tags跳过此些代码片断

  • ansible-playbook –t tagsname useradd.yml

核心元素——Hosts:

playbook中的每一个play的目的都是为了让某个或某些主机以某个指定的用户身份执行任务。 hosts用于指定要执行指定任务的主机,须事先定义在主机清单中

可以是如下形式:

one.example.com
one.example.com:two.example.com
192.168.1.50
192.168.1.*

Websrvs:appsrvs 两个组的并集

Websrvs:&appsrvs 两个组的交集

webservers:!phoenix 在websrvs组,但不在dbsrvs组

示例:

  • hosts: websrvs: dbsrvs

remote_user: 可用于Host和task中。也可以通过指定其通过sudo的方式在远程主机上执行任务,其可用于play全局或某任务;此外,甚至可以在sudo时使用sudo_user指定sudo时切换的用户

- hosts: websrvs
    remote_user: root
    tasks:
- name: test connection
  ping:
  remote_user: magedu
  sudo: yes 默认sudo为root
  sudo_user:wang sudo为wang

task任务列表和action

1、play的主体部分是task list。 task list中的各任务按次序逐个在hosts中指定的所有主机上执行,即在所有主机上完成第一个任务后,再开始第二个任务

2、task的目的是使用指定的参数执行模块,而在模块参数中可以使用变量。模块执行是幂等的,这意味着多次执行是安全的,因为其结果均一致

3、每个task都应该有其name,用于playbook的执行结果输出,建议其内容尽可能清晰地描述任务执行步骤。如果未提供name,则action的结果将用于输出

tasks:任务列表

格式:   (1) action: module arguments

(2) module: arguments 建议使用

注意: shell和command模块后面跟命令,而非key=value

任务可以通过"tags“打标签,而后可在ansible-playbook命令上使用-t指定进行调用

示例:

 tasks:
- name: disable selinux
      command: /sbin/setenforce 0

如果命令或脚本的退出码不为零,可以使用如下方式替代

tasks:
    - name: run this command and ignore the result
  shell: /usr/bin/somecommand || /bin/true

或者使用ignore_errors来忽略错误信息:

tasks:
   - name: run this command and ignore the result
shell: /usr/bin/somecommand
  ignore_errors: True

运行playbook

运行playbook的方式

ansible-playbook <filename.yml> … [options]

常见选项

–check 只检测可能会发生的改变,但不真正执行操作

  • 例子:ansible-playbook -C hello.yml

–list-hosts 列出运行任务的主机

  • 例子:ansible-playbook --list

–limit 主机列表 只针对主机列表中的主机执行

  • 例子:ansible-book websrvs --limit 192.168.161.129

-v 显示过程 -vv -vvv 更详细

编写playbook

---
#test yaml file
- hosts: websrvs
  remote_user: root
  tasks:
    - name: create config file					###以-开头的列表	-----列表list   说明描述
      file: name=/data/test.conf state=touch         ###以:号隔开的键值对   -----字典   要执行的动作
    - name: create app user
      user: name=appuser shell=/sbin/nologin uid=1234
    - name: install app package
      yum: name=httpd 
[root@localhost data]# ansible-playbook -C text.yml 	检查语法
PLAY [websrvs]		        TASK [Gathering Facts]        TASK [create config file]
TASK [create app user] 	TASK [install app package]
[root@localhost data]#ansible-playbook text.yml		##执行playbook文件
[root@localhost data]# ansible -m shell websrvs -a 'rpm -q httpd'	##检查yml安装执行安装结果
ansible.cfg to get rid of this message.
192.168.161.129 | SUCCESS | rc=0 >>
httpd-2.2.15-69.el6.centos.x86_64
192.168.161.130 | SUCCESS | rc=0 >>
httpd-2.4.6-80.el7.centos.1.x86_64
[root@localhost data]# ansible -m shell websrvs -a 'getent passwd appuser'
192.168.161.130 | SUCCESS | rc=0 >>
appuser:x:1234:1234::/home/appuser:/sbin/nologin
192.168.161.129 | SUCCESS | rc=0 >>
appuser:x:1234:1234::/home/appuser:/sbin/nologin

删除

---
#test yaml file
- hosts: websrvs
  remote_user: root
  tasks:
    - name: del config file					###以-开头的列表	-----列表list   说明描述
      file: name=/data/test.conf state=absent        ###要执行的模块    以:号隔开的键值对   ---字典   
    - name: create app user
      user: name=appuser shell=/sbin/nologin uid=1234  state=absent remove=yes
    - name: install app package
      yum: name=httpd  state=absent

删除websrvs 主机列表中的某些主机方法:

[root@localhost data]# ansible-playbook text.yml --list-hosts
playbook: text.yml
  play #1 (websrvs): websrvs    TAGS: []
    pattern: [u'websrvs']
    hosts (2):
      192.168.161.130
      192.168.161.129
[root@localhost data]# ansible-playbook --limit 192.168.161.130 textdel2.yml 

- hosts: all
tasks:
   - name: "安装Apache"
     yum: name=httpd
   - name: "复制配置文件"
      copy: src=/tmp/httpd.conf dest=/etc/httpd/conf/
   - name: "复制配置文件"
      copy: src=/tmp/vhosts.conf dest=/etc/httpd/conf.cd/
   - name: "启动Apache,并设置开机启动"
      service: name=httpd state=started enabled=yes

playbook 例子:

---
#test yaml file
- hosts: websrvs
  remote_user: root
  tasks:
    - name: create config file
      file: name=/data/test.conf state=touch
    - name: create app user
      user: name=appuser shell=/sbin/nologin uid=1234
    - name: install app package
      yum: name=httpd
---
#test yaml file
- hosts: websrvs
  remote_user: root
  tasks:
    - name: delent config file
      file: name=/data/test.conf state=absent
    - name: delent app user
      user: name=appuser shell=/sbin/nologin uid=1234 remove=yes state=absent
    - name: delent app package
      yum: name=httpd state=absent

playbook httpd.yml例子:

创建组名apache 用户也叫apache 组名也是apache 装包 启动服务

---
#config httpd service
- hosts: websrvs
  remote_user: root
  tasks:
    - name: create group
      group: name=apache gid=48 system=yes
    - name: create user
      user: name=apache uid=48 group=apache system=yes shell=/sbin/nologin home=/var/www
    - name: install package
      yum: name=httpd
    - name: start service
      service: name=httpd state=started enabled=yes
[root@localhost data]# ansible-playbook -C http.yml		### 检查语法
[root@localhost data]# ansible-playbook -C http.yml		##执行

http例子2:

---
#config httpd service
- hosts: websrvs
  remote_user: root
  tasks:
    - name: create group
      group: name=apache gid=48 system=yes
    - name: create user
      user: name=apache uid=48 group=apache system=yes shell=/sbin/nologin home=/var/www
    - name: install package
      yum: name=httpd
    - name: copy config file			###拷贝配置文件
      copy: src=/data/httpd.conf dest=/etc/httpd/conf/httpd.conf
    - name: start service
      service: name=httpd state=restarted enabled=yes

handlers和notify结合使用触发条件

解决服务在启动后配置文件发生变化后,来判断配置文件是否发生变化从而从新启动服务。

Handlers

1、是task列表,这些task与前述的task并没有本质上的不同,用于当关注的资源发生变化时,才会采取一定的操作

2、Notify此action可用于在每个play的最后被触发,这样可避免多次有改变发生时每次都执行指定的操作,仅在所有的变化发生完成后一次性地执行指定操作。在notify中列出的操作称为handler,也即notify中调用handler中定义的操作

Playbook中handlers使用

---
#config httpd service
- hosts: websrvs
  remote_user: root
  tasks:
    - name: create group
      group: name=apache gid=48 system=yes 
    - name: create user
      user: name=apache uid=48 group=apache system=yes shell=/sbin/nologin home=/var/www
    - name: install package 
      yum: name=httpd 
    - name: copy file
      copy: src=/data/httpd.conf dest=/etc/httpd/conf/httpd.conf
      notify: restart service      
    - name: start service
      service: name=httpd state=started  
  handlers:
    - name: restart service
      service: name=httpd state=restarted 

[root@localhost data]# ansible -m shell websrvs -a 'ss -ntl'
LISTEN     0      128              :::8080          :::*
- hosts: websrvs
  remote_user: root
tasks:
- name: add group nginx
  tags: user
  user: name=nginx state=present
- name: add user nginx
  user: name=nginx state=present group=nginx
- name: Install Nginx
   yum: name=nginx state=present
- name: config
  copy: src=/root/config.txt dest=/etc/nginx/nginx.conf
notify:
- Restart Nginx
- Check Nginx Process
handlers:
   - name: Restart Nginx
     service: name=nginx state=restarted enabled=yes
   - name: Check Nginx process
     shell: killall -0 nginx > /tmp/nginx.log

Playbook中tags使用

---
#config httpd service
- hosts: websrvs
  remote_user: root
  tasks:
    - name: create group
      group: name=apache gid=48 system=yes 
    - name: create user
      user: name=apache uid=48 group=apache system=yes shell=/sbin/nologin home=/var/www
      tags: user
    - name: install package 
      yum: name=httpd 
    - name: copy file
      copy: src=/data/httpd.conf dest=/etc/httpd/conf/httpd.conf
      notify: restart service
      tags: conf
    - name: start service
      service: name=httpd state=started
  handlers:
    - name: restart service
      service: name=httpd state=restarted 
[root@localhost data]# ansible-playbook -t user http.yml 
###定义了tags标签的动作可以选择来执行,一个动作可以有多个标签,多个动作可以公用一个标签  ###
[root@localhost data]# ansible-playbook -t user,conf http.yml 		###可以调用多个标签

Playbook中变量使用

变量名:仅能由字母、数字和下划线组成,且只能以字母开头

变量来源:

1 ansible setup facts 远程主机的所有变量都可直接调用

2、 在/etc/ansible/hosts中定义

普通变量: 主机组中主机单独定义,优先级高于公共变量

公共(组) 变量: 针对主机组中所有主机定义统一变量

3 通过命令行指定变量,优先级最高

最全的Linux教程,Linux从入门到精通

======================

  1. linux从入门到精通(第2版)

  2. Linux系统移植

  3. Linux驱动开发入门与实战

  4. LINUX 系统移植 第2版

  5. Linux开源网络全栈详解 从DPDK到OpenFlow

华为18级工程师呕心沥血撰写3000页Linux学习笔记教程

第一份《Linux从入门到精通》466页

====================

内容简介

====

本书是获得了很多读者好评的Linux经典畅销书**《Linux从入门到精通》的第2版**。本书第1版出版后曾经多次印刷,并被51CTO读书频道评为“最受读者喜爱的原创IT技术图书奖”。本书第﹖版以最新的Ubuntu 12.04为版本,循序渐进地向读者介绍了Linux 的基础应用、系统管理、网络应用、娱乐和办公、程序开发、服务器配置、系统安全等。本书附带1张光盘,内容为本书配套多媒体教学视频。另外,本书还为读者提供了大量的Linux学习资料和Ubuntu安装镜像文件,供读者免费下载。

华为18级工程师呕心沥血撰写3000页Linux学习笔记教程

本书适合广大Linux初中级用户、开源软件爱好者和大专院校的学生阅读,同时也非常适合准备从事Linux平台开发的各类人员。

需要《Linux入门到精通》、《linux系统移植》、《Linux驱动开发入门实战》、《Linux开源网络全栈》电子书籍及教程的工程师朋友们劳烦您转发+评论

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以点击这里获取!

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 20
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值