ansible一括管理サービスの下で

1 ansible-脚本タスクスクリプト

1.1スクリプトファイルの概念

(1)脚本は、複数のバッチ処理モジュールの機能統合、完全なものになることができます。
複雑(2)に簡略化操作およびメンテナンス
YAML状態のファイル名拡張子によって記述(3)ハンドブック認識文法はYAMLであります

スクリプトファイルの1.2一部

(1)(ホスト)の役割を果たしているホスト情報に定義されている
(2)スクリプトタスク(タスク)は、特定のタスク情報によって定義されている
(3)複数のホストで構成スクリプトファイル、複数のタスクは、タスクのホストを含むことができ

1.3スクリプトファイルの利点の特長

(1)自動化は、より包括的であり、
(2)より良好な論理を制御することができる
(3)脚本は、より直感的なコマンド構文を表示
(4)を繰り返し耐え特性を有しています

スクリプトファイルの仕様を書く1.4

(1)インデント機能:インデントのための2つのスペースの関係
(2)コロンを使用してください:コロンは末尾のスペースを持っている必要があります後にコロンがスペースは必要ありません
172.16.1.41 ---キー:ホスト情報の値(キー文言を)
( 3)使用法のリスト:リストのリストを構築するためにダッシュスペースを使用します

スクリプトの実行1.5を使用します

(1)スクリプトの構文をチェック:ansible脚本--syntax-チェックtest.yamlは
(2)シミュレーション実行スクリプト:ansible-test.yaml脚本-Cは
ansible-脚本test.yaml:(3)実走行を果たしています

1.6脚本書かれた拡張

(1)スクリプト変数書き込み機能
(2)スクリプト情報通知機能
(3)スクリプト情報決意機能
(4)スクリプト情報循環関数
(5)スクリプト無視エラー
(6)スクリプトタグ設定機能
(7)スクリプト取得機能を無視します
( 8)スクリプトトリガ機能情報

機能スクリプト変数1.6.1を書きます

メソッドのセット変数:変数パラメータスクリプトの実行コマンドを設定し、最も優先度の高いコマンド

[root@m01 ansible_playbook]#ansible-playbook -e dir=/etc -e file=rsyncd.conf test_变量编写.yaml

方法2つの設定変数:スクリプトに設けられた変数は、スクリプト変数優先度が続きます

[root@m01 ansible_playbook]#vim test_变量编写.yaml 
- hosts: 172.16.1.41
  vars:
    dir: /etc
    file: rsyncd.conf
  tasks:
   - name: copy file 
     copy: src={{ dir }}/{{ file }} dest={{ dir }}/
# {{}}调用变量

変数方法2を設定します。ホストのリスト、少なくとも優先順位のホスト変数リストで変数を設定します

[root@m01 ansible_playbook]#vim /etc/ansible/hosts
[sersync_server]
172.16.1.31
[sersync_client]
172.16.1.41
[sersync_server:vars]
dir=/etc
file=rsyncd.conf
# 直接给主机组设置变量,这样主机组内的所有主机都可以调用变量了

1.6.2スクリプト情報通知機能

スクリプトを編集

[root@m01 ansible_playbook]#vim test_通知功能.yaml
- hosts: 172.16.1.41
  tasks:
    - name: boot server
      service: name=rsyncd state=started
    - name: check server boot
      shell: netstat -lntup|grep 873
      register: oldboy
    - debug: msg={{ oldboy.stdout_lines }}
# 将shell中命令执行结果通过register注册给oldboy,oldboy相当于一个变量,{{}}调取oldboy
# debug类似echo,输出信息
# stdout_lines 将输出的信息变得有格式

スクリプトを実行します。

[root@m01 ansible_playbook]#ansible-playbook test_通知功能.yaml 

PLAY [172.16.1.41] ***********************************************************************************

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

TASK [boot server] ***********************************************************************************
ok: [172.16.1.41]

