[Ansible] What I am careful about when writing ansible

It has become much more convenient (my writing style is older) than the old version, so a memo when I switched the management of my dot file to ansible and tried various things

environment

Referenced site

Read the Best Practices directory layout

Looking at various repositories, it seems that ansible is written with various directory structures, but the official document [Directory layout](http://docs.ansible.com/ansible/playbooks_best_practices.html#directory- Since it is described in layout), let's write (or aim) with a directory structure that refers to it

Things related to judgment (when system)

There are various problems that cannot be solved by modules alone.

The changed of the playbook execution result is always changed only when the behavior of the target host changes.

Although described in

If it's a playbook you wrote, this is a changed you don't have to worry about. You can judge immediately by looking at the execution result of ansible, but if you get a changed when someone else executes this playbook, you will be wondering, "Oh, has something changed?" I think it is desirable that changed becomes changed only when the behavior of the target OS changes.

If you do not do so, changed will become scary, and you will not be able to write common tasks for production, staging, develop, etc., and you will enter the process of completing tasks according to each environment. At that point, the "secret sauce" should be quite advanced. I feel idempotent for what, but I don't know.

Since when is fast, write the execution condition branch as much as possible with when

It's a speedy story, but from changed_when [when](http://docs.ansible. com / ansible / playbooks_error_handling.html # controlling-what-defines-failure) often feels faster to run playbooks, so use when (Maybe chaged_when feels like it's doing an action and then a when decision. I'm sorry if it's wrong.)

For OS-specific modules, add ``` when:" ansible_os_family =='{uname}' "` ``

For example, if you limit the target OS to OS X when writing the task to install nkf, you would write like this

roles/nkf/install/tasks/main.yml


- name: Install the nkf from homebrew
  homebrew: name=nkf state=present

But nkf is also necessary for guest OS, right? If you write this so that you do not create useless roles when it happens, you can guarantee the same operation to different OS with one role.

roles/nkf/install/tasks/main.yml


- name: Install the nkf from homebrew
  homebrew: name=nkf state=present
  when: "ansible_os_family == 'Darwin'"

- name: Install the nkf from yum
  yum: pkg=nkf state=present
  when: "ansible_os_family == 'RedHat'"

With `fail_when: no` rather than ignore_erros

There is ignore_errors because you can ignore it even if an error occurs.

"disgusting····"

Or rather, if ignore_errors occurs when running the playbook, failed is displayed on the display.

Something red appears on the console, even if it's not ansible.

What you want to do with ignore_errors is to ignore fail, so if you ignore it, don't cause fail

failed_when: no

Let's write. That way you won't get failed on the console.

Use meta if you have a dependent role

For example, chrome is required when installing chrome-cli that operates chrome with CUI.

The way to write the task in that case is like this

Task to install chrome-cli

roles/homebrew/brew/chrome-cli/tasks/main.yml


- name: Install the chrome-cli in the brew.
  homebrew: name=chrome-cli state=present
  when: "ansible_os_family == 'Darwin'"

Just install chrome-cli with homebrew

Task to install google-chrome

roles/homebrew/brew-cask/google-chrome/tasks/main.yml


- name: Install the google-chrome in the brew-cask
  homebrew_cask: name=google-chrome state=present
  when: "ansible_os_family == 'Darwin'"

Just install google-chrome with homebrew-cask

chrome-cli is homebrew / brew-cask / google-chrome dependency meta

homebrew/brew/chrome-cli/meta/main.yml


dependencies:
  - { role: homebrew/brew-cask/google-chrome }
chrome-cli
├── meta
│   └── main.yml
└── tasks
    └── main.yml

google-chrome
└── tasks
    └── main.yml

Write two roles and describe the dependencies in meta

If you want to wget, use get_url

As the title says. For personal notes

File directory manipulation

I often forget this and look at the documentation of the ansible module, so my memo

File / directory existence check is stat

If you look at the above examples, you can do most of the things.

Finally

I will add it if there is an additional note.

Recommended Posts

[Ansible] What I am careful about when writing ansible
What I was careful about when implementing Airflow with docker-compose
A miscellaneous summary of what I researched about Ansible
What I learned about Linux
What I was worried about when displaying images with matplotlib
What I checked about Qiita's post
What I got into when using Tensorflow-gpu
What I referred to when studying tkinter
What I did when updating from Python 2.6 to 2.7
[Question] What happens when I use% in python?
What I learned about AI / machine learning using Python (1)
What I do when imitating embedded go in python
What I did when I stumbled on a Django tutorial
What I learned about AI / machine learning using Python (3)
What I was addicted to when using Python tornado
What I learned about AI / machine learning using Python (2)
A note of what I learned when I thought about using pyenv or virtualenv on Windows