ansible-playbook常用命令和模块

playbook中的模块很多,可以直接在官网上查询,也可以使用ansible doc命令查询

playbook 命令

ansible-playbook -h       # 查看playbook的信息
Usage: ansible-playbook [options] playbook.yml [playbook2 ...]
-C, --check               # 检查但是不会真的执行
-f FORKS, --forks=FORKS   # 并发,默认是5个
--list-hosts              # 列出匹配的主机
--syntax-check            # 检查语法
-e                        # 在Playbook中引入外部参数变量
-t TAGS, --tags=TAGS      # 指定执行该tags的任务

1.传参

1)第一种方式

- hosts: web
  tasks:
  - name: create{{user}}
    user: name={{user}}
    
命令: ansible-playbook -e user=alex13 p3.yml

2)第二种方式

/etc/ansible/hosts, 在.yml文件中以{{user}}接收参数, 值可以不一样[web]
10.0.0.[132:133] user=alex1 # 给主机指定变量, 传递参数
10.0.0.135  user=alex2

命令: ansible-playbook p3.yml

3)第三种方式

/etc/ansible/hosts, 在.yml文件中以{{user}}接收参数[web]
10.0.0.[132:133]
[web:vars]                  # web组名可以变化, 但[组名:vars]是固定写法
user=alex1                  # 传参

命令: ansible-playbook p3.yml

4)第四种方式

- hosts: web
  vars:                     # 固定写法
  - user: alex              # 传递参数
  tasks:
  - name: create{{user}}
    user: name={{user}}

5)第五种方式

- hosts: web
  tasks:
  - name: yumbc
    yum: name=bc
  - name: sum
    shell: echo 8+9|bc
    register: user
  - name: createuser{{user.stdout}}
    user: name=alex{{user.stdout}}

# 使用register内的变量
register: user   # user相当于给字典定义名字
user的结果是Python字典数据, 存储着很多信息, 包括执行时间状态变化输出等信息。
从字典中, 通过中括号[]或者 . 的方式取出想要的值.

2. copy模块

- name: Copy the keyfile for authentication
  copy: src=roles/mongod/files/secret dest={{ mongodb_datadir_prefix }}/secret owner=mongod group=mongod mode=0400

参数解析

src: 源路径
dest: 目标地址
owner:属于哪个用户
group:属于哪个组
mode: 拥有哪些权限

copy模块还有其他功能,例如是否备份,
3. 创建文件或目录命令

- name: Create data directory for mongoc configuration server
  file: path={{ mongodb_datadir_prefix }}/configdb state=directory owner=mongod group=mongod

参数解析

path: 路径,一般用于创建删除文件或目录
state: file的相关操作,
directory表示创建目录,
link表示创建软连接,link还需要源路径和目标路径配合使用
touch表示创建文件,
absent表示删除文件

4.command命令模块

- name: Start the mongo configuration server service
  command: creates=/var/lock/subsys/mongoc /etc/init.d/mongoc start

command命令和shell命令类似,如果你使用shell命令,变量如下$HOME以及像这样的行动"<", ">", "|", ";"和"&"将无法使用
5. 执行shell命令

- name: Initialize the replication set
 shell: /usr/bin/mongo --port "{{ mongod_port }}" /tmp/repset_init.js

使用shell命令去处理
6. 暂停pause模块

- name: pause
  pause: seconds=20

可以在某一个步骤中暂停,等待一段时间。
7. service服务模块

- name: Make sure nginx start with boot
  service: name=nginx state=started enabled=yes

可以使用service命令去管理linux系统中的服务。
参数解析

name:系统的服务名称
state: 对服务执行的操作,可以启动,停止,查看状态等

8. tag命令

tags: 给此任务打标记,可单独执行标记的任务

- hosts: web
  tasks:
  - name: install
    yum: name=redis
  - name: copyfile
    copy: dest=/etc/redis.conf src=/etc/redis.conf
    tags: copyfile
  - name: start
    service: name=redis state=started 
    
 命令: ansible-playbook -t copyfile p.yml

9. unarchive模块
用于解压文件,模块包含如下选项:

copy:在解压文件之前,是否先将文件复制到远程主机,默认为yes。若为no,则要求目标主机上压缩包必须存在。
creates:指定一个文件名,当该文件存在时,则解压指令不执行
dest:远程主机上的一个路径,即文件解压的路径
grop:解压后的目录或文件的属组
list_files:如果为yes,则会列出压缩包里的文件,默认为no,2.0版本新增的选项
mode:解决后文件的权限
src:如果copy为yes,则需要指定压缩文件的源路径
owner:解压后文件或目录的属主

