Ansible - exemples

- name: install truc import_tasks: "1-install-truc.yml" - name: conf truc import_tasks: "2-conf-truc.yml" - name: install plop import_tasks: "3-install-plop.yml"

Équivalent try except finally

- name: Attempt and graceful roll back demo block: - name: Print a message ansible.builtin.debug: msg: 'I execute normally' - name: Force a failure ansible.builtin.command: /bin/false - name: Never print this ansible.builtin.debug: msg: 'I never execute, due to the above task failing, :-(' rescue: - name: Print when errors ansible.builtin.debug: msg: 'I caught an error' - name: Force a failure in middle of recovery! >:-) ansible.builtin.command: /bin/false - name: Never print this ansible.builtin.debug: msg: 'I also never execute :-(' always: - name: Always do this ansible.builtin.debug: msg: "This always executes"

Plugin lookup

- name: raw result of running date command" debug: msg: "{{ lookup('pipe', 'date') }} - name: create a mysql user with a random password using many different char sets mysql_user: name: "{{ client }}" password" "{{ lookup('password', '/tmp/passwordfile chars=ascii_letters,digits,hexdigits,punctuation') }}" priv: "{{ client }}_{{ tier }}_{{ role }}.*:ALL"

Exemple config projet

.ansible-lint

--- skip_list: - '303' # Using command rather than module for yum/dnf - ignore-errors

.yamllint.yml

# SPDX-License-Identifier: MIT --- extends: .yamllint_defaults.yml # possible customizations over the base yamllint config # skip the yaml files in the /tests/ directory # NOTE: If you want to customize `ignore` you'll have to # copy in all of the config from .yamllint.yml, then # add your own - so if you want to just add /tests/ to # be ignored, you'll have to add the ignores from the base # ignore: | # /tests/ # /.tox/ # skip checking line length # NOTE: the above does not apply to `rules` - you do not # have to copy all of the rules from the base config

.yamllint_defaults.yml

# SPDX-License-Identifier: MIT --- ignore: | /.tox/ extends: default rules: braces: max-spaces-inside: 1 level: error brackets: max-spaces-inside: 1 level: error truthy: allowed-values: ["yes", "no", "true", "false"] level: error document-start: disable colons: disable

.yamllint

extends: default rules: colons: max-spaces-before: 1 max-spaces-after: 20 line-length: disable

ansible.cfg

[defaults] roles_path = ../
# To generate an example config file (a "disabled" one with all default settings, commented out): ansible-config init --disabled > ansible.cfg # Also you can now have a more complete file by including existing plugins: ansible-config init --disabled -t all > ansible.cfg # Voir la configuration : ansible-config dump --only-changed -t all

Voir ansible-navigator config

.gitignore

*.retry
/.vagrant
*.swp