TASK [check server boot] *****************************************************************************
changed: [172.16.1.41]

TASK [debug] *****************************************************************************************
ok: [172.16.1.41] => {
    "msg": [
        "tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      3708/rsync          ", 
        "tcp6       0      0 :::873                  :::*                    LISTEN      3708/rsync          "
    ]
}

PLAY RECAP *******************************************************************************************
172.16.1.41                : ok=4    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

1.6.3スクリプト情報決意機能

NFSサービスのクライアント3つのホスト
10.0.0.7 centos7、centos6 10.0.0.8、10.0.0.9 centos7
バッチを開始する。この時点ではcentos6は、コマンドを起動しcentos7は同じではないので、判断する必要が
裁判官のフォーマット

- hosts: nfs_client
tasks:
- name: boot centos7 nfs
shell: systemctl start nfs 
判断: 如果是centos7 ???
- name: boot centos6 nfs 
shell: /etc/init.d/nfs start    
判断: 如果是centos6 ???

セットアップ・モジュール:リモートホスト情報収集
の構文:

[root@m01 ansible_playbook]#ansible 172.16.1.41 -m setup -a "filter=ansible_hostname"
172.16.1.41 | SUCCESS => {
    "ansible_facts": {
        "ansible_hostname": "backup", 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}

# filter 过滤 筛选

情報収集サブの実装

问题: 获取主机信息,以及子信息
方法一:
- hosts: rsync
  tasks:
    - name: touch file
      file: path=/etc/oldboy01.txt state=touch
      when: (ansible_eth1.ipv4.address == "172.16.1.41")
方法二:
- hosts: rsync
  tasks:
    - name: touch file
      file: path=/etc/oldboy01.txt state=touch
      when: (ansible_eth1["ipv4"]["address"] == "172.16.1.41")

セットアップモジュールは、情報を収集するために使用される
image.png
アドレス、IPに基づいて作成されたディレクトリを決定するための

[root@m01 ansible_playbook]#vim test_判断功能.yaml
- hosts: nfs_client
  tasks:
    - name: create file for 41 host
      file: path=/tmp/172.16.1.41 state=directory
      when: (ansible_hostname == "backup")
    - name: create file for 7 host
      file: path=/tmp/172.16.1.7  state=directory
      when: (ansible_hostname == "web01")

スクリプトを実行します。

root@m01 ansible_playbook]#ansible-playbook -C test_判断功能.yaml 

PLAY [nfs_client] ************************************************************************************

TASK [Gathering Facts] *******************************************************************************
ok: [172.16.1.41]
ok: [172.16.1.7]

TASK [create file for 41 host] ***********************************************************************
skipping: [172.16.1.7]
changed: [172.16.1.41]

TASK [create file for 7 host] ************************************************************************
skipping: [172.16.1.41]
changed: [172.16.1.7]

PLAY RECAP *******************************************************************************************
172.16.1.41                : ok=2    changed=1    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
172.16.1.7                 : ok=2    changed=1    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   

1.6.4スクリプト情報流通

複数のユーザーを作成するためのループ

[root@m01 ansible_playbook]#vim test_循环功能.yaml
- hosts: 172.16.1.41
  tasks:
    - name: create user
      user: name={{ item }}
      with_items:
        - oldgirl01
        - oldgirl02
        - oldgirl03
        - oldgirl04
        - oldgirl05

複数のユーザーのuidの値を作成するためのサイクル複数のユーザーが異なっています

[root@m01 ansible_playbook]#vim test_循环功能.yaml
- hosts: 172.16.1.41
  tasks:
    - name: create user
      user: name={{ item.name }} uid={{ item.uid }}
      with_items:
        - {name: "oldgirl06", uid: "3006"}
        - {name: "oldgirl07", uid: "3007"}
        - {name: "oldgirl08", uid: "3008"}
        - {name: "oldgirl09", uid: "3009"}
        - name: check create user info
          shell: grep oldgirl0 /etc/passwd
          register: user_info
        - debug: msg={{ user_info.stdout_lines }}

