Ansibleでの実行制御

##一.cyclic ###サイクル
反復タスク
##1。単純なサイクル#

loop:		       ##赋值列表
  - value1
  - value2
  - ...
  {
   
   {item}}	##迭代变量名称

インスタンス

---
- name: playbook test
  hosts: 172.25.6.1
  tasks:
    - name: test
      file:
        name: /mnt/{
   
   {item}}
        state: touch

      loop:
        - westos_file1
        - westos_file2
~                       

ここに写真の説明を挿入
ここに写真の説明を挿入

#2。循環ハッシュまたは辞書リスト#

---
- name: playbook test
  hosts: 172.25.6.1
  tasks:
    - name:
      dnf:
        name: "{
   
   { item.name }}"
        state: "{
   
   { item.state }}"
      loop:
        - name: httpd
          state: latest
        - name: vsftpd
          state: latest

ここに写真の説明を挿入
##二.条件##

when:
  - 条件1
  - 条件2

#条件判断#
=			value == "字符串",value == 数字
<			value <  数字
>			value >  数字
<=			value <= 数字
>=			value >= 数字 
!=			value != 数字
is defined value	value is defined	变量存在
is not defined		value is not defined	变量不存在
bool变量 为true		value			value的值为true
bool变量 false		not value		value的值为false
			value in value2		value的值在value2列表中
#多条条件组合#
when:
  条件1 and 条件2
  - 条件1
  - 条件2

when:
  条件1 or 条件2

when: >
  条件1
  or
  条件2

ここに写真の説明を挿入
ここに写真の説明を挿入
ここに写真の説明を挿入
##三。トリガー##
通知:変更が発生すると、トリガーはハンドラーをトリガーします

ハンドラー:トリガーがトリガーされた後に実行されるアクション

---
- name: playbook test
  hosts: 172.25.6.1
  vars_files:
    ./vhost_list.yml
  tasks:
    - name: create document
      file:
        path: "{
   
   { web2.document }}"
        state: directory
    - name: create vhost.conf
      copy:
        dest: /etc/httpd/conf.d/vhost.conf
        content:
          "<VirtualHost *:{
   
   {web1.port}}>\n\tServerName {
   
   {web1.name}}\n\tDocumentRoot {
   
   {web1.document}}\n\tCustomLog logs/{
   
   {web1.name}}.log combined\n</VirtualHost>\n\n<VirtualHost *:{
   
   {web2.port}}>\n\tServerName {
   
   {web2.name}}\n\tDocumentRoot {
   
   {web2.document}}\n\tCustomLog logs/{
   
   {web2.name}}.log combined\n</VirtualHost>"
      notify:
          restart apache


  handlers:
    - name: restart apache
      service: 
        name: httpd
        state: restarted

ここに写真の説明を挿入
ここに写真の説明を挿入
#四。
处理失敗タスク###1.ignore_errors #Function

遊びは、タスクの失敗に遭遇すると、それが終了します
IGNORE_ERRORSを:はいタスクの失敗を無視して、次のタスクを実行し続けます

---
- name: test
  hosts: 172.25.6.1
  ignore_errors: no
  tasks:
    - name: dnf
      dnf:
        name: westos
        state: latest
    - name: create file
      file:
        path: /mnt/westos
        state: touch

ここに写真の説明を挿入
ここに写真の説明を挿入
#2.force_handlers ## Function

タスクが失敗した後に再生が終了すると、トリガープロセスが呼び出されます

---
- name: apache change port
  hosts: 172.25.6.1
  force_handlers: yes
  vars:
    http_port: 80
  tasks:
    - name: configure appache conf
      lineinfile:
        path: /etc/httpd/conf/httpd.conf
        regexp: "^Listen"
        line: "Listen {
   
   {http_port}}"
      notify: restart apache
    - name: install error
      dnf:
        name: westos
        state: latest
  handlers:
    - name: restart apache
      service:
        name: httpd
        state: restarted
        enabled: yes

ここに写真の説明を挿入
ここに写真の説明を挿入
#3.changed_when ## Function

タスクが変更を報告するタイミングを制御する

---
- name: apache change port
  hosts: 172.25.6.1
  force_handlers: yes
  vars:
    http_port: 80
  tasks:
    - name: configure appache conf
      lineinfile:
        path: /etc/httpd/conf/httpd.conf
        regexp: "^Listen"
        line: "Listen {
   
   {http_port}}"
      changed_when: true
      notify: restart apache
    - name: install error
      dnf:
        name: westos
        state: latest
  handlers:
    - name: restart apache
      service:
        name: httpd
        state: restarted
        enabled: yes     

ここに写真の説明を挿入
ここに写真の説明を挿入
#4.failed_when ##
条件が満たされたときにタスクを強制的に失敗させる

---
- name: test
  hosts: 172.25.6.1
  tasks:
    - name: shell
      shell: echo hello
      register: westos
      failed_when: "'hello' in westos.stdout"

ここに写真の説明を挿入
ここに写真の説明を挿入
#5.block#
ブロック:##実行
するタスクを定義するレスキュー:##失敗したタスクが
常にブロック文に表示された後に実行するタスクを定義する:##最終的に独立して実行されるタスクを定義する
例:
プレイブックを作成する〜/ westos.yml要件次のように:
サイズが1500Mの/ dev / vdb1という名前のデバイスを作成します
。/dev/vdbが存在しない場合は、次のように入力します
。/dev / vdbは存在しません。/dev/vdb
のサイズが
1.5G未満の場合、出力してください:/ dev / vdbはより小さい1.5G
で800Mサイズを作成/ dev / vdb1
このデバイスは/ westosにマウントされています

---
- name: fdisk
  hosts: all
  tasks:
    - name: check
      debug:
        msg: "{
   
   {ansible_facts.enp1s0.ipv4.address}} is not exist with /dev/vdb"
      when: ansible_facts.devices.vdb is not defined
    - name: create disk
      block:
        - name: create 1500M
          parted:
             device: /dev/vdb
             number: 1
             state: present
             part_end: 1500MiB
          when: ansible_facts.devices.vdb is defined
      rescue:
        - name: disk 800
          parted:
             device: /dev/vdb
             number: 2
             state: present
             part_end: 800MiB
          when: ansible_facts.devices.vdb is defined
      always:
        - name: xfs
          filesystem:
             dev: /dev/vdb1
             fstype: xfs
        - name: mounted
          mount:
            path: /westos
            src: /dev/vdb1
            fstype: xfs
            state: mounted

ここに写真の説明を挿入
赤はブロック、
青はレスキュー、
緑は常に

おすすめ

転載: blog.csdn.net/qq_42958401/article/details/108727579