Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
10
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

@gamisan9999

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

どうも情弱氏の俺です。

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

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
10
Help us understand the problem. What are the problem?