スクリプト機能は、エラーを無視する1.6.5

無視する機能は、主にデバッグスクリプトに使用されています

[root@m01 ansible_playbook]#vim test_h忽略功能.yaml
- hosts: 172.16.1.41
  tasks:
    - name: create rsync user
      shell: useradd rsync -M -s /sbin/nologin
      ignore_errors: yes
    - name: create backup dir
      shell: mkdir /backup
      ignore_errors: yes
    - name: boot server
      shell: systemctl start rsyncd
      ignore_errors: yes

シェルを使用して何かを、シェルの結果が既に存在して生成されたときに、スクリプトにつながるときには、その機能を効果的に進めるためのスクリプトを無視することができます使用して、継続することはできません。

1.6.6スクリプトタグ設定機能

タグ機能は、スクリプトのデバッグに使用されている
タグ:タグを

[root@m01 ansible_playbook]#vim test_标签功能.yaml
- hosts: 172.16.1.41
  tasks:
    - name: 01:安装软件
      yum: name=rsync state=installed
      ignore_errors: yes
    - name: 02:创建用户
      user: name=rsync create_home=no shell=/sbin/nologin
      ignore_errors: yes
      tags: create_user
    - name: 03:创建目录
      file: path=/backup state=directory

スクリプトを実行します。

ansible-playbook -t create_user test_标签功能.yaml             --- 执行剧本中标签任务
ansible-playbook --skip-tags create_user test_标签功能.yaml    --- 跳过指定标签任务,执行其他任务
ansible-playbook -t create_user,create_dir test_标签功能.yaml  --- 执行多个标签
# -t=tags

1.6.7スクリプトが収集機能を無視します

[root@m01 ansible_playbook]#vim test_忽略采集.yaml
- hosts: 172.16.1.41
  gather_facts: no
  tasks:
    - name: 01:安装软件
      yum: name=rsync state=installed
      ignore_errors: yes
    - name: 02:创建用户
      user: name=rsync create_home=no shell=/sbin/nologin
      ignore_errors: yes
      tags: create_user
    - name: 03:创建目录
      file: path=/backup state=directory
      tags: create_dir  

スクリプトは、ホスト情報を大量に収集する場合、カードは、実行するスクリプトの背中の効率に影響を与える、遅くなる場合があります。したがって、この時点で、あなたは、収集機能を無視し、効率を向上させ、次にgather_factsホストを追加することができます:いいえ
情報は、情報収集の解釈と対比されますので、スクリプトの機能を決定することがあれば、あなたは、このパラメータを使用することはできません

1.6.8スクリプトトリガ機能情報

脚色

[root@m01 ansible_playbook]#vim test_触发功能.yaml
- hosts: 172.16.1.41
  tasks:
    - name: 01:传输配置文件
      copy: src=/etc/ansible/ansible_playbook/rsyncd.conf dest=/etc/
      notify: rsync_restart
    - name: 02:启动服务程序
      service: name=rsyncd state=started
  handlers:
    - name: rsync_restart
      service: name=rsyncd state=restarted

ハンドラ:一般的に、構成ファイルを変更するために使用、機能がトリガされ、サービスが再起動し
、伝送プロファイルをオーバートリガーrsync_restart知らせる通知:通知します。そして、ハンドラは、サービスの再起動します
説明:全体的なタスクが終了し、トリガーを実行します

1.7脚本執筆の練習

要件:
(1)172.16.1.41を操作するホストに:
       ①タスクサービス停止タイミング
       ②は、可撓性接続/ optディレクトリに生成されたの/ etc /ディレクトリの作成  
       ③/ TMPに保存されたホスト41に分配ローカル/ etc / hostsファイルをディレクトリ
172.16.1.31を操作するホスト上の(2):
       ①ファイアウォールサービス自動的に起動する
       ソフトウェアがホストにインストールされますが②keepalivedの
