Help us understand the problem. What is going on with this article?

AnsibleのVariables優先順位で死んだ話

More than 3 years have passed since last update.

どうも情弱氏の俺です。

vars/main.ymlをrole default variablesと勘違いしていた話です。

Ansible 2.0.0.2 での話です。

http://docs.ansible.com/ansible/playbooks_roles.html#role-default-variables

http://docs.ansible.com/ansible/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable
をよく読みましょうという話です。

使い回しの効くいい感じのプレイブック目指すならrole_name/defaults/main.ymlに書こうという話です。

overrideできなくて死ぬ

  • tree
orenomac$ tree  roles/mysql
roles/mysql
├── binaries
├── vars
│   └── main.yml
├── files
├── handlers
├── tasks
│   ├── main.yml
│   └── mysql_client.yml
└── templates
    └── my.cnf.j2
  • vars/main.yml
mysql_client:
  install: true
  version: "5.6"
mysql_server:
  install: false
  version: "5.6"
  • hosts

こーするじゃないですか。
overrideだと思ってて

- hosts: orenomassiiiiiiiinnnnnnn
  user: ec2-user
  vars:
    mysql_server:
      install: true
      version: "5.6"
  sudo: true
  roles:
    - mysql
  • ansible-playbook

ファーmysql_server.installがoverrideされてない

TASK [mysql : debug] ***********************************************************
ok: [orenomassiiiiiiiinnnnnnn] => {
    "mysql_server": {
        "install": false,
        "version": "5.6"
    }
}

overrideできて生き延びた

  • tree
orenomac$ tree  roles/mysql
roles/mysql
├── binaries
├── defaults
│   └── main.yml
├── files
├── handlers
├── tasks
│   ├── main.yml
│   └── mysql_client.yml
└── templates
    └── my.cnf.j2
  • defaults/main.yml
mysql_client:
  install: true
  version: "5.6"
mysql_server:
  install: false
  version: "5.6"
  • hosts

今度こそoverride

- hosts: orenomassiiiiiiiinnnnnnn
  user: ec2-user
  vars:
    mysql_server:
      install: unko
      version: "5.6"
  sudo: true
  roles:
    - mysql
  • ansible-playbook

ファーできた

TASK [mysql : debug] ***********************************************************
ok: [orenomassiiiiiiiinnnnnnn] => {
    "mysql_server": {
        "install": unko,
        "version": "5.6"
    }
}

gamisan9999
俺です。 株式会社アンドゲートに全力コミットしてます
http://www.andgate.co.jp
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした