12
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2016-03-13

どうも情弱氏の俺です。

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"
    }
}

12
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?