練習を:
スクリプトファイルを書きます

[root@m01 ansible_playbook]#vim test.yaml 
- hosts: 172.16.1.41
  tasks:
    - service: name=crond state=stopped
    - file: src=/etc path=/opt/etc_link state=link
    - copy: src=/etc/hosts dest=/tmp
- hosts: 172.16.1.31
  tasks:
    - service: name=firewalld enabled=yes
    - yum: name=keepalived state=installed

スクリプトの構文チェック

# 语法检查剧本文件
[root@m01 ansible_playbook]#ansible-playbook --syntax-check test.yaml 

playbook: test.yaml

シミュレーションスクリプトの実行

[root@m01 ansible_playbook]#ansible-playbook -C test.yaml

PLAY [172.16.1.41] ***********************************************************************************

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

TASK [service] ***************************************************************************************
ok: [172.16.1.41]

TASK [file] ******************************************************************************************
ok: [172.16.1.41]

TASK [copy] ******************************************************************************************
ok: [172.16.1.41]

PLAY [172.16.1.31] ***********************************************************************************

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

TASK [service] ***************************************************************************************
ok: [172.16.1.31]

TASK [yum] *******************************************************************************************
ok: [172.16.1.31]

PLAY RECAP *******************************************************************************************
172.16.1.31                : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.16.1.41                : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

脚本実際の実行

[root@m01 ansible_playbook]#ansible-playbook test.yaml

PLAY [172.16.1.41] ***********************************************************************************

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

TASK [service] ***************************************************************************************
ok: [172.16.1.41]

TASK [file] ******************************************************************************************
ok: [172.16.1.41]

TASK [copy] ******************************************************************************************
ok: [172.16.1.41]

PLAY [172.16.1.31] ***********************************************************************************

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

TASK [service] ***************************************************************************************
ok: [172.16.1.31]

TASK [yum] *******************************************************************************************
ok: [172.16.1.31]

PLAY RECAP *******************************************************************************************
172.16.1.31                : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.16.1.41                : ok=4    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

注:
システムはコマンドを実行し、cowsayソフトウェアが装備されている場合は、パターン情報を生成し、インパクトのレビュー結果、それは閉じることができます。

[root@m01 ansible]#vim ansible.cfg 
# don't like cows?  that's unfortunate.
# set to 1 if you don't want cowsay support or export ANSIBLE_NOCOWS=1
# nocows = 1
把# nocows = 1 中的 # 去掉即可。

キーの配置を達成するために、1.8 ansible rsyncのスクリプト

最初のコース:実施形態のモジュール、サービス展開の完了するために、各段階
のステップを:構成サーバー

# 安装软件程序
ansible rsync -m yum -a "name=rsync state=installed"
# 编写配置文件:要在批量管理主机上提前写好,然后推送给服务端
# 在管理端准备好服务配置文件
ansible rsync_server -m copy -a "src=/etc/ansible/conf_file/rsyncd.conf dest=/etc/"
# 创建虚拟用户
ansible rsync_server -m user -a "name=rsync create_home=no shell=/sbin/nologin"
# 创建密码文件 (授权600)
ansible rsync_server -m copy -a "content='rsync_backup:oldboy123' dest=/etc/rsync.password mode=600"
# 创建备份目录 (授权 属主 属组)
ansible rsync_server -m file -a "path=/backup state=directory owner=rsync group=rsync"
@ 启动程序服务
ansible rsync_server -m service -a "name=rsyncd state=started enabled=yes"

ステップ2:クライアントの設定

# 创建密钥文件 (授权600)
ansible rsync_client -m copy -a "content='oldboy123' dest=/etc/rsync.password mode=600"
# 批量测试传输文件
ansible rsync_client -m shell -a "rsync -avz /etc/hosts [email protected]::backup --password-file=/etc/rsync.password"

第二講座:スクリプトの情報を書き込みます

