ansibleでタスクをスキップしても同名のregisterが設定される

ansibleでデプロイするタスクを書いていて遭遇した仕様の話。

やりたいことは、gitのtagやbranch名を引っ張ってきてslackに通知したい。
ステージング環境でのデプロイでも使っているタスクなので、「masterブランチのtag」が入るか、「develop/test」などのブランチが入ってきても適切に通知したい。

最初は、

# branchがmasterの場合、gittagにtag名を入れる
  - name: git status
    shell: git status | head -1 | awk '{print $2}'
    args:
      chdir: "{{ workdir }}/{{ repobase }}/{{ project }}"
    register: branch_name
    changed_when: false

  - name: check tag version
    shell: git status | head -1 | awk '{print $5}'
    args:
      chdir: "{{ workdir }}/{{ repobase }}/{{ project }}"
    register: gittag
    when: branch_name.stdout == "HEAD"
    changed_when: false


# branchがmasterではない場合、gittagにブランチ名を入れる
  - name: check branch
    shell: git status --short --branch | awk '{print $2}'
    args:
      chdir: "{{ workdir }}/{{ repobase }}/{{ project }}"
    register: gittag
    when: branch_name.stdout != "HEAD"
    changed_when: false

# slack送信
  - name : send tag (finish)
    slack:
      token: '{{ slack_token }}'
      msg: "デプロイが開始します\n
            ``` 
            project: {{project}}\n
            env: {{env}}\n
            version: {{ gittag.stdout }}\n
            date: {{ lookup(\"pipe\",\"date +%Y/%m/%d-%H:%M:%S\") }}
            ```"
      channel: '{{ post_channel }}'
      color: good
    run_once: true

と書いたが、branchがmasterで、check branchタスクがskipされているにも関わらず、register: gittagの中身が空っぽになる。
公式を確認すると、

If a task fails or is skipped, the variable still is registered with a failure or skipped status, the only way to avoid registering a variable is using tags.

タスクが失敗したり、スキップしたりした場合も変数は設定される。
らしい。

結局、以下のように修正。

  vars_files:
    - ../vars/vars.yml
  tasks:

# branchがmasterの場合、gittagにtag名を入れる
  - name: git status
    shell: git status | head -1 | awk '{print $2}'
    args:
      chdir: "{{ workdir }}/{{ repobase }}/{{ project }}"
    register: branch_name
    changed_when: false

  - name: check tag version
    shell: git status | head -1 | awk '{print $5}'
    args:
      chdir: "{{ workdir }}/{{ repobase }}/{{ project }}"
    register: gittag
    when: branch_name.stdout == "HEAD"
    changed_when: false


# branchがdevelopではない場合、gittagにブランチ名を入れる
  - name: check branch
    shell: git status --short --branch | awk '{print $2}'
    args:
      chdir: "{{ workdir }}/{{ repobase }}/{{ project }}"
    register: gitbranch
    when: branch_name.stdout != "HEAD"
    changed_when: false

# slack送信
  - name : send tag (finish)
    slack:
      token: '{{ slack_token }}'
      msg: "デプロイを開始します\n
            ``` 
            project: {{project}}\n
            env: {{env}}\n
            version: {{ gittag.stdout }}\n
            date: {{ lookup(\"pipe\",\"date +%Y/%m/%d-%H:%M:%S\") }}
            ```"
      channel: '{{ post_channel }}'
      color: good
    when: branch_name.stdout == "HEAD"
    run_once: true


  - name : send branch (finish)
    slack:
      token: '{{ slack_token }}'
      msg: "デプロイを開始します\n
            ``` 
            project: {{project}}\n
            env: {{env}}\n
            version: {{ gitbranch.stdout }}\n
            date: {{ lookup(\"pipe\",\"date +%Y/%m/%d-%H:%M:%S\") }}
            ```"
      channel: '{{ post_channel }}'
      color: good
    when: branch_name.stdout != "HEAD"
    run_once: true

上手い書き方無いかなぁ

CentOS7のファイルディスクリプタの設定

確認方法

cat /proc/`pgrep -f サービス名`/limits

Limit                     Soft Limit           Hard Limit           Units     
Max cpu time              unlimited            unlimited            seconds   
Max file size             unlimited            unlimited            bytes     
Max data size             unlimited            unlimited            bytes     
Max stack size            8388608              unlimited            bytes     
Max core file size        0                    unlimited            bytes     
Max resident set          unlimited            unlimited            bytes     
Max processes             29222                29222                processes 
Max open files            1024                1024                files     
Max locked memory         65536                65536                bytes     
Max address space         unlimited            unlimited            bytes     
Max file locks            unlimited            unlimited            locks     
Max pending signals       29222                29222                signals   
Max msgqueue size         819200               819200               bytes     
Max nice priority         0                    0                    
Max realtime priority     0                    0                    
Max realtime timeout      unlimited            unlimited            us        

変更方法

/usr/lib/systemd/system/サービス管理ファイル に、

LimitNOFILE=65536

を追加する。

追加した跡は、

systemctl daemon-reload
systemctl restart daemon

として、サービスを再起動する。