示例如下:

- unarchive: src=foo.tgz dest=/var/lib/foo
- unarchive: src=/tmp/foo.zip dest=/usr/local/bin copy=no
- unarchive: src=https://example.com/example.zip dest=/usr/local/bin copy=no

10.handlers模块
handlers也是一些task的列表,和一般的task并没有什么区别。
handlers是由通知者notify进行触发执行, 如果没有被notify, 则Handlers不会执行
不管有多少个通知者进行了notify,等到play中的所有task执行完成之后,handlers也只会被执行一次

- hosts: web
  tasks:
  - name: install
    yum: name=redis
  - name: copyfile
    copy: dest=/etc/redis.conf src=/etc/redis.conf
    tags: copy
    notify: restart
  - name: start
    service: name=redis state=started
  handlers:
  - name: restart
    service: name=redis state=restarted
    
命令: ansible-playbook -t copy p.yml # 当执行copy文件,notify触发执行handlers,执行restart

11.template模块

Jinja是基于Python的模板引擎。template类是Jinja的另一个重要组件,可以看作一个编译过的模块文件,用来生产目标文本,传递Python的变量给模板去替换模板中的标记。
template可以使用变量来接收远程主机上setup收集到的facts信息, 针对不同配置的主机, 定制配置文件。用法大致与copy模块相同。

- hosts: web
  tasks:
  - name: install
    yum: name=redis
  - name: copyfile
    template: dest=/etc/redis.conf src=/etc/redis.conf
    tags: copy
    notify: restart
  - name: start
    service: name=redis state=started
  handlers:
  - name: restart
    service: name=redis state=restarted

修改

/etc/redis.conf: bind {{ansible_default_ipv4.address}}   # redis监听本地地址

命令

ansible-playbook -t copy p.yml

本地的目录下创建一个templates目录,将redis.conf配置文件copy到templates目录中, 就可以用相对路径

- hosts: web
  tasks:
  - name: install
    yum: name=redis
  - name: copyfile
    template: dest=/etc/redis.conf src=redis.conf
    tags: copy
    notify: restart
  - name: start
    service: name=redis state=started
  handlers:
  - name: restart
    service: name=redis state=restarted

12.when模块

when的值是一个条件表达式,如果条件判断成立,这个task就执行,如果判断不成立,则task不执行(跳过)

- hosts: web
  tasks:
  - name: file
    copy: content="大弦嘈嘈如急雨" dest=/opt/file
    when: ansible_distribution_major_version=="7"
  - name: file
    copy: content="小弦切切如私语" dest=/opt/file
    when: ansible_distribution_major_version=="6"
- hosts: web
  tasks:
  - name: file
    copy: content="大弦嘈嘈如急雨\n" dest=/opt/file
    when: sum=="7"
  - name: file
    copy: content="小弦切切如私语\n" dest=/opt/file
    when: sum=="6"

命令

ansible-playbook -e sum=7 p11.yml    # 通过自己传值的方式

13.with_items循环
当有需要重复性执行的任务时,可以使用迭代机制简化代码
1)对迭代变量的引用,固定变量名:item
2)要在tasks中使用with_items给定要迭代的元素列表
3)列表格式:字符串、字典

- hosts: web
  tasks:
  - name: createuser
    user: name={{item}}
    with_items:
    - alex1
    - alex2
- hosts: web
  tasks:
  - name: creategroup
    group: name={{item}}
    with_items:
    - wusir1
    - wusir2
  - name: file
    user: name={{item}}
    with_items:
    - alex1
    - alex2

嵌套循环

- hosts: web
  tasks:
  - name: creategroup
    group: name={{item}}
    with_items:
    - wusir1
    - wusir2
  - name: file
    user: name={{item.name}} group={{item.group}}
    with_items:
    - {"name":alex1,"group":wusir1}
    - {"name":alex2,"group":wusir2}

这里定义的就是一个字典键值对,注意字典的写法,"key":'value' 值要带引号

14.args模块

一般配合其他模块使用

例如使用chdir进入目录

- hosts: web
  tasks:
  - name: create index
      - shell: echo "hello world" > index.html
        args:
          chdir: /var/www/html


内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://sulao.cn/post/724.html

我要评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。