[root@m01 ansible_playbook]#vim rsync_auto.yaml 
- hosts: rsync_server
  tasks:
    - name: 01:install rsync
      yum: name=rsync state=installed
    - name: 02:copy conf file
      copy: src=/etc/ansible/conf_file/rsyncd.conf dest=/etc/
    - name: 03:create rsync user
      user: name=rsync create_home=no shell=/sbin/nologin
    - name: 04:create password file
      copy: content='rsync_backup:oldboy123' dest=/etc/rsync.password mode=600
    - name: 05:create backup dir
      file: path=/backup state=directory owner=rsync group=rsync
    - name: 06:boot rsync server
      service: name=rsyncd state=started enabled=yes

- hosts: rsync_client
  tasks:
    - name: 01:create password file
      copy: content='oldboy123' dest=/etc/rsync.password mode=600

回復環境脚本

[root@m01 ansible_playbook]#vim rsync_backup.yaml 
- hosts: rsync_server
  tasks:
    - name: 01:delete conf file
      file: path=/etc/rsyncd.conf state=absent
    - name: 02:delete rsync user
      user: name=rsync state=absent 
    - name: 03:delete password file
      file: path=/etc/rsync.password state=absent
    - name: 04:delete backup dir
      file: path=/backup/ state=absent
    - name: 05:boot rsync server
      service: name=rsyncd state=stopped enabled=no

- hosts: rsync_client
  tasks:
    - name: 01:delete password file
      file: path=/etc/rsync.password state=absent

キー配置は、NFSを達成1.9 ansibleプレイ

 最初のコース:モジュラーファッションによると、各ステップは、サービスの展開を完了するために

服务端配置 
01. 安装部署软件程序: rpcbind nfs-utile
ansible nfs_server -m yum -a "name=rpcbind state=installed"
ansible nfs_server -m yum -a "name=nfs-utile state=installed"
02. 编写配置文件:配置文件要提前写好
# 批量管理主机写好的配置文件推送给服务端/etc/ansible-playbook/nfs.conf 
ansible nfs_server -m copy -a "src=/etc/ansible/ansible_playbook/nfs.conf  dest=/etc/exports"
03. 创建共享目录:
ansible nfs_server -m file -a "path=/data/ state=directory owner=nfsnobody group=nfsnobody"
04. 启动程序服务:
ansible nfs_server -m service -a "name=rpcbind state=started enabled=yes"
ansible nfs_server -m service -a "name=nfs state=started enabled=yes"
    
客户端配置:
01. 安装部署软件 
ansible nfs_client -m yum -a "name=nfs-utile state=installed"
02. 挂载共享目录
ansible nfs_client -m mount -a "src=172.16.1.31:/data/ path=/mnt fstype=nfs state=mounted"

スクリプトを書くの第二のコース:

[root@m01 ansible_playbook]#vim nfs_auto.yaml 
- hosts: nfs_server
  tasks:
    - name: 1:install rpcbind nsf-utils
      yum:
        name:
          - rpcbind
          - nfs-utils
        state: installed
    - name: 2:copy conf file
      copy: src=/etc/ansible/ansible_playbook/nfs.conf dest=/etc/exports
    - name: 3:create data dir
      file: path=/data/ state=directory owner=nfsnobody group=nfsnobody
    - name: 4:boot server rcbind
      service: name=rpcbind state=started enabled=yes
    - name: 4:boot server nfs
      service: name=nfs state=restarted enabled=yes
- hosts: nfs_client
  tasks:
    - name: 1:install nfs
      yum: name=nfs-utils state=installed
    - name: 2:mount data dir
      mount: src=172.16.1.31:/data/ path=/mnt fstype=nfs state=mounted

回復環境脚本

[root@m01 ansible_playbook]#vim nfs_backup.yaml 
- hosts: nfs_server 
  tasks:
    - name: 01:install rpcbind nfs-utils
      yum:
        name:
          - rpcbind
          - nfs-utils
        state: removed
    - name: 02:copy conf file
      shell: echo ""  >/etc/exports
    - name: 03:create data dir
      file: path=/data/ state=absent
- hosts: nfs_client
  tasks:
    - name: 01:install nfs
      yum: name=nfs-utils state=removed
    - name: 02:mount data dir
      mount: src=172.16.1.31:/data/ path=/mnt fstype=nfs state=unmounted

最適化スクリプト:

[root@m01 ansible_playbook]#vim nfs_auto.yaml 
- hosts: nfs_server
  vars:
    conf_file: exports
    data_dir: /data
  tasks:
    - name: 01:install nfs rpcbind
      yum:
        name: ['nfs-utils', 'rpcbind'] 
        state: installed
    - name: 02:copy conf file
      copy: src=/etc/ansible/ansible_playbook/nfs.conf  dest=/etc/{{ conf_file }}
      notify: 
        - nfs_restart
    - name: 03:create data dir
      file: path={{ data_dir }} state=directory owner=nfsnobody group=nfsnobody
    - name: 04:boot server rpcbind
      service: name={{ item.name }} state={{ item.state }} enabled={{ item.enabled }}
      with_items:
        - {name: "rpcbind", state: "started", enabled: "yes"}
        - {name: "nfs",     state: "started", enabled: "yes"}
  handlers:
    - name: nfs_restart
      service: name=nfs state=reloaded
- hosts: nfs_client
  vars:
    data_dir: /data
  tasks:
    - name: 01:install nfs
      yum: name=nfs-utils state=installed
    - name: 02:mount data dir
      mount: src=172.16.1.31:{{ data_dir }} path=/mnt fstype=nfs state=mounted
    - name: 03:check mount info
      shell: df -h|grep mnt
      register: mount_info
    - debug: msg={{ mount_info.stdout_lines }}

1.10 ansible sersyncを達成するための重要な展開を果たしています

 最初のコース:モジュラーファッションによると、各ステップは、サービスの展開を完了するために

配置hosts主机清单
[server_server]
172.16.1.31
[server_client]
172.16.1.41

#安装rsync
ansible backup_server -m yum -a "name=rsync state=installed"
#在批量管理主机上下载sersync,解压发送给客户端
ansible backup_server -m file -a "src=/usr/local/sersync_installdir_64bit/sersync dest=/usr/local"
#在批量管理主机上写好sersync配置文件,发送给客户端
ansible backup_server -m copy -a "src=/usr/local/sersync_installdir_64bit/sersync/conf/confxml.xml dest=/usr/local/sersync/conf/"
#给sersync加上执行权限
ansible backup_server -m file -a "path=/usr/local/sersync/bin/sersync mode=a+x"
#给sersync创建软链接
ansible backup_server -m file -a "src=/usr/local/sersync/bin/sersync path=/usr/local/sbin/sersync state=link"
#启动sersync 测试实时同步
ansible backup_server -m shell -a "sersync -dro /usr/local/sersync/conf/confxml.xml"

スクリプトを書い二もちろん、

[root@m01 ansible_playbook]#vim sersync_auto.yaml 
- hosts: sersync_server
  tasks:
    - name: 安装rsync
      yum: name=rsync state=installed
    - name: 将sersync传输到客户端
      file: src=/usr/local/sersync_installdir_64bit/sersync/ dest=/usr/local
    - name: 将写好的配置文件传输到客户端
      copy: src=/usr/local/sersync_installdir_64bit/sersync/conf/confxml.xml dest=/usr/local/sersync/conf/
    - name: 加上执行权限
      file: path=/usr/local/sersync/bin/sersync mode=a+x
    - name: 创建软链接
      file: src=/usr/local/sersync/bin/sersync path=/usr/local/sbin/sersync state=link
    - name: 启动sersync 测试实时同步
      shell: sersync -dro /usr/local/sersync/conf/confxml.xml

回復環境脚本

[root@m01 ansible_playbook]#cat sersync_backup.yaml
- hosts: sersync_server
  tasks:
    - name: 卸载rsync 
      yum: name=rsync state=removed
    - name: 删除sersync
      file: path=/usr/local/sersync

複数のスクリプトを統合する方法2

最初のコース:各スクリプトが正常に実行されていることを確認し
、スクリプトの統合:第二のコースを
1は:お勧めできません

[root@m01 ansible_playbook]#vim zhenghe.yaml  # ---角色里使用
- hosts: all
  remote_user: root
  tasks:
    - include_tasks: nfs_auto.yml
    - include_tasks: rsync_auto.yml
# 不写hosts信息,只写任务信息

方法2:ansibleがキャンセルされてもよいした後関数を含みます

[root@m01 ansible_playbook]#vim zhenghe.yaml 
- include:nfs_auto.yml  
- include:rsync_auto.yml

方法3:このメソッドを使用することをお勧めします

[root@m01 ansible_playbook]#vim zhenghe.yaml 
- import_playbook: nfs_auto.yaml     
- import_playbook: rsync_auto.yaml 

以下のように書かれた3 ansibleスクリプト役割:

(1)仕様ansibleディレクトリ構造
定義されたホスト情報要約スクリプト(2)

3.1ロールコール・フロー図

役割.PNG

3.2 NFSサービスの役割を書きます

最初のコース:役割のディレクトリ構造を作成

cd roles/;mkdir {nfs,rsync,web,sersync} 
cd nfs/{vars,tasks,templates,handlers,files}
# vars:      定义变量信息
# tasks:     定义任务信息
# templates: 定义模板文件(jinja2模板文件)
# handlers:  定义触发器信息
# files:     定义需要分发的文件

第二講座:書き込みファイル情報の
タスク:方法で記述されたタスク情報:
NFSサービスの書き込み

vim main.yaml
- name: 01:install nfs rpcbind
  yum:
    name: ['nfs-utils', 'rpcbind'] 
    state: installed
- name: 02:copy conf file
  copy: src=/etc/ansible/ansible_playbook/nfs.conf  dest=/etc/{{ conf_file }}
  notify: 
    - nfs_restart
- name: 03:create data dir 
  file: path={{ data_dir }} state=directory owner=nfsnobody group=nfsnobody
- name: 04:boot server rpcbind
  service: name={{ item.name }} state={{ item.state }} enabled={{ item.enabled }}
  with_items:
    - {name: "rpcbind", state: "started", enabled: "yes"}
    - {name: "nfs",     state: "started", enabled: "yes"}
- name: 01:install nfs
  yum: name=nfs-utils state=installed
- name: 02:mount data dir
  mount: src=172.16.1.31:{{ data_dir }} path=/mnt fstype=nfs state=mounted
- name: 03:check mount info
  shell: df -h|grep mnt
  register: mount_info
- debug: msg={{ mount_info.stdout_lines }}

タスク:タスク情報は、第二の方法を記述します。
タスク:タスク情報を定義します

cd tasks
vim main.yaml
vim nfs_boot.yaml
vim nfs_conf.yaml
vim nfs_datadir.yaml
vim nfs_install.yaml
vim nfs_mount.yaml
#########################
vim main.yaml
- include_tasks: nfs_install.yaml
- include_tasks: nfs_conf.yaml
- include_tasks: nfs_datadir.yaml
- include_tasks: nfs_boot.yaml
- include_tasks: nfs_mount.yaml

VARS:変数情報の定義

vim main.yaml
conf_file: exports
data_dir: /data

ファイル:ファイルを配布する必要性を定義します

[root@m01 files]# ll
total 4
-rw-r--r-- 1 root root 42 Jul 29 10:34 nfs.conf

ハンドラ:トリガ情報を定義します

vim main.yaml 
- name: nfs_restart
service: name=nfs state=reloaded

おすすめ

転載: www.cnblogs.com/basa/p/